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.
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/…
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With