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
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
Step 3: Install Jenkins
sudo apt update
sudo apt install -y jenkins
Step 4: Start & Enable Jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins
sudo systemctl status jenkins
- 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
- Verify port:
ss -tulnp | grep jenkins
- Allow firewall access:
sudo ufw allow 8080
- 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
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
- Generate SSH key for Jenkins user:
ssh-keygen -t rsa -b 4096 -C "jenkins@jenkins-server"
- Copy the public key to your application server:
ssh-copy-id user@application-server-ip
- Test password-less login:
ssh user@application-server-ip "ls -la"
Step 9: Add SSH Key to Jenkins Credentials
- Go to Manage Jenkins → Credentials → Global → Add Credentials
- 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
- In GitLab → Settings → Personal Access Tokens, create a token:
-
Token Name:
jenkins-server
- Expiration Date: As desired
- Scopes: Check API
-
Token Name:
- Copy the generated token.
- In Jenkins, add it as a credential:
- Kind: GitLab API token
- API Token: Paste the token
-
ID:
gitlab-access-token
- Configure GitLab connection in Jenkins:
-
Connection Name:
gitlab.com
-
GitLab Host URL:
https://gitlab.com
-
Credential:
gitlab-access-token
- Apply/Save
-
Connection Name:
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
- Add this public key in GitLab: Preferences → SSH Keys → Add new key
Step 12: Create Jenkins Pipeline Job
- Go to New Item → Pipeline
- Configure GitLab connection:
- Triggers: Check Build when a change is pushed to GitLab
- 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'"
}
}
}
}
}
- 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)