Configure Jenkins CI/CD with GitHub and deploy to server via SSH
Install Java Version 8 — Jenkins is a Java based application, hence Java is a must.
Prerequisite: Install Java Version 8
To install Java version 8 execute the below command:
sudo yum install java-1.8.0-openjdk
Step 1 — Installing Jenkins
First, add the repository key to the system:
wget -q -O — https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
When the key is added, the system will return OK. Next, append the Debian package repository address to the server’s sources.list:
sudo sh -c ‘echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list’
When both of these are in place, run update so that apt will use the new repository:
sudo apt update
Finally, install Jenkins and its dependencies:
sudo apt install jenkins
Step 2— Starting Jenkins
Let’s start Jenkins using systemctl:
sudo systemctl start jenkins
Since systemctl doesn’t display output, you can use its status command to verify that Jenkins started successfully:
sudo systemctl status jenkins
Step 3 — Opening Firewall
By default, Jenkins runs on port 8080, so let’s open that port using ufw:
sudo ufw allow 8080
Step 4— Setting up Jenkins
To set up your installation, visit Jenkins on its default port, 8080, using your server domain name or IP address: http://your_server_ip_or_domain:8080
You should see the Unlock Jenkins screen, which displays the location of the initial password:
In the terminal window, use the cat command to display the password:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Copy the 32-character alphanumeric password from the terminal and paste it into the Administrator password field, then click Continue.
The next screen presents the option of installing suggested plugins or selecting specific plugins:
We’ll click the Install suggested plugins option, which will immediately begin the installation process:
When the installation is complete, you will be prompted to set up the first administrative user. It’s possible to skip this step and continue as admin using the initial password we used above, but we’ll take a moment to create the user.
Enter the name and password for your user:
You will see an Instance Configuration page that will ask you to confirm the preferred URL for your Jenkins instance. Confirm either the domain name for your server or your server’s IP address:
After confirming the appropriate information, click Save and Finish. You will see a confirmation page confirming that “Jenkins is Ready!”:
Click Start using Jenkins to visit the main Jenkins dashboard:
At this point, you have completed a successful installation of Jenkins.
Install Build Pipeline Plugin in Jenkins
Step 1) The settings for the plugin can be found under Manage Jenkins > Manage Plugins.
If you have already installed the plugin, it is shown under the installed tab.
Install GIT Plugin
In the Plugins Page
- Select the GIT Plugin
- Click on Install without restart. The plugin will take a few moments to finish downloading depending on your internet connection, and will be installed automatically.
- You can also select the option Download now and Install after restart button. In which plugin is installed after restart
- You will be shown a “No updates available” message if you already have the Git plugin installed.
Step 4: Once the plugins have been installed, go to Manage Jenkins on your Jenkins dashboard. You will see your plugins listed among the rest.
Integrate Your GitHub Repository to Your Jenkins Project
Step 1: Go to your GitHub repository and click on ‘Settings’.
Step 2: Click on Webhooks and then click on ‘Add webhook’.
Step 3: in the ‘Payload URL’ field, paste your Jenkins environment URL. At the end of this URL add /github-webhook/. In the ‘Content type’ select ‘application/json’ and leave the ‘Secret’ field empty.
Step 4: in the ‘Which events would you like to trigger this webhook?’ choose ‘Let me select individual events.’ Then, check ‘Pull Requests’ and ‘Pushes’. At the end of this option, make sure that ‘Active’ is checked and click on ‘Add webhook’.
We’re done with the configuration on GitHub’s side!
Creating your first Jenkins pipeline
Step 1: Log into Jenkins and select ‘New item’ from the dashboard.
Step 2: Next, enter a name for your pipeline and select ‘pipeline’ project. Click on ‘ok’ to proceed.
Step 3: Select the GitHub project checkbox and set the Project URL to point to your GitHub Repository.
Step 4: Now Under Build Triggers tab, select the “Build when a change is pushed to GitHub” checkbox.
Step 5: Scroll down to the pipeline and choose if you want a declarative pipeline or a scripted one.
Step 5a: If you want a scripted pipeline then choose ‘pipeline script’ and start typing your code.
Step 5b: If you want a declarative pipeline then select ‘pipeline script from SCM’ and choose your SCM. In my case I’m going to use Git throughout this demo. Enter your repository URL.
Step 6: Within the script path is the name of the Jenkinsfile that is going to be accessed from your SCM to run. Finally click on ‘apply’ and ‘save’. You have successfully created your first Jenkins pipeline.
Declarative Pipeline Demo
Now that I’ve explained the code, lets run the pipeline. The following screenshot is the result of the pipeline. In the below image, the pipeline waits for the user input and on clicking ‘proceed’, the execution resumes.
Scripted Pipeline Demo
To give you a basic understanding of the scripted pipeline, lets execute a simple code. Refer to Creating your first Jenkins pipeline to create the scripted pipeline. I will run the following script.
Install SSH Agent plugin
Search for “ssh agent” plugin in Available Plugins option
Install and restart the Jenkins.
Generate RSA Key Pair on server
After entering the command, you should see the following output:
Generating public/private rsa key pair.
Enter file in which to save the key (/your_home/.ssh/id_rsa):
Press ENTER to save the key pair into the .ssh/ subdirectory in your home directory, or specify an alternate path.
You should then see the following prompt:
Enter passphrase (empty for no passphrase):
You should then see the following output:
Your identification has been saved in /your_home/.ssh/id_rsa.
Your public key has been saved in /your_home/.ssh/id_rsa.pub.
The key fingerprint is:
The key’s randomart image is:
+ — [ RSA 2048] — — +
| ..o |
| E o= . |
| o. o |
| .. |
| ..S |
| o o. |
| =o.+. |
|. =++.. |
+ — — — — — — — — -+
To read the ssh key, run the command:
Setup the credentials
Create a new credential of type SSH Username with private key:
Configure “ssh remote host” with the credentials you created above:
Go to Jenkins → Manage Jenkins → Configure System and check for SSH Remote hosts and give the hostname, number and add the username and password of remote hosts
Configure the Jenkinsfile for deploying python app to server:
Here,[‘ssh-key’] is the ID you gave to your ssh credentials.It can be different in your case.
Push the code to your Git repository.This will automatically trigger the pipeline.