Have Jenkins Run A Shell Script In A Remote Machine Every Time There Is A Change In The Git Repository

From Jenkins home page, click on “New Item” and select a “Freestyle Project” and fill in the general details such as “Project Name and Description”

Under the Source Code Management section, add your bitbucket url and set up the ssh credential so that Jenkins will be able to poll bitbucket after certain intervals.

To set up the ssh credential, generate a pair of public and private RSA key and put the public key in your bitbucket repo

and the private key in your Jenkins Source Code Management section by clicking on the “add” button which opens up a new window as shown below where you add in the ssh credential.

Then, we set up the Build Trigger section where we have Jenkins poll the repository we added in the earlier step. As shown in the figure, I am having it poll every 15 minutes.

Next thing we want to do is to step the Build Environment. In this step, we can set things that we want to do before and after the actual build step. I decided to run my test script before the build and the screenshot below shows the set up.

As you can see, you need to select the ssh server which is the remote machine where you want your script to run. To set up the ssh server, you need to go to the home page of Jenkins and select “Manage Jenkins” and select “Configure Jenkins” and set up your ssh server.

When you come back to this section after you set up your ssh server, you will see the name of your ssh server in the pull-down list. Here you can set up the ssh credential as password or key. I have set it as a password.

Since we are only running a shell script, there is nothing to build and that is the reason why my Build Section is empty.

The last step “Post-Build Section” is optional but you can use it to set up your email notification. But that’s it. Following the above steps will run your bash script every time there is a new code in your bitbucket repository. Since github is also a git repository the steps should be exactly the same I think.

Leave a Reply

Your email address will not be published. Required fields are marked *