tm1701
tm1701

Reputation: 7581

Jenkins: Git push will trigger a Jenkins build for only that branch

We are working in parallel on a number of Git branches.

When pushing a specific Git branch, how can we start a Jenkins item/job that will trigger the build of that specific branch?

As an example: we push a Git branch "feature-abc" ==> this should trigger a Jenkins build job using/pulling that branch "feature-abc".

For each new feature we have a branch, so the number of branches is not fixed.

So, I think it is different than this answer with a manual action.

Upvotes: 3

Views: 4360

Answers (2)

tm1701
tm1701

Reputation: 7581

A much better alternative for building Jenkins jobs via the GUI is making them as Jenkins pipelines.

The result of the (below) simple demo configuration is that each time you push updates to the Git repo, a Jenkinsfile for that (changed) branch is started. In the Jenkinsfile you can output the branch name.

How? You can easily create such a configuration yourself in a few minutes. In this case I have a Jenkins server running on my local PC. So, this Jenkins server is by default not accessable for a webhook in the Git project. (Notice: you can use Webhook relay to simulate real webhooks). When the Git server can access the Jenkins server, then you should definitely use a webhook.

  • Create a project and put it in a Git(lab) repo.
  • Add this simple Jenkinsfile (pipeline) to your project.

.

node {
    stage('Preparation') {
        checkout scm
    }
    stage('Who am i?') {
        echo "This job was triggered by a Git push to branch: ${env.BRANCH_NAME}"
    }
}
  • Jenkins > New Item > Multibranch pipeline. If your Jenkins server is not accessable for the Git server you can use polling. Otherwise you can setup a webhook via the Git server. enter image description here
  • Jenkins will now scan the Git project for branches having a 'Jenkinsfile'. For each matching branch (having a Jenkinsfile) it will automatically create a Jenkins Pipeline job. The jobs have the name of the branch. At first there will be only 1 branch, so there will be 1 job with the name 'master' created. The newly created jenkins job will execute the 'Jenkinsfile' for the 'master' branch. In the console you will see the output with the name of the branch: master.
  • Create a branch and make a change to the Jenkinsfile. Push the changes to the branch.
  • After waiting max 1 minute (due to the polling) the multibranch job will scan whether there are new branches with a Jenkinsfile. In this case a new jenkins job is created with the name of the branch. All jobs are visible below the Multibranch job. The job will be executed and printout the branch name.
  • If you only make a change to an existing branch AND you push the change to the Git project, then within 1 minute the branches are scanned. Jenkins will see that the branch is changed and start the Jenkins pipeline job.

Upvotes: 1

saurabh14292
saurabh14292

Reputation: 1401

If the use case is to trigger a build for every git push, you can add a a git pre-receive hook, which will trigger the build, once git-push is completed.

#!/bin/bash
while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    #Trigger based on branch name "$branch" or pass this parameter to trigger the build
    curl -X POST -u "user" "http://<jenkins_url>/job/buildWithParameters?branch_name=$branch"
done

Upvotes: 1

Related Questions