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

后端 未结 11 1940
时光取名叫无心
时光取名叫无心 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:14

    Through Groovy:

    <!-- CHANGE SET -->
    <% changeSet = build.changeSet
    if (changeSet != null) {
    hadChanges = false %>
    <h2>Changes</h2>
    <ul>
    <% changeSet.each { cs ->
    hadChanges = true
    aUser = cs.author %>
    <li>Commit <b>${cs.revision}</b> by <b><%= aUser != null ? aUser.displayName :      it.author.displayName %>:</b> (${cs.msg})
    <ul>
    <% cs.affectedFiles.each { %>
    <li class="change-${it.editType.name}"><b>${it.editType.name}</b>: ${it.path}                              </li> <%  } %> </ul>   </li> <%  }
    
     if (!hadChanges) { %>  
      <li>No Changes !!</li>
     <%  } %>   </ul> <% } %>
    
    0 讨论(0)
  • 2020-11-28 10:17
    echo $SVN_REVISION
    svn_last_successful_build_revision=`curl $JOB_URL'lastSuccessfulBuild/api/json' | python -c 'import json,sys;obj=json.loads(sys.stdin.read());print obj["'"changeSet"'"]["'"revisions"'"][0]["'"revision"'"]'`
    diff=`svn di -r$SVN_REVISION:$svn_last_successful_build_revision --summarize`
    
    0 讨论(0)
  • 2020-11-28 10:20
    #!/bin/bash
    
    set -e
    
    job_name="whatever"
    JOB_URL="http://myserver:8080/job/${job_name}/"
    FILTER_PATH="path/to/folder/to/monitor"
    
    python_func="import json, sys
    obj = json.loads(sys.stdin.read())
    ch_list = obj['changeSet']['items']
    _list = [ j['affectedPaths'] for j in ch_list ]
    for outer in _list:
      for inner in outer:
        print inner
    "
    
    _affected_files=`curl --silent ${JOB_URL}${BUILD_NUMBER}'/api/json' | python -c "$python_func"`
    
    if [ -z "`echo \"$_affected_files\" | grep \"${FILTER_PATH}\"`" ]; then
      echo "[INFO] no changes detected in ${FILTER_PATH}"
      exit 0
    else
      echo "[INFO] changed files detected: "
      for a_file in `echo "$_affected_files" | grep "${FILTER_PATH}"`; do
        echo "    $a_file"
      done;
    fi;
    

    It is slightly different - I needed a script for Git on a particular folder... So, I wrote a check based on jollychang.

    It can be added directly to the job's exec shell script. If no files are detected it will exit 0, i.e. SUCCESS... this way you can always trigger on check-ins to the repository, but build when files in the folder of interest change.

    But... If you wanted to build on-demand (i.e. clicking Build Now) with the changed from the last build.. you would change _affected_files to:

    _affected_files=`curl --silent $JOB_URL'lastSuccessfulBuild/api/json' | python -c "$python_func"`
    
    0 讨论(0)
  • 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']
    
    0 讨论(0)
  • 2020-11-28 10:25

    It's simple, but this works for me:

    $DirectoryA = "D:\Jenkins\jobs\projectName\builds"  ####Jenkind directory
    $firstfolder = Get-ChildItem -Path $DirectoryA | Where-Object {$_.PSIsContainer} | Sort-Object LastWriteTime -Descending | Select-Object -First 1
    $DirectoryB = $DirectoryA + "\" + $firstfolder
    
    $sVnLoGfIle = $DirectoryB + "\" + "changelog.xml"
    
    write-host $sVnLoGfIle
    
    0 讨论(0)
  • 2020-11-28 10:28

    I tried to add that to comments but code in comments is no way:

    Just want to prettify code from heroin's answer:

    def changedFiles = []
    def changeLogSets = currentBuild.changeSets
    for (entries in changeLogSets) {
        for (entry in entries) {
            for (file in entry.affectedFiles) {
                echo "Found changed file: ${file.path}"
                changedFiles += "${file.path}"
            }
        }
    }
    

    Keep in mind for some cases git plugin returns empty changeSet, like:

    • First run in newly created branch
    • 'Build now' button build

    Refer to https://issues.jenkins-ci.org/browse/JENKINS-26354 for more details.

    0 讨论(0)
提交回复
热议问题