Breaking News
Home / How To / How to install Jenkins on CentOS and connect it to GitHub

How to install Jenkins on CentOS and connect it to GitHub

Jenkins often provides open source CI / CD tools. In this article we will teach you how to install Jenkins and configure it with Github to send files to the server with SSH.

Jenkins Installation:

Jenkins is a JAVA-based application, first of all we have to install JAVA. The following command will install JAVA at centos 7.

  sudo yum install java-1.8.0-openjdk-devel 

Verify the 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.21
2-b04, mixed mode)

Now configure Jenkins Repository with the following commands.

  curl --silent --location | sudo tee /etc/yum.repos.d/jenkins.repo

This shows the following output.

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

Then run the following command to add Jenkins key.

  sudo rpm --import .key 

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

  sudo yum install jenkins 

After installation, check Jenkins service and activate it on system restart.


  sudo systemctl enable jenkins 

Now allows Jenkins in the firewall.


  sudo systemctl enable jenkins 

Restart the system and make sure that jenkins service is running with the following command. -cmd --permanent --add port = 8080 / tcp
firewall cmd reload

Configure settings:

We cannot use Jenkins dir ectly, we must enter the domain name or IP address in the browser followed by Jenkins standard port number 8080.

  Unlock Jenkins

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

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

This yields results as 2179c452db3b45f98cf9e4c4961ab635

After the password, we need to install plugins to the Jenkins system. For that, just select the installed suggested plug-in to install all the default settings.

  Create Admin users

Fill in all the details and click Save and Continue.

Now it will ask for Instance Configuration, where we can change the default port number

 Configure Jenkins Instance

Now our Jenkins stores and are ready to use.

 Jenkins is ready

Congratulations, new Jenkins setup successfully completed and ready to be used.

The following screenshot shows the standard dashboard for Jenkins.

 Jenkins Dashboard

Jenkins Configuration:

Install Plugins:

Before going to the next step, be sure to install the following plugins. [19659030] Go to Manage Jenkins -> Manage Plugins -> Available -> Filters -> Search for the following plugin needed for integration with GitHub.

Publish over SSH
GitHub plugin
] Git client plugin [19659030] Here you publish the SSH plugin needed to drive our files to the server over SSH.

Add credentials:

We need to add server information to Jenkins.

Go to Jenkins Dashboard ] Credentials → system → global references (unlimited) → Add references

 Add references

We can choose which type of references we want to install. Here we add GitHub credential username and password for later use.

 Enter GitHub Credentials

ssh on Jenkins Server.

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

To this end, we must create a ssh connection between Jenkin's server and remote server.

The next step adds SSH references to Jenkins that we want to connect with the Jenkins server.

We want to connect to a web server from Jenkins with user root. For example, our web server is 138,197,151,144 and user root, so we must log in to that server with ssh.

  ssh [email protected] 

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

  cd ~ / .ssh 

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

  ssh-keygen -t rsa -f private_key_for_jenkins 

After running the command, it will ask "Enter password phrase", we do not need to enter the password phrase. Then click on enter twice and it will generate two keys on the private_key_for_jenkins web server and

Here private_key_for_jenkins is the private key to 138,197,151,144 and we must use the contents of this file in our Jenkins server. 19659030] We have the second file named which is the public key on the web server. We need to copy data to another file called authorised_keys that is in the same directory.

In most cases, authorized_keys files are presented in ~ / .ssh / folder but sometimes missing and we need to create new authorised_keys files.

  cat> authorized_keys 

Sign in now at the Jenkins server and go to Manage Jenkins -> Configure Systems [19659030] An alternative we can directly type http: // ip_address: 8080 / configure in the browser, opens this configure systems for ssh configurations.

Look for the SSH servers Server section in "Configuring Jenkins". Please note that this section is only available if we have the "SSH Credential Plugin" installed on the Jenkins server.

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

Now click on the SSH Servers option Add the button and enter the SSH Server details. We can change ssh port and other settings from advanced options.

 SSH Server details

Once you have made the changes, click on "Test Configuration" and we will get a success message as shown in the above screenshot if all the settings are correct.

Please note that we can add multiple servers according to our requirement to publish the same code on several servers with ssh.

For that we need to generate the private key for every server we mention earlier and need to add their private key to the Jenkins server.

Finally, click on the "Save" button and save our changes. We have successfully created a ssh connection to a remote server. Now we can send the file to the remote server and also be able to execute any command or script on that server.

Jenkins Configuration with Github

For automation of buildings triggered by PUSH and PULL inquiry, a Jenkins WebHook must be added to each GitHub archive or organization web site.

Steps to configure webhost from the Github archive.

first Log in to your GitHub accounts

2. Click on the repository we need to enter webhook

3. go to setting options for this repository

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

5. Select "application / json" as the content type coding type

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

7. Select "Let me choose individual events"

8. Enable PUSH and Pull Request event

9. Make sure "Active" is selected


 Add Webhook

 Webhook settings

Jenkins will now get the print and click the Add webhook button.

Creating the First Job at Jenkins

Now go to our Jenkin's dashboard select New Item then Enter an Object Name select Freestyle Project .

 Add a job to Jenkins

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

We are on the General tab right now, as shown in the picture below. In general, the tab gives the description of the project and clicks on the Github project. Enter our GitHub URL for the project here.

 General Settings

Now go to Source Code Management and then click on Git

We add GitHub permission, click the drop-down menu and select GitHub user name and password.

Next, go to Branches to build and select * / master file "width =" 550 "height =" 357 "style =" display: block; margin-left: auto; margin-right: auto; "/>

Now go to the Build Trigger tab and select Github hook trigger for the GITScm polling option build when we make some changes to the GitHub repository.

 Build triggers [19659031] Now go to Build environment and select Send files or run commands over SSH after the building runs.

This sends files to the remote server after building runs

Go to SS H Publishers -> SSH Server -> select server name from the drop-down menu [19659031] Here we can set Advanced parameter as well but at the moment we go with basic settings.

Now we have to add what type of file we want to copy over ssh, here we select all folders and files in our GitHub archive. To add files and folder ranges, just add ** / * to source files.

Similarly, we can give the command in the Exec command text, which will run on our remote server. The directory where we have to copy our files is an optional option.

SSH Publishers -> Transfers -> Transfers Set -> Source Files -> add ** / *

SSH Publishers -> Transfers -> Transfers Set -> Remote Catalog -> / jenkins test

SSH Publishers - > Transfers -> Transfers Set -> Exec command -> command

 Build environment

As usual, Jenkins will exclude certain files like .git .svn .gitignore etc. Here we want to copy all files to the remote server. To click the Advanced button just below the Exec command and check the "No standard exclude" option.

 Environmental Settings

That's how we have many options in Post-build actions that we can use

Building Sample

It's time to test our configuration.

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

 Building test [19659030] Then click on the Build Now option for the building. If the construction process failed, it will display the build version number with a yellow icon.

Let us verify our construction process, just click on the construction number shown on the screen dump. [19659030]   GitHub Jenkins Integration was successfully tested

Now click on Console Output. Here we see all processes in verbal mode.

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

Congratulations! our Jenkins successfully integrated with GitHub now and ready to use.

Source link