How can I make Jenkins CI with Git trigger on pushes to master?
I'm trying to set up Jenkins-ci for a project using GitHub. I've already set up Jenkins with the appropriate plugins. I want Jenkins to run build scripts only whenever someone on the project pushes to master. So far I've been able to set it up so that a build will be triggered anytime anyone pushes to anywhere, but that is too broad. I've done this with post-receive service hooks on Git.
I've read the Jenkins wiki, and a couple of tutorials, but this particular detail is missing... is it something t开发者_C百科o do with polling maybe? Or should work be done on the Git side, so that Git only triggers Jenkins when master
is changed?
As already noted by gezzed in his comment, meanwhile there is a good solution (described in Polling must die: triggering Jenkins builds from a Git hook):
Set the Jenkins job's build trigger to Poll SCM, but do not specify a schedule.
Create a GitHub post-receive trigger to notify the URL
http://yourserver/jenkins/git/notifyCommit?url=<URL of the Git repository>?token=<get token from git to build remotely>
This will trigger all builds that poll the specified Git repository.
However, polling actually checks whether anything has been pushed to the used branch.
It works perfectly.
As of version 0.5, the GitHub plugin for Jenkins can trigger a build when a change is pushed to GitHub.
Instead of triggering builds remotely, change your Jenkins project configuration to trigger builds by polling.
Jenkins can poll based on a fixed internal, or by a URL. The latter is what you want to skip builds if there are not changes for that branch. The exact details are in the documentation. Essentially you just need to check the "Poll SCM" option, leave the schedule section blank, and set a remote URL to hit JENKINS_URL/job/name/polling.
One gotcha if you have a secured Jenkins environment is unlike /build
, the /polling
URL requires authentication. The instructions here have details. For example, I have a GitHub Post-Receive hook going to username:apiToken@JENKIS_URL/job/name/polling
.
For GitLab, use these steps:
- Go to your project's settings → Web hooks
Enter the "Build Now" URL from your Jenkins project as a Push Event URL:
http://server.com/jenkins/job/project_name/build?delay=0sec
for example- Click
Add Web Hook
and thentest hook
Then any time you commit to the repository, the web hook is triggered and a build is created. Be sure to set your Jenkins workspace to delete workspace before each build
so you get a fresh copy of the new code.
Not related to Git, but below I will help with the Jenkins job configuration in detail with Mercurial. It may help others with a similar problem.
- Install the URL Trigger Plugin
- Go to the job configuration page and select
Poll SCM
option. Set the value to* * * * *
- Check the option:
[URLTrigger] - Poll with a URL
. Now you can select some options like modification date change, URL content, etc. - In the options, select URL content change, select first option –
Monitor change of content
- Save the changes.
Now, trigger some change to the Mercurial repository by some test check-ins.
See that the Jenkins job now runs by detecting the SCM changes. When the build is run due to Mercurial changes, then, you will see text Started by an SCM change
. Else, the user who manually started it.
I hope this helps: How to trigger a Jenkins build on Git commit
It's just a matter of using curl to trigger a Jenkins job using the Git hooks provided by Git.
The command curl http://localhost:8080/job/someJob/build?delay=0sec
can run a Jenkins job, where someJob
is the name of the Jenkins job.
Search for the "hooks" folder in your hidden .git folder. Rename the "post-commit.sample" file to "post-commit". Open it with Notepad, remove the ": Nothing" line and paste the above command into it.
That's it. Whenever you do a commit, Git will trigger the post-commit commands defined in the file.
Continuous Integration with Jenkins, after code is pushed to repository from Git command/ GUI:
- Create a job in Jenkins with only job name and select type of the project freestyle. Click OK. The next page doesn't add anything - just click Save.
- Go to the your local Git repository where you have the source code and navigate to the
.git/hooks
folder. The
hooks
folder contains the few files. Check for the "post-commit". If not present, create a file, "post-commit" without a file extension:C:\work\test\\.git\hooks\post-commit
Edit the "post-commit" file with the below command. Make sure it is present in your local source code hooks folder.
curl -u userName:apiToken -X POST http://localhost:8080/jenkins/job/jobName/build?token=apiToken
Example:
curl -u admin:f1c55b3a07bb2b69b9dd549e96898384 -X POST http://localhost:8080/jenkins/job/Gitcommittest/build?token=f1c55b3a07bb2b69b9dd549e96898384
5.
userName
: Jenkins user namejobName
: Job name of the buildapiToken
: To get your API token, go to your Jenkins user page (top right in the interface). It is available in the "Configure" menu on the left of the page: "Show API token"Make changes in your source code and commit the code to repository.
Your job,
http://localhost:8080/jenkins/job/Gitcommittest/
, should be building.
You need to specify the branch. By default it listens to anything. See the blog post Hudson: Git and Maven plugins.
Generic Webhook Trigger Plugin can be configured with filters to achieve this.
When configured with
- A variable named
ref
and expression$.ref
. - A filter with text
$ref
and filter expression like^refs/heads/master$
.
Then that job will trigger for every push to master
. No polling.
You probably want more values from the webhook to actually perform the build. Just add more variables, with JSONPath, to pick what you need.
There are some use cases here: https://github.com/jenkinsci/generic-webhook-trigger-plugin/tree/master/src/test/resources/org/jenkinsci/plugins/gwt/bdd
In my current organization, we don't do this in master but do do it on both develop and release/ branches (we are using Git Flow), in order to generate snapshot builds.
As we are using a multi branch pipeline, we do this in the Jenkinsfile with the when{} syntax...
stage {
when {
expression {
branch 'develop'
}
}
}
This is detailed in this blog post: https://jenkins.io/blog/2017/01/19/converting-conditional-to-pipeline/#longer-pipeline
Above answers are correct but i am addressing to them who are newbie here for their simplicity
especially for setting build trigger for pipeline:
Consider you have two Github branches: 1.master, 2.dev, and Jenkinsfile (where pipeline script is written) and other files are available on each branch
Configure new Pipeline project (for dev branch)
##1.Code integration with git-plugin and cron based approach Prerequisite git plugin should be installed and configure it with your name and email
- General section.Check checkbox - 'This project is parameterized’ and add Name-SBRANCH Default Value-'refs/remotes/origin/dev'
- Build triggers section" Check checkbox - 'Poll SCM' and schedule as per need for checking commits e.g. '*/1 * * * *' to check every minute
- Pipeline definition section.Select - Pipeline script from SCM—> select git—> addRepository URL—>add git credentials—> choose advanced—> add Name- origin, RefSpec- '+refs/heads/dev:refs/remotes/origin/dev'(dev is github branch )—> Branches to build - ${SBRANCH} (Parameter name from ref 1st point)—> Script Path—> Jenkinsfile —> Uncheck Lightweightcheckout
- Apply—> save
##2.Code integration: github-plugin and webhook approach Prerequisite Github plugin should be installed and Github server should be configured, connection should be tested if not consider following configuration
Configure Github plugin with account on Jenkins
GitHub section Add Github server if not present API URL: https://api.github.com Credentials: Add secret text (Click add button: select type secret text) with value Personal Access Token (Generate it from your Github accounts—> settings—> developer setting—> personal access token—> add token—> check scopes—> copy the token) Test Connection—> Check whether it is connected to your Github account or not Check checkbox with Manage Hooks In advance sub-section just select previous credential for 'shared secret'
Add webhook if not added to your repository by
- Go to Github Repository setting —> add webhook—> add URL
http://Public_IP:Jenkins_PORT/github-webhook/ - Or if you don't have Public_IP use ngrok. Install, authenticate, get public IP from command ./ngrok http 80(use your jenkins_port) then add webhook —> add URL http://Ngrok_IP/github-webhook/
- Test it by delivering payload from webhook page and check whether you get 200 status or not.
If you have Github Pull requests plugin configure it also with published Jenkins URL.
- General section.Check checkbox - 'Github project' add project URL -(github link ending with '.git/')
- General section.Check checkbox - 'This project is parameterized' and add Name-SBRANCH Default Value-'refs/remotes/origin/dev'
- Build triggers.section.Check checkbox - 'GitHub hook trigger for GITScm polling'
- Pipeline def'n section: Select - Pipeline script from SCM—> select git—> addRepository URL—> add git credentials—>choose advanced —>add Name- origin, RefSpec- '+refs/heads/dev:refs/remotes/origin/dev' (dev is github branch ) —> Branches to build - ${SBRANCH} (Parameter name from ref 1.st point)—> Script Path—> Jenkinsfile—> Uncheck Lightweightcheckout
- Apply—> save
Use the pull request builder plugin: https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin
It's really straightforward. You can then setup GitHub webhooks to trigger builds.
My solution for a local git server: go to your local git server hook directory, ignore the existing update.sample and create a new file literally named as "update", such as:
gituser@me:~/project.git/hooks$ pwd
/home/gituser/project.git/hooks
gituser@me:~/project.git/hooks$ cat update
#!/bin/sh
echo "XXX from update file"
curl -u admin:11f778f9f2c4d1e237d60f479974e3dae9 -X POST http://localhost:8080/job/job4_pullsrc_buildcontainer/build?token=11f778f9f2c4d1e237d60f479974e3dae9
exit 0
gituser@me:~/project.git/hooks$
The echo statement will be displayed under your git push result, token can be taken from your jenkins job configuration, browse to find it. If the file "update" is not called, try some other files with the same name without extension "sample".
That's all you need
Manage Jenkins/ configure system /GitHub Servers
On jenkins job / git credentials and Branch Specifier (give the branch you want to look for pushes)
- Webhook on github
I also had same problem. And this saved me
To filter according to the payload 'ref' tag, optional filter is set in jenkins pipeline job.
Then applied optional filter on 'ref' variable, to allow only master branch
or expression could be simply
refs/heads/master
Now my jenkins job is triggering only when there is a commit to master
精彩评论