How to trigger a build only if changes happen on particular set of files

后端 未结 8 1614
后悔当初
后悔当初 2020-11-28 20:01

How do I tell Jenkins/Hudson to trigger a build only for changes on a particular project in my Git tree?

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 20:27

    I answered this question in another post:

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

    #!/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;
    

    You can add the check directly to the top of the job's exec shell, and it will exit 0 if no changes are detected... Hence, you can always poll the top level for check-in's to trigger a build.

提交回复
热议问题