With the latest version of the Jenkins and Git plug-in this
is quite easily doable using the notifyCommit feature of the Git Plug-in but if someone is stuck with the older version
of Hudson as in our case there are the workarounds.
Let's walk though them step by step.
Step 1.
In the Hudson job, check option Trigger builds remotely
(e.g., from scripts) in the Build Triggers
This URL can trigger the build remotely
Use the following URL to trigger build remotely:
HUDSON_URL/job/JOB_NAME/build?token=TOKEN_NAME
or /buildWithParameters?token=TOKEN_NAME
or /buildWithParameters?token=TOKEN_NAME
If your Hudson server requires authentication before
triggering the build then you need to provide the user name and password with
this URL , Hudson provides feature of using BASIC auth
Step 2.
Modify post update hook under your git repository
Location of hook <yourrepos.git>/hooks/post-update
This curl command will trigger the Hudson build
curl --user
<hudsonuser>:<password> ‘HUDSON_URL/job/JOB_NAME/build?token=TOKEN_NAME
'
e.g. this can be
something like
curl --user myuser:mypass
'http://myhudson:8080/job/Git_trigger_job/build?token=authtoken'
|
Step 3.
Customize the post update hook to trigger the build only
when changes are pushed to a specific branch.
#!/bin/bash
#
# An example hook script that is called after a successful
# upadte is made.
#
# To enable this hook, rename this file to "post-update".
echo in "post update hook"
#next line fetches the branch name which got updated
branch=$(git rev-parse --symbolic --abbrev-ref $1)
echo Update pushed to branch $branch
if [ $branch == "dev" ]; then
echo "dev branch
updated"
curl --user myuser:mypass
'http://myhudson:8080/job/Git_trigger_job/build?token=authtoken'
elif [ $branch ==
"test" ]; then
echo "test script
called "
# place your code to call
any job when code pushed to the test branch
elif [ $branch ==
"production" ]; then
echo "production
script"
# place your code to call
any job when code pushed to the production branch
fi
exec git update-server-info
|
Step 4 .
Testing it out
Testing it out
This is the output shown in git console whenever the branch is updated /pushed
git push --progress origin dev
Counting objects: 19, done.
Compressing objects: 100%
(9/9)
Writing objects: 100%
(10/10)
Writing objects: 100%
(10/10), 838 bytes, done.
Total 10 (delta 3), reused
0 (delta 0)
remote: in post update hook
remote: Update pushed to
branch dev
remote: dev
remote: % Total
% Received % Xferd Average
Speed Time Time
Time Current
remote: 0
0 0 0
0 0 0
0 --:--:-- --:--:-- --:--:--
0
remote: 0
0 0 0
0 0 0
0 --:--:-- --:--:-- --:--:--
0
To
git@mygit:repos/myrepos.git
a5b64bb..497b512 dev -> dev
Success
|
And this will trigger the Hudson job
Comments
Post a Comment