Search code examples
jenkinsjenkins-pipelineartifacts

Jenkins pipeline with Copy Artifact Plugin to copy artifact from last complete build


In declarative Jenkins pipeline, we could copy artifacts from last complete build of a project with below codes:

            def lastBuildId = currentBuild?.getPreviousBuild()?.getId()
            copyArtifacts(projectName: "${JOB_NAME}", 
            selector: specific("${lastBuildId}")
            )

But in plugin instruction, I noticed one of selectors is "lastCompletedLast". but I don't know what is the correct usage of those selectors. It is wrong if I use:

                copyArtifacts(projectName: "${JOB_NAME}", 
                selector: lastCompletedLast())
                )

Is there any documentation?


Solution

  • Using the Snippet Generator (use the Pipeline Syntax link on any Pipeline job) is a good way to find these sorts of things. You fill in the UI, it generates the groovy code.

    According to the generator, the following should work: copyArtifacts projectName: "${JOB_NAME}", selector: lastCompleted()

    EDIT: I originally added the last successful build selector, not per the OP request. Fixed to be lastCompleted()