Is it possible for Jenkins to automatically detect and build newly created tags in a git repo?

前端 未结 5 1257
情深已故
情深已故 2020-12-07 12:59

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?

5条回答
  •  我在风中等你
    2020-12-07 13:11

    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'.

    1. Check 'this build is parametrized' and add the parameter
    2. In 'source code management -> branches to build' put '$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.

提交回复
热议问题