How to get list of changed files since last build in Jenkins/Hudson

后端 未结 11 1977
时光取名叫无心
时光取名叫无心 2020-11-28 09:35

I have set up Jenkins, but I would like to find out what files were added/changed between the current build and the previous build. I\'d like to run some long running tests

11条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 10:22

    You can use the Jenkins Remote Access API to get a machine-readable description of the current build, including its full change set. The subtlety here is that if you have a 'quiet period' configured, Jenkins may batch multiple commits to the same repository into a single build, so relying on a single revision number is a bit naive.

    I like to keep my Subversion post-commit hooks relatively simple and hand things off to the CI server. To do this, I use wget to trigger the build, something like this...

    /usr/bin/wget --output-document "-" --timeout=2 \
        https://ci.example.com/jenkins/job/JOBID/build?token=MYTOKEN
    

    The job is then configured on the Jenkins side to execute a Python script that leverages the BUILD_URL environment variable and constructs the URL for the API from that. The URL ends up looking like this:

    https://ci.example.com/jenkins/job/JOBID/BUILDID/api/json/
    

    Here's some sample Python code that could be run inside the shell script. I've left out any error handling or HTTP authentication stuff to keep things readable here.

    import os
    import json
    import urllib2
    
    
    # Make the URL 
    build_url = os.environ['BUILD_URL']
    api = build_url + 'api/json/'
    
    # Call the Jenkins server and figured out what changed
    f = urllib2.urlopen(api)
    build = json.loads(f.read())
    change_set = build['changeSet']
    items = change_set['items']
    touched = []
    for item in items:
        touched += item['affectedPaths']
    

提交回复
热议问题