P Kuijpers
P Kuijpers

Reputation: 1653

How to copy artifact from in the same pipeline-build?

When I use the Copy Artifact Plugin for Jenkins, how can I retrieve the proper artifact from another job that as run (downstream) by the same pipeline? Right now I'm using the artifact from the 'Latest successful build', but that might also be the wrong one, because the job I'm getting the artifact from might already be run again and produced another artifact.

A little more explanation for the complete setup:


For my project I'm using Jenkins to build & auto-deploy to Nexus, HockeyApp and the Play Store. I've created a Multibranch Pipeline which uses the following JenkinsFile (simplified version):

#!groovy

node('android') {
    def branchName = env.BRANCH_NAME
    def params = [string(name: 'BRANCH_NAME', value: branchName)]
    echo "Using branch: ${branchName}"

    stage('Build & Unittests') {
        build job: 'Android - Unittests', parameters: params
    }

    if (branchName == 'master') {
        stage('Nexus publish') {
            build job: 'Android - Nexus publish', parameters: params
        }

        stage('HockeyApp') {
            build job: 'Android - HockeyApp', parameters: params
        }

        stage('Google Play Store') {
            build job: 'Android - Play Store', parameters: params
        }
    }
}

All other jobs are Multi-configuration projects with 2 axis: FLAVOR and TYPE

The job 'Android - Unittests' will create an artifact of the .apk which can be used by all jobs that follow. To do so, I've used the Copy Artifact Plugin.

As an example, this is my build configuration for the job 'Android - Nexus publish':

'Android - Nexus publish' build configuration example

So how can I use the proper artifact from the 'Android - Unittests' job?

ps. this is quite important in my case, because I'm working with several branches. It happens quite often that several branches will start the pipeline, so the 'last successful build' isn't always the correct one: the job 'Android - Play Store' might be built after another branch has already started

Upvotes: 0

Views: 1087

Answers (1)

Vasiliki Siakka
Vasiliki Siakka

Reputation: 1283

You can copy an artifact based on the id of the build that generated it.

To do that, you would need to:

  • change your pipeline so that it captures the id of the 'Android - Unittests' it's triggering
  • change 'Android - Nexus publish'
    • add a string variable which will hold the build id of the 'Android - Unittests' job eg 'BUILD_AND_UNIT_TEST_JOB_ID'
    • change the configuration of the copy artifacts step, to use a specific build based on the param you added.

So, your 'Android - Unittests' pipeline should look like this:

#!groovy
node('android') {
    def branchName = env.BRANCH_NAME
    def params = [string(name: 'BRANCH_NAME', value: branchName)]
    echo "Using branch: ${branchName}"

    def buildAnUnitTestJobId
    stage('Build & Unittests') {
        def buildAnUnitTestJob = build job: 'Android - Unittests', parameters: params
        // Note that buildAnUnitTestJob will be null if 'Android - Unitttests' fails
        buildAnUnitTestJobId  = buildAnUnitTestJob.id
    }

    if (branchName == 'master') {
        stage('Nexus publish') {
            build job: 'Android - Nexus publish', parameters: params + [string(name:'BUILD_AND_UNIT_TEST_JOB_ID', value: buildAnUnitTestJob)]
        }

        stage('HockeyApp') {
            build job: 'Android - HockeyApp', parameters: params
        }

        stage('Google Play Store') {
            build job: 'Android - Play Store', parameters: params
        }
    }
}

And 'Android - Nexus publish' should have these changes: Add string variable for build id Change your copy artifact step

Upvotes: 2

Related Questions