Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reuse artifacts at a later stage in the same Jenkins project

I have a Jenkins pipeline whose Build step has an archiveArtifacts command.

After the Build step there is Unit test, Integration test and Deploy.

In Deploy step, I want to use one of the artifacts. I thought I could find it in the same place the Build step generated it, but apparently the archiveArtifacts has deleted them.

As a workaround I can copy the artifact before it is archived, but it doesn't look elegant to me. Is there any better way?

like image 267
ris8_allo_zen0 Avatar asked May 11 '17 13:05

ris8_allo_zen0


People also ask

Can we use different nodes for each stage in Jenkins?

You can also mix and match node { stage {..}} and stage { node {..}} within your pipeline codes. By design (in Jenkins, as well as in the concept of continuous delivery), stages do not execute in parallel. Only the steps within a single stage are executed in parallel.

Can we have multiple steps in a stage in Jenkins?

Jenkins Pipeline allows you to compose multiple steps in an easy way that can help you model any sort of automation process. Think of a "step" like a single command which performs a single action. When a step succeeds it moves onto the next step. When a step fails to execute correctly the Pipeline will fail.


1 Answers

As I understand it, archiveArtifacts is more for saving artifacts for use by something (or someone) after the build has finished. I would recommend looking at using "stash" and "unstash" for transferring files between stages or nodes.

You just go...

stash include: 'globdescribingfiles', name: 'stashnameusedlatertounstash'

and when you want to later retrieve that artifact...

unstash 'stashnameusedlatertounstash'

and the stashed files will be put into the current working directory.

Here's the example of that given in the Jenkinsfile docs (https://jenkins.io/doc/book/pipeline/jenkinsfile/#using-multiple-agents):

pipeline {
    agent none
    stages {
        stage('Build') {
            agent any
            steps {
                checkout scm
                sh 'make'
                stash includes: '**/target/*.jar', name: 'app' 
            }
        }
        stage('Test on Linux') {
            agent { 
                label 'linux'
            }
            steps {
                unstash 'app' 
                sh 'make check'
            }
            post {
                always {
                    junit '**/target/*.xml'
                }
            }
        }
        stage('Test on Windows') {
            agent {
                label 'windows'
            }
            steps {
                unstash 'app'
                bat 'make check' 
            }
            post {
                always {
                    junit '**/target/*.xml'
                }
            }
        }
    }
}
like image 88
Spencer Malone Avatar answered Sep 17 '22 14:09

Spencer Malone