Wednesday, February 27, 2013

Jenkins Installation w/ Git Support in CentOS 6


Jenkins Installation w/ Git Support in CentOS 6

Installation
First, we need to set up the Jenkins repository for CentOS.
# sudo wget –O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
# sudo yum install –y jenkins
Make sure git is installed and check the path:
# which git
Output: /usr/bin/git
If no output, do a:
# sudo yum install git

Start/Stop Service
Start Jenkins service by issuing:
# sudo service jenkins start
Enable Jenkins to start at boot by:
# sudo chkconfig jenkins on

Logging in to Jenkins Web UI and Install Git and Github Plugins
Access jenkins web interface by pointing your browser to:
Where “localmachine” is the ip address or hostname of the machine you installed jenkins on.
Next, click on “Manage Jenkins” on the left hand column, and on the next menu, “Manage Plugins”.
Click the “Available” tab and check the boxes next to “Git Plugin” and “Github Plugin”, then click “Download now and install after restart” at the bottom of the page.
Once the plugins are installed, check the box that says “Restart Jenkins when installation is complete and no jobs are running”.  Jenkins will now restart and the plugins will be installed.

Configure Security
Click on “Manage Jenkins” and on the next menu, “Configure Global Security”.
Check “Enable security”, and under “Security Realm”, click the radio button next to “Jenkins’s own user database”.  Under “Authorization”, click on “Matrix-based security”.
Add a user named “administrator” (or whatever you like) and once the user appears, check *all* the boxes under *all* the categories.
Click “Save” in the bottom left, and you will be taken to a sign up page.  Enter “administrator” under the username field, and fill in the rest of the fields.  You will then be logged in as administrator and have full control over Jenkins.

Giving Jenkins user Access to Github
First, we need to temporarily give the Jenkins user shell access so we can generate SSH keys in order to access github.
# sudo vim /etc/passwd
Change the following line from:
jenkins:x:496:492:Jenkins Continuous Build server:/var/lib/jenkins:/bin/false
To:
jenkins:x:496:492:Jenkins Continuous Build server:/var/lib/jenkins:/bin/bash
Save and close the file.
Then switch to jenkins user:
#sudo su - jenkins
At the bash shell, issue:
# ssh-keygen
Hit enter for all the options.
Once the keys are generated and you’re back at the bash prompt, type “exit”.
Next, we have to rename the key files by doing the following:
# sudo cp /var/lib/jenkins/.ssh/id_rsa.pub /var/lib/jenkins/.ssh/authorized_keys
Now we need to copy the key.
# sudo cat /var/lib/jenkins/.ssh/authorized_keys
Copy the entire output and add to the SSH Keys section of your github user profile, giving it a name such as “Jenkins ssh key”.

Lastly, we have to connect to the github repo that we’re going to use in order to accept github into Jenkins’ known_hosts file.  Do this by issuing:
# sudo su – jenkins
# /usr/bin/git ls-remote -h git@github.com:starscriber/MessageBroker.git HEAD
When a message comes up asking you to continue, type “yes”.
If it is successful, you should go back to the bash prompt after a few seconds.  You can now type “exit”.
Now edit the /etc/passwd file again, and change the jenkins user’s shell back to /bin/false.

Adding Github repo to Jenkins Web UI
Click “Manage Jenkins” and then “Configure System”.
Click on “Git installations” and change “Path to Git executable” to full git path from step 1: /usr/bin/git
Make sure “Manually manage hook URLs” at the bottom of the page is checked.
Click on “Save”.
Next, select “New Job”.
Put something under “Job name” ie: MyCompany_MyApplication, and select “Build a free-style software project”, and click OK.
On the next screen, enter the “GitHub project” URL, ie: https://github.com/mycompany/MyApplication/
Under “Source Code Management”, select “Git” and enter the SSH repository URL, ie: git@github.com:mycompany/MyApplication.git
Under “Build Triggers”, check the box next to “Build when a change is pushed to GitHub”.
Save.

Test Everything
Go back to Jenkins main page, and you should see a list of your Jobs.  If you hover your mouse on the Job Name, ie: Starscriber MessageBroker, you should get a dropdown list.  Click on “Build Now” and if successful, everything is working properly.