ChrisAdkin
ChrisAdkin

Reputation: 1326

How to copy an artefact from one agent to another in a Jenkins declarative pipeline?

I'd like to use a Jenkins declarative pipeline and the agent syntax to build an artefact that I want to then deploy to a side car container, as illustrated in this pseudo-code:

pipeline {
    agent none 
    stages {
        stage('Build Artefact') {
            agent { docker 'build-agent' } 
            steps {
                < I want to create the artefact to deploy to a side car container here >
            }
        }
        stage('Deploy Artefact') {
            agent { docker 'side-car' } 
            steps {
                < I want to deploy the artefact created in the previous stage here >
            }
        }
    }
}

What I am struggling with is working out how to pass a file from the container used by the 'Build Artefact' stage to the container used in the 'Deploy Artefact', as far as I am aware stash will not work across containers, unless anyone has experience otherwise.

According to the Jenkins documentation, you can use the args argument to specify a volumes for the declarative pipeline syntax:

pipeline {
    agent {
        docker {
            image 'maven:3-alpine'
            args '-v $HOME/.m2:/root/.m2'
        }
    }
    stages {
        stage('Build') {
            steps {
                sh 'mvn -B'
            }
        }
    }
}

However, I'm wondering if there is a more elegant solution that does not involve passing volumes around.

Upvotes: 8

Views: 9097

Answers (2)

Ophidian
Ophidian

Reputation: 10033

Provided that the artifact isn't too big, you can use the stash directive to pass some file(s) between stages in different containers.

pipeline {
    agent none 
    stages {
        stage('Build Artefact') {
            agent { docker 'build-agent' } 
            steps {
                sh 'make'
                stash includes: 'myartefact', name: 'ARTEFACT'
            }
        }
        stage('Deploy Artefact') {
            agent { docker 'side-car' } 
            steps {
                unstash 'ARTEFACT'
                sh 'deploy.sh'
            }
        }
    }
}

For full details see the stash Documentation

Upvotes: 11

Daniel Majano
Daniel Majano

Reputation: 1116

You can map a logical docker volume with your physical jenkins directory (the own job workspace for example) using the -v parameter when you use the "docker run".

Here is more detailed:

https://damnhandy.com/2016/03/06/creating-containerized-build-environments-with-the-jenkins-pipeline-plugin-and-docker-well-almost/

Upvotes: 0

Related Questions