How do I install Jenkins CI

How to install Jenkins on CentOS and connect to GitHub

Jenkins offers widely used open source CI / CD tools. In this article we will learn how to install Jenkins and use Github to configure it to send files to the server via SSH.

Jenkins installation:

Jenkins is a JAVA based application so we need to install JAVA first. The following command installs JAVA on centos 7.

sudo yum install java-1.8.0-openjdk-devel

Check Java version

[[email protected] ~] # java -version openjdk version "1.8.0_212" OpenJDK Runtime Environment (build 1.8.0_212-b04) OpenJDK 64-Bit Server VM (build 25.212-b04, mixed mode)

Now configure the Jenkins repository with the following commands.

curl --silent --location http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo | sudo tee /etc/yum.repos.d/jenkins.repo

This shows the following outputs.

[jenkins] name = Jenkins-stable baseurl = http: //pkg.jenkins.io/redhat-stable gpgcheck = 1

Then run the following command to add the Jenkins key.

sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key

Now we have configured the Jenkins repository and are ready to install Jenkins with the following simple commands.

sudo yum install jenkins

After the installation is complete, check the Jenkins service and enable it when the system reboots.

sudo systemctl start Jenkins sudo systemctl enable jenkins

Now restart the system and make sure the jenkins service is running by using the command below.

sudo systemctl enable jenkins

Now allow Jenkins in the firewall.

firewall-cmd --permanent --add-port = 8080 / tcp firewall-cmd --reload

Configure the settings:

We can't use Jenkins directly, we need to enter the domain name or IP address in the web browser followed by the standard Jenkins port number 8080.

In the screenshot above, we have to enter the password generated automatically by Jenkins once. Run the following command to get a password.

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

This will give an output like 2179c452db3b45f98cf9e4c4961ab635.

After the password we need to install plugins on the Jenkins system. To do this, simply select Installed Suggested Plugin to install all standard plugins.

Fill in all the information and click Save and continue.

Now we are asked for the instance configuration, where we can also change the standard port number.

Now our Jenkins is safe and ready to use.

Congratulations, the new Jenkins setup has been successfully completed and is ready to use.

The following screenshot shows the default dashboard for Jenkins.

Jenkins configuration:

Install plugins:

Before we move on to the next steps, make sure you install the following plugins.

Go to Manage Jenkins -> Manage Plugin -> Available -> Filters -> look for the following plugin which is needed to integrate with GitHub.

Publish via SSH
Git
GitHub plugin
Git client plugin

Here the Publish Over SSH plugin had to push our files to the server via SSH.

Add credentials:

We need to add server credentials in Jenkins.

Are you going to the Jenkins Dashboard? Access data ? System? Global access data (unrestricted)? Add credentials

We can choose the type of credentials we want to install. Here we are adding GitHub username and password for later use.

ssh on Jenkins server.

Jenkins can connect to a remote server through ssh and run scripts, execute commands, or copy files from one server to another.

For this purpose we need to establish an ssh connection between Jenkins Server & Remote Server.

The next step is to add SSH credentials on Jenkins that we want to connect to the Jenkins server.

We want to connect to a Jenkins web server with the user root. For example, our web server is 138.197.151.144 and the user is root, so we need to log into this server with ssh.

ssh [email protected]

Now go to the home directory and cd to the .ssh folder.

cd ~ / .ssh

The next step will be to generate a private and public key for the user root on our web server.

ssh-keygen -t rsa -f private_key_for_jenkins

After the above command is executed, it will ask for “Enter passphrase”, we do not need to enter the passphrase. So click Enter twice and two keys will be generated on the web server private_key_for_jenkins and private_key_for_jenkins.pub.

Here private_key_for_jenkins is the private key of 138.197.151.144 and we have to import the content of this file into our Jenkins server.

We have the second file called private_key_for_jenkins.pub which is the web server's public key. We need to copy its data to another file called authorized_keys which is in the same directory.

In most cases the authorized_keys files exist in the ~ / .ssh / folder, but sometimes they are missing and we need to generate new authorized_keys files. Just run the following command to copy it to authorized_keys, this command will also create new authorized_keys when available.

cat private_key_for_jenkins.pub> authorized_keys

Now log into the Jenkins server and then go to Manage Jenkins -> Configure Systems

We can write an alternative directly in the web browser http: // ip_address: 8080 / configure, this opens Configure Systems for ssh configurations.

Now look for the SSH Server Server section in “Configure Jenkins”. Please note that this section is only available if we have installed the 'SSH Credential Plugin' on the Jenkins server.

Go to the Publish via SSH section, go to the key and paste the contents of private_key_for_jenkins

Now click the Add button in the SSH Server option and enter the SSH server details. We can change the ssh port and other settings through the Advanced option.

After making the changes click on 'Test Configuration' and we should get a success message like the screenshot above if all the settings are correct.

Please note that we can add multiple servers as is our requirement for publishing the same code on multiple servers using ssh.

To do this, as mentioned earlier, we need to generate the private key for each server and add the private key to the Jenkins server.

Finally, click the 'Save' button and save our changes. We have successfully established an ssh connection to a remote server. Now we can send the file to a remote server and also run any command or script on that server.

Jenkins configuration with Github

To automate builds triggered by PUSH and PULL requests, a Jenkins WebHook must be added to each GitHub repository or webhooks in the organization.

Steps to configure webhook from the Github repository.

1. Sign in to your GitHub accounts

2. Click on the repository where we need to set the webhook

3. Go to the settings for this repository

4. Click on Webhooks and add the payload url, it looks like this http: // jenkins_ip: 8080 / github-webhook /

5. Select "application / json" as the encoding type from the Content type area

6. Leave "Secret" blank (unless a secret has been created and configured in the Jenkins "Configure System -> GitHub plugin" section)

7. Choose Select Individual Events.

8. Activate the PUSH and Pull Request events

9. Make sure that “Active” is checked

10. Finally, click the “Add Webhook” button.

See the screenshot below for details.

Jenkins will now receive push and pull notifications for this repository and related builds will be triggered automatically.

Create the first job on Jenkins

Now go to our Jenkins dashboard, select New Item and enter an item name, select Freestyle Project.

After clicking ok you will be redirected to the configuration section.

We are currently on the general tab as shown in the screenshot below. Typically, you enter the description of the project and click Github Project, this is where you enter our GitHub URL for the project.

Now go to source control, click on Git and enter our repository URL from which we want to take the codes.

We're already adding GitHub credentials, click the drop-down menu and select GitHub username and password.

Next go to Build Branches and choose * / Master Branch, we can add any branch as per our project needs.

Now go to the Build Trigger tab and select the Github hook trigger for GITScm polling option, this will be automatic
created as soon as we make changes to the GitHub repository.

Now go to the build environment and choose Send Files or Run Commands via SSH after the build is done.

This will send files to a remote server after they are built.

Go to SSH Publishers -> SSH Server -> choose the server name from the drop-down menu

Here we can also set the Advanced parameter, but for now we are going with the basic settings.

Now we have to add what kind of file we want to copy via ssh. Here we select all the folders and files from our GitHub repository. To add files and select folders, just add ** / * in the source files.

Similarly, we can enter any command in the Exec Command text box that will be executed on our remote server.

Then specify the remote directory where we need to copy our files. This is an optional option.

SSH Publisher -> Transfers -> Set Transfers -> Source Files -> Add ** / *

SSH Publisher -> Transfers -> Set transfers -> Remote directory -> / jenkins-test

SSH Publisher -> Transfers -> Set Transfers -> Execute Command -> Command

By default, Jenkins excludes certain files like .git .svn .gitignore etc. Here we want to copy all files to the remote server. To do this, click the Advanced button directly under the Execute command and activate the “No standard exclusions” option.

That's it, we have many options in the post-build actions that we can use depending on the project needs.

Build test

It's time to test our configuration.

Go to the Jenkins dashboard and then click on our Jenkins test project.

Then click on the "Build Now" option for the build. If the build process failed, the build version number is shown with a yellow icon.

Let's review our build process, just click the build number as shown in the screenshot.

Now click on Console Output. Here we can see each process in verbose mode.

Finally we will "Finished" see: SUCCESS "line at the end of the console edition.

Congratulations! Our Jenkins have been successfully integrated into GitHub and are now ready for use.