Dmitry
Dmitry

Reputation: 3169

How can I copy artifacts from executed jobs with declarative pipeline?

My pipeline script looks like this:

pipeline {
    agent {
        label 'my-pc'
    }

    stages {
        stage ('Build') {
            steps {
                build job: 'myjob', parameters: [string(name: 'BRANCH', value: 'master')]
            }
            post {
                always {
                    sh 'echo TODO: copy artifacts here'
                }
            }
        }

    }
}

I want to copy artifacts generated by myjob. How can I do this?

Jenkins documentation page "Recording tests and artifacts" has an instruction which is not applicable for my pipeline (in my case artifact is generated by a separate job).

Upvotes: 5

Views: 11976

Answers (2)

f0zz
f0zz

Reputation: 31

There is a simpler syntax than what Travenin and Vadim Kotov wrote above. It was introduced in version 1.39 of the plugin. You can use the following to copy all artifacts from the last successful run of myJob:

pipeline {
    // pipeline code

    steps {
        copyArtifacts(filter:'*', projectName: 'myJob', selector: lastSuccessful())
    }

    // pipeline code
}

This syntax works with both scripted and declarative pipeline. Check out the available parameters for the copyArtifacts function on the Copy Artifact Plugin wiki page.

Upvotes: 3

Travenin
Travenin

Reputation: 1590

You can use Copy Artifact plugin and then you can use it with step step, which allows to call builders or post-build actions as in Freestyle jobs. See Pipeline Syntax of your job and consult Snippet Generator. (https://[jenkins-url]/[path-to-your-job]/pipeline-syntax/)

This is how to copy all artifacts from job myjob to current pipeline job workspace:

pipeline {
    agent {
        label 'my-pc'
    }

    stages {
        stage ('Build') {
            steps {
                build job: 'myjob', parameters: [string(name: 'BRANCH', value: 'master')]
            }
            post {
                always {
                    step([
                        $class: 'CopyArtifact',
                        filter: '*',
                        projectName: 'myjob',
                        selector: [
                            $class: 'StatusBuildSelector',
                            stable: false
                        ]])
                }
            }
        }
    }
}

Upvotes: 4

Related Questions