I have a Jenkins job (multibranch pipeline
) setup for a GitHub repo. I want to trigger that job automatically whenever there is a change pushed to that Git repo. How can I achieve this using a Jenkinsfile
?
I want to avoid any config change in the Jenkins job such as Poll SCM
etc. For multibranch pipeline there is no such option as Build whenever a change is pushed to GitHub
Go to Manage Jenkins -> Global Tool Configuration -> Git Add git executable path to Global Tool Configuration. Let us start with creating a Freestyle Project : Step 1: Go to New Item -> create a freestyle project. Step 2: Go to Configure, add a project description, and Github project URL.
Follow the steps as mentioned below to trigger a Jenkins job automatically based on GitHub's webhook configurations: Step 1: Go to the Configuration page of the respective job and under the build trigger section, check the "GitHub hook trigger for GITScm polling" checkbox and click on the Save button.
The easiest option by far (that I'm aware of) is to remotely tell the Jenkins Git plugin that there's a new commit for a defined repository. However, this will not trigger Jenkins to start a job immediately. What happens is that the Git plugin starts (re-)indexing the specific repository. If changes are detected the Jenkins job is then started.
From your repository (GitHub, GitLab, etc.) you should trigger the following URL:
http://my-jenkins-host/git/[email protected]:group/repository.git&delay=0sec
The value for url
must match the SCM URL you configured in the Jenkins job (Git plugin)!
Gotcha: it may be that your Jenkins is not deployed under the root context (/
) in which case the URL would be http://my-jenkins-host/context-path/git/...
For your job "reacting" to events from your GitHub, set the job build trigger to "Poll SCM", but do not specify a schedule, as described on this Stackoverflow answer.
For declarative Jenkinsfile, this means:
pipeline {
triggers {
pollSCM('') // Enabling being build on Push
}
...
}
Further readings:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With