Installing Jenkins in Local and Create a Pipeline Connected to Private Github Repo
Install Jenkins
First, install Jenkins (I specifically use Fedora). After installation is finished, you will be presented the configuration dashboard when accessing it from web (by default localhost:8080). Specify the admin username and just installed suggested plugins. For website URL, keep it localhost:8080
if you run it on local.
Add Credentials
In order to connect to github privates repository, you will need to set ssh credential both on Jenkins and put the public key on Github.
Create a new SSH key
In linux, use ssh-keygen
command to generate a new ssh key. Be careful not to overwrite you current existing ssh keys. You might want to backup your current ~/.ssh/id_rsa(.pub)
files to make sure you won’t lose your existing ssh keys. During the creation, set different path (by default it’s ~/.ssh/id_rsa
) to keep the key, for example type /home/username/.ssh/jenkins/id_rsa
to store the new ssh key pair in ~/.ssh/jenkins
). Once finish, the ssh key pair will be created in the directory you chose.
Add SSH credential to Jenkins
Go to jenkins dashboard, click “Manage Jenkins” on the left panel. Then click “Credentials” under security section. In Credentials dashboard, click “System” then “Global credentials”, then click “Add Credentials”.
On New Credentials dashboard, choose SSH username with private key for the “kind”, leave scope as Global and enter the id (will be used during credential setup on pipeline) and username as you please. Then on Private Key section, click “Enter Directly”, then click Add.
You will need to copy the private key that has been generated on the previous step. If you put the new SSH key pair in ~/.ssh/jenkins
as the previous example, then you can copy it to clipboard using xclip like this:
cat ~/.ssh/jenkins/id_rsa | xclip -selection clipboard
Then paste the key in Jenkins
Then click Create.
Add the public key to github
You have installed the private key in jenkins. Now you need to put the public key in your github account. Go to github.com then click your avatar on the top right of the dashboard, choose Settings. On Settings dashboard, click “SSH and GPG keys”, then click New SSH Keys.
On Add new SSH Key dashboard, put the Title as you please, for example: “Jenkins Local”. Then paste the public key to the text area. To copy public key from your machine, use the same command as when you copy the private key but on the .pub
file:
cat ~/.ssh/jenkins/id_rsa.pub | xclip -selection clipboard
Paste the command to the text area in github, then click “Add SSH Key”.
Change Known Host configuration
It’s a hassle to manually set “known host” for SSH connection between Jenkins machine to any other third party (Somehow I couldn’t change user to jenkins in my local). Thus, let’s change the configuration to automatically put the host to known host on the first connection.
From jenkins home, click “Manage Jenkins”, then click “Security”. Scroll down until you see section “Git Host Key Verification Configuration”, then change the value to “Accept first connection”
Click Save
Create First Pipeline
Once you setup the credential, you can create the first pipeline on Jenkins. From Jenkins Dashboard, click “New Item”, choose “Pipeline”.
On Pipeline configuration, go to Pipeline section and choose “Pipeline script from SCM”. Note that you must already have Jenkinsfile in the code repository to be used for this pipeline. For SCM choose Git, enter the ssh url for the private repository, and choose the credential that you have set on the previous step.
If you don’t see any red message indicating some error, then that means jenkins is succefully connecting to the repository. Now click “Save”.