Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

docker

jenkins

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.

like image 443
ChrisAdkin Avatar asked Dec 11 '17 16:12

ChrisAdkin


People also ask

How do you copy move files from one agent to another in a pipeline in Jenkins?

One way to copy files is using the stash and unstash directive in Jenkins. However, keep in mind that it is useful for only small files. jenkins.io/doc/pipeline/steps/workflow-basic-steps/…

Under which section is the copy artifacts option present in Jenkins?

Next you need to install the Copy Artifact plugin in the Manage Plugins section of Jenkins. Go to "[PROJECT-NAME]-Output" > configure and add a new build step. Because you have installed the Copy Artifact plugin you should see an option called 'copy artifacts from another project' in the drop down menu.


1 Answers

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

like image 77
Ophidian Avatar answered Oct 26 '22 03:10

Ophidian