Setup Jenkins on Ubuntu 20.04 / 22.04 and run Jobs are root user

Requirements

Add the repository key to the system and append the Debian package repository.

wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo apt-key add -

sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'

sudo apt update

Install Java 17

sudo apt install openjdk-17-jre-headless

Update JAVA_HOME in .bashrc : nano ~/.bashrc

export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64

Install Jenkins

sudo apt install jenkins

Allow firewall for 8080 port

sudo ufw allow 8080
sudo ufw status

Start Jenkins

sudo systemctl start jenkins

Check Jenkins Status

sudo systemctl status jenkins

Enable Port in AWS

If you are using AWS for hosting, you will need to add 8080 port to security group. Open your Security Group and check Inbound rules. Then click on Edit inbound rules.

Click on Add rule button at bottom left and add new port, once done Save rules.

Get default password and create your own user.

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Use root as Jenkins User for running Jobs

sudo nano /etc/default/jenkins

Update JENKINS_USER and JENKINS_GROUP at end of file.

# Change Default Jenkins user - Username
JENKINS_USER=root
JENKINS_GROUP=root

Update Jenkins Service for user

sudo nano /usr/lib/systemd/system/jenkins.service
# Change Default Jenkins user - Username
User=root
Group=root

Reload units by

sudo systemctl daemon-reload

Restart jenkins

sudo service jenkins restart

Verify by adding whoami in Job script.


Provide necessary ssh keys to Jenkins

cd /var/lib/jenkins/
mkdir /var/lib/jenkins/.ssh/

Copy your private key here

cp ~/.ssh/id_mykey /var/lib/jenkins/.ssh/

How to Load SSH Key in Job

eval `keychain --eval id_mykey`

Run Job via Webhook when Github Push Event occurs

Github Settings

Add Webhook URL to your github project by Settings > Webhooks > Add Webhook. Use following as Payload URL

http://mydomain.com:8080/github-webhook/

Content type: application/json
Secret: Keep Empty.
Just the push event
Check Active
Click Add webhook button


Jenkins Job Settings

Now in Jenkins, Create / Open a Job and do following settings:

General > GitHub project > Project url: Put Your Github Web URL.

Source Code Management > Git > Repository URL:

git@github.com:username/my-repository.git

In Credentials, Click on Add and Select Jenkings.

Domain : Global
Kind : SSH Username with private key

Scope : Global
ID: Keep Empty
Description : Keep Empty
Username: Use Github Username
Private Key > Check Enter directly:
Private Key > Key > Click Add

Now Open terminal and get contents of your priavte key by cat ~/.ssh/id_mykey. Copy that key content to Textarea.

Passphrase : Enter if given at the time of Private Key Creation.

Click on Add and select in Credentials list.

Build Triggers > Check GitHub hook trigger for GITScm polling

Save Job.

No try pushing commit to your repository.


I hope this helps you !

References:

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.