It would be nice for our Jenkins CI server to automatically detect, deploy and build tags as they are created in our Github repository.
Is this possible?
You can install a post-receive hook, which checks if a tag was commited, and creates a build in jenkins.
The hook can look something like this [*]:
#!/usr/bin/env python
import sys
from subprocess import Popen, PIPE, check_call
def call_git(command, args):
return Popen(['git', command] + args, stdout=PIPE).communicate()[0]
JENKINS = 'http://localhost:8002/jenkins'
TOKEN = 'asdf8saffwedssdf'
jobname = 'project-tag'
def handle_ref(old, new, ref):
print 'handle_ref(%s, %s, %s)' % (old, new, ref)
if not ref.startswith('refs/tags/'):
return
url = '%s/job/%s/buildWithParameters?token=%s&branch=%s' % (
JENKINS, jobname, TOKEN, new)
print "queueing jenkins job " + jobname + " for " + new
check_call(["wget", "-O/dev/null", "--quiet", url])
if __name__ == '__main__':
for line in sys.stdin:
handle_ref(*line.split())
[*] note: this is just a quick conversion from a slightly different script, so it's quite probable that there are some small bugs here. This is mostly to show the idea.
On the jenkins side, you need to configure a parametrized job. The only parameter is 'branch'.
This gives a fairly secure and robust way to build. To test, run a build through the web interface, it'll ask for the value of the parameter.