How to trigger a build remotely from Jenkins?
How to configure Git post commit hook?
My requirement is whenever changes are made in the Git repository for a particular project it will automatically start Jenkins build for that project.
In Jenkins trigger build section I selected trigger build remotely.
In .git
directory, hooks directory is there in that we have to configure post commit file.
I am confusing how to trigger a build from there (I know some part we should use curl command).
curl cmbuild.aln.com/jenkins/view/project name/job/myproject/buildwithparameters?Branch=feat-con
I have placed this command in my git server hooks directory (post commit hook).
Whenever the changes happen in repository it is running automate build.
I want to check in changeset whether in at least one java file is there the build should start.
Suppose the developers changed only xml files or property files the build should not start.
Along with xml
, suppose the .java
files is there the build should start.
Hope this helps: http://nrecursions.blogspot.in/2014/02/how-to-trigger-jenkins-build-on-git.html
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.
I want to add to the answers above that it becomes a little more difficult if Jenkins authorization is enabled.
After enabling it I got an error message that anonymous user needs read permission.
I saw two possible solutions:
1: Changing my hook to:
curl --user name:passwd -s http://domain?token=whatevertokenuhave
2: setting project based authorization.
The former solutions has the disadvantage that I had to expose my passwd in the hook file. Unacceptable in my case.
The second works for me. In the global auth settings I had to enable Overall>Read for Anonymous user. In the project I wanted to trigger I had to enable Job>Build and Job>Read for Anonymous.
This is still not a perfect solution because now you can see the project in Jenkins without login. There might be an even better solution using the former approach with http login but I haven't figured it out.
As the previous answer did show an example of how the full hook might look like here is the code of my working post-receive hook:
#!/usr/bin/python
import sys
from subprocess import call
if __name__ == '__main__':
for line in sys.stdin.xreadlines():
old, new, ref = line.strip().split(' ')
if ref == 'refs/heads/master':
print "=============================================="
print "Pushing to master. Triggering jenkins. "
print "=============================================="
sys.stdout.flush()
call(["curl", "-sS", "http://jenkinsserver/git/notifyCommit?url=ssh://user@gitserver/var/git/repo.git"])
In this case I trigger jenkins jobs only when pushing to master and not other branches.
Source: Stackoverflow.com