Jenkins Copy Artifact parse copied build id

依然范特西╮ 提交于 2019-12-02 17:07:23

问题


I'm using in Jenkins 2.73.1, in a multibranch pipeline Copy Artifact Plugin to get the last successful artifacts from two other pipelines, see my Jenkinsfile:

def branchname = "${BRANCH_NAME}".replace("/", "%2F")
pipeline {
  agent {
    label 'windows'
  }
  stages {
    stage('get artifacts') {
      steps {
        script {
          parallel('get-backend': {
            step([$class: 'CopyArtifact', projectName: "backend/${branchname}", target: 'input/backend'])
          },
          'get-frontend': {
            step([$class: 'CopyArtifact', projectName: "frontend/${branchname}", target: 'input/frontend'])
          })
        }
      }
    }
  }
}

In the build log I see e.g.:

...
[Pipeline] script
[Pipeline] {
[Pipeline] parallel
[Pipeline] [get-backend] { (Branch: get-backend)
[Pipeline] [get-frontend] { (Branch: get-frontend)
[Pipeline] [get-backend] step
[Pipeline] [get-frontend] step
[get-frontend] Copied 344 artifacts from "frontend » foo/bar" build number 17
[Pipeline] [get-frontend] }
[get-backend] Copied 2'287 artifacts from "backend » foo/bar" build number 3
[Pipeline] [get-backend] }
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // script
...

Question / Goal: I'd like to parse and save to a file (to be archived, then loaded and parsed by groovy in a different pipeline) the build number of frontend and backend (in the log see "build number 17" and "build number 3").

In the question Jenkins Pipeline - Reading previous stage log I read it's possible to redirect the standard output of an sh script into a groovy variable, like this:

def out = sh script: 'command', returnStdout: true

But in my pipeline I need to use windows, also the step in this case is not a command but it's a "step" (containing class "CopyArtifact", still new to Jenkins, I just made it work googling around and found some examples using this syntax).

How can I achieve my goal? Should I parse the entire log in this case or is there a more elegant solution? Please provide some code in your answer, so I can test it directly.


回答1:


You could write the build number to a file in the upstream jobs and include that in the archive. Then you copy that artifact forward. You could use the pipeline utility steps plugin to gather information from the file, but if you keep the file simple (i.e. write nothing but the build number), then something like this would get you the build number:

def backendBuildId = sh script: "cat backendBuild.txt", returnStdout: true

or on Windows:

def backendBuildId = bat script: "type backendBuild.txt", returnStdout: true



回答2:


You cannot get the output of a Jenkins step, only the sh step allows it, but in this case you get the output or the inner sh (or bat) command.

However in your case it is pretty simple as you only need the other builds numbers, you can use Jenkins API, as described in this question:

def backendBuildId = sh script: "wget http://yourjenkinsurl/job/backend/${branchname}/lastSuccessfulBuild", returnStdout: true
def frontendBuildId = sh script: "wget http://yourjenkinsurl/job/frontend/${branchname}/lastSuccessfulBuild", returnStdout: true

For more details, please check the API details in your Jenkins.



来源:https://stackoverflow.com/questions/46601108/jenkins-copy-artifact-parse-copied-build-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!