Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 301
Dmitry Avatar asked Apr 28 '17 09:04

Dmitry


2 Answers

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
                        ]])
                }
            }
        }
    }
}
like image 83
Travenin Avatar answered Oct 03 '22 08:10

Travenin


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.

like image 23
f0zz Avatar answered Oct 03 '22 07:10

f0zz