问题
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