Check Jenkins job status after triggering a build remotely

前端 未结 4 833
夕颜
夕颜 2020-12-10 02:39

I have a script to trigger a job on Jenkins remotely using a token. Here is my script:

JENKINS_URL=\'http://jenkins.myserver.com/jenkins\'
JOB_NAME=\'job/uti         


        
4条回答
  •  猫巷女王i
    2020-12-10 03:01

    I had similar problem to get state with rest api only.
    this was my solution (it is a weak and not stable solution!):

    #Ex. http://jenkins.com/job/test
    JOB_URL="${JENKINS_URL}/job/${JOB_NAME}"
    
    #here you can ask for next build job number
    function getNextBuildNr(){
      curl --silent ${JOB_URL}/api/json | grep -Po '"nextBuildNumber":\K\d+'
    }    
    
    # this will request the Status of job
    function getBuildState(){
      buildNr=$1
      curl --silent ${JOB_URL}/${buildNr}/api/json | grep -Po '"result":\s*"\K\w+'
    }
    
    #this will wait for your Job state, by polling Jenkins every second
    function waitForJob() {
      buildNr=$1
      state=""
    
      while [ "${state}" == "" ]
      do
         sleep 1
         state=$(getBuildState ${buildNr})
         echo -n '.'
      done
    
      echo -e "\n"
    }
    
    #now you can run and build
    BUILD_NR=$(getNextBuildNr)
    
    # input here your code/function to trigger the job
    
    waitForJob ${BUILD_NR}
    BUILD_STATE=$(getBuildState ${BUILD_NR})
    echo "$BUILD_STATE"
    

提交回复
热议问题