Reputation: 22365
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 to do with polling maybe? Or should work be done on the Git side, so that Git only triggers Jenkins when master
is changed?
Upvotes: 216
Views: 260987
Reputation: 552
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
Upvotes: 0
Reputation: 5581
Manage Jenkins/ configure system /GitHub Servers
On jenkins job / git credentials and Branch Specifier (give the branch you want to look for pushes)
Upvotes: 0
Reputation: 753
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
Upvotes: 0
Reputation: 61
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
##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
If you have Github Pull requests plugin configure it also with published Jenkins URL.
Upvotes: 1
Reputation: 2729
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
Upvotes: 1
Reputation: 3500
Generic Webhook Trigger Plugin can be configured with filters to achieve this.
When configured with
ref
and expression $.ref
.$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
Upvotes: 3
Reputation: 129526
You need to specify the branch. By default it listens to anything. See the blog post Hudson: Git and Maven plugins.
Upvotes: 3
Reputation: 367
Continuous Integration with Jenkins, after code is pushed to repository from Git command/ GUI:
.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 name
jobName
: Job name of the build
apiToken
: 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.
Upvotes: 4
Reputation: 4210
For GitLab, use these steps:
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
Add Web Hook
and then test 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.
Upvotes: 8
Reputation: 20648
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.
Upvotes: 4
Reputation: 51
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.
Poll SCM
option. Set the value to * * * * *
[URLTrigger] - Poll with a URL
.
Now you can select some options like modification date change, URL content, etc.Monitor change of content
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.
Upvotes: 5
Reputation: 91
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
.
Upvotes: 9
Reputation: 11694
As of version 0.5, the GitHub plugin for Jenkins can trigger a build when a change is pushed to GitHub.
Upvotes: 34
Reputation: 577
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.
Upvotes: 199
Reputation: 781
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.
Upvotes: 0