Auto Build of Jenkins Jobs from local GIT

Auto Build of Jenkins Jobs from local GIT

Introduction:  

  We've quite few ways to build the job through Jenkins(manually or automatically), we'll see the other way to trigger the Jenkins job automatically when we commit changes to local git.

Step 1:

Go to Jenkins home directory and create a repo.


                      cd /var/lib/jenkins
                      mkdir repo


Let's initialize this the empty repo using the init command.

                       git init .  => this step would initialize the git repo locally

Create a file under repo and commit the changes.

                         cd /var/lib/jenkins/repo
                         vi file1{
                                         Add Data
                         }:wq!

                       git add file1
                       git commit -m "first commit"

Step 2:


Let's create a Jenkins job, provide local git location under SCM to verify

From your terminal, start the Jenkins server and open the dashboard.

                                       service jenkins start

        URL:  localhost:8080

Go to Jenkins dashboard > New Item > Free Style Job > Ok

Under SCM, provide the local git location as file:///${JENKINS_HOME}/repo

If you don't see any errors after entering the location, we have provided the correct path. If you see any error here at this page, check the correct path and provide it.

In Jenkins we have to disable a permission CSRF Protection under Global Security Tool.

Note: CSRF Protection helps to protect against the class of attacks.

Go to Manage Jenkins > Global Security Tool > Disable CSRF Protection.

Step 3:

 Next step would be adding Build elements. Under Build Trigger column, choose a token and name it. Eg: shivaji

And add the shell command in the Build actions, which you wish to see as a output on the console as a reference.

Step 4:

In order to automate the Jenkins job builds, we have to go for hooks locally. 

Go to hooks under .git file, create a new file post-commit and write a shell script in it.

#!/bin/bash
curl --user 'jenkins username:jenkins password' -X POST "http://localhost:8080/job/job-name/build" --data token=token-name --data delay=0sec

Example script:

#!/bin/bash
curl --user 'admin:admin' -X POST "http://localhost:8080/job/newjob/build" --data token=shivaji --data delay=0sec

Note: Admin & admin are default Jenkins username & password
         newjob - name of the job in Jenkins which you've already created
         shivaji - token which I've created earlier in Build Trigger options

Please make sure that you provide correct details. If you're missing out on Jenkins username & password, you'll see ERROR:401, if it's with token, you'll see ERROR:403.

Once done, give the 755 permissions to post-commit file.

                                        chmod 755 post-commit

Step 5:

 Once the above configurations are done, go to Jenkins home location and append the data in your file and commit >> which would automatically trigger build in your Jenkins.

               cd /var/lib/Jenkins/repo
               vi file1{
                               Appended data  
               }:wq!
   
              git add .
             git commit -m "Second commit"

Check the Jenkins Job build status and console output.

That's All, Cheers!! !! :)




Comments

Popular posts from this blog

Reverse Proxy with Web-servers

Nagios Installation and Monitoring