How to trigger a Jenkins Pipeline on commit to a branch?

18,518

Solution 1

To trigger a Jenkins pipeline with a new commit to Git branch you need to add the web hook on Git repository settings and select Pushes trigger event.

Or if you want Jenkins pipeline to perform some specific step on a specific branch .. you can try adding a condition in your Jenkinsfile like below..

def branchName = "${env.BRANCH_NAME}"
if(branchName == 'dev'){
  ...
}

Solution 2

You need to define a trigger in the repository in case of push for your branch. Additionally, you can poll the scm and define a cron for that.

Do not worry if you have several consecutive pushes in your branch. Jenkins is able to collapse multiple triggers in case of several executions in the queue.

Share:
18,518

Related videos on Youtube

fuzzi
Author by

fuzzi

Updated on June 04, 2022

Comments

  • fuzzi
    fuzzi almost 2 years

    I'm working on triggering a Jenkins Pipeline job when a commit is added to a particular branch on GitHub.

    I do not see an appropriate Build Triggers on the Jenkins job options, like I would for a Jenkins Freestyle job.

    How do I set a Jenkins Pipeline job to be triggered when a branch is updated with a new commit?

    So far, I haven't found explicit answers to this scenario, Only trigger Jenkins Pipeline job on commit to master, only answered regarding Multi-branch jobs.

    Any help on this question would be greatly appreciated!

    • changeme
      changeme over 3 years
      Have you figure out the solution yet? If so, can you please add as solution.
  • fuzzi
    fuzzi over 5 years
    Is there anything settings required on the Jenkins Pipeline job that receives the Push trigger?
  • Sreeni
    Sreeni over 5 years
    On the Jenkins server add the github server as repository. and you can also add the name of the Jenkinsfile you want to use in pipeline build configuration.
  • fuzzi
    fuzzi over 5 years
    Within the Jenkins Pipeline Job, I have added the URL to the "GitHub project" box, and checked the Build Triggers option for "GitHub hook trigger for GITScm polling"