DEV Community

M. K. Tanjin Sarker
M. K. Tanjin Sarker

Posted on

Complete Step-by-Step Jenkins CI/CD Deployment on Ubuntu with GitLab

Step 1: Update Ubuntu & Install Dependencies

Install Java 17 (required by Jenkins), curl, and other dependencies:

sudo apt update
sudo apt install -y openjdk-17-jdk curl gnupg2 apt-transport-https software-properties-common
Enter fullscreen mode Exit fullscreen mode

Step 2: Add Jenkins Repository

Add the Jenkins GPG key and repository:

curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee \
    /usr/share/keyrings/jenkins-keyring.asc > /dev/null

echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian binary/ | \
    sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
Enter fullscreen mode Exit fullscreen mode

Step 3: Install Jenkins

sudo apt update
sudo apt install -y jenkins
Enter fullscreen mode Exit fullscreen mode

Step 4: Start & Enable Jenkins

sudo systemctl start jenkins
sudo systemctl enable jenkins
sudo systemctl status jenkins
Enter fullscreen mode Exit fullscreen mode
  • Default Jenkins runs on port 8080.
  • To change the port:
sudo nano /etc/default/jenkins
# Change HTTP_PORT=8080 to HTTP_PORT=8008

sudo nano /lib/systemd/system/jenkins.service
# Add Environment="JENKINS_PORT=8008"

sudo systemctl daemon-reload
sudo systemctl restart jenkins
Enter fullscreen mode Exit fullscreen mode
  • Verify port:
ss -tulnp | grep jenkins
Enter fullscreen mode Exit fullscreen mode
  • Allow firewall access:
sudo ufw allow 8080
Enter fullscreen mode Exit fullscreen mode
  • Open Jenkins in your browser: http://<jenkins-server-ip>:8080

Step 5: Unlock Jenkins

Get the initial admin password:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Enter fullscreen mode Exit fullscreen mode

Use this password to login via the browser.


Step 6: Install Recommended Plugins

  • Choose Install suggested plugins.
  • Create the first admin user and continue.

Step 7: Install Essential Plugins

Go to Manage Jenkins → Plugins → Available and install:

  • GitLab
  • SSH Agent
  • GitLab API

Restart Jenkins after installation.


Step 8: Setup SSH Access for Deployment

  1. Generate SSH key for Jenkins user:
ssh-keygen -t rsa -b 4096 -C "jenkins@jenkins-server"
Enter fullscreen mode Exit fullscreen mode
  1. Copy the public key to your application server:
ssh-copy-id user@application-server-ip
Enter fullscreen mode Exit fullscreen mode
  1. Test password-less login:
ssh user@application-server-ip "ls -la"
Enter fullscreen mode Exit fullscreen mode

Step 9: Add SSH Key to Jenkins Credentials

  1. Go to Manage Jenkins → Credentials → Global → Add Credentials
  2. Configure as:
    • Kind: SSH Username with private key
    • Scope: Global
    • ID: deploy-private-key
    • Username: root (or your server user)
    • Private Key: Enter directly (paste your ~/.ssh/id_rsa)

Step 10: Add GitLab Access Token

  1. In GitLab → Settings → Personal Access Tokens, create a token:
    • Token Name: jenkins-server
    • Expiration Date: As desired
    • Scopes: Check API
  2. Copy the generated token.
  3. In Jenkins, add it as a credential:
    • Kind: GitLab API token
    • API Token: Paste the token
    • ID: gitlab-access-token
  4. Configure GitLab connection in Jenkins:
    • Connection Name: gitlab.com
    • GitLab Host URL: https://gitlab.com
    • Credential: gitlab-access-token
    • Apply/Save

Step 11: Add Application Server SSH Key to GitLab

On your application server:

ssh-keygen -t rsa -b 4096 -C "server.application"
cat ~/.ssh/id_rsa.pub
Enter fullscreen mode Exit fullscreen mode
  • Add this public key in GitLab: Preferences → SSH Keys → Add new key

Step 12: Create Jenkins Pipeline Job

  1. Go to New Item → Pipeline
  2. Configure GitLab connection:
    • Triggers: Check Build when a change is pushed to GitLab
  3. Pipeline script example:
pipeline {
    agent any

    stages {
        stage('Deploy main') {
            steps {
                sshagent(['deploy-private-key']) {
                    sh "ssh -o StrictHostKeyChecking=no -p 22 user@application_server_ip 'cd /var/www/html/application && git pull origin main'"
                }
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Adjust the SSH port and paths as needed.

Step 13: Build & Deploy

Now, whenever you push to GitLab, Jenkins will:

  • Pull the latest code from the repository
  • Deploy it automatically to your application server

Check the Build History in Jenkins to verify successful deployments.


Your CI/CD pipeline is now fully functional, secure, and automated!

Top comments (0)