Search code examples
jenkinsamazon-ec2jenkins-pipelineartifact-deployer

How to send artifacts back to master from EC2 slave


I'm building my Continuous Delivery System with Jenkins. To avoid spending a lot of money I set up my jenkins to spawn a new EC2 m4.large instance when a new push is made to Bitbucket. In this way I only pay when I work.

The problem now is that my artifacts stay in the slave and when the instance is terminated I can no longer access my files.

To archive the artifacts I set up my pipeline in this way:

pipeline {
agent {
  node {
    label 'jenkins-slave-spawn'
  }
}
stages {
    stage('prepare') {
        steps {
            sh 'npm install'
            sh 'npm update'
            dir ('./scripts/Web') {
              sh 'bower install'
              sh 'bower update'
            }
            dir ('./scripts/App') {
              sh 'bower install'
              sh 'bower update'
            }
            dir ('./scripts/Site'){
              sh 'bower install'
              sh 'bower update'
            }
            dir ('./scripts/Assistance'){
              sh 'bower install'
              sh 'bower update'
            }
        }
    }
    stage('build') {
        steps {
            sh 'grunt build-beta'
        }
    }
    stage('archive') {
        steps {
            archiveArtifacts artifacts: '**/builds/*', onlyIfSuccessful: true
        }
    }
}

}

But it doesn't seems to work since I cannot see my files anywhere.


Solution

  • I found a way using stash and unstash and switching nodes

     pipeline {
    agent {
      node {
        label 'jenkins-slave-spawn'
      }
    }
    stages {
        stage('prepare') {
            steps {
                sh 'npm install'
                sh 'npm update'
                dir ('./scripts/Web') {
                  sh 'bower install'
                  sh 'bower update'
                }
                dir ('./scripts/App') {
                  sh 'bower install'
                  sh 'bower update'
                }
                dir ('./scripts/Site'){
                  sh 'bower install'
                  sh 'bower update'
                }
                dir ('./scripts/Assistance'){
                  sh 'bower install'
                  sh 'bower update'
                }
            }
        }
        stage('build') {
            steps {
                sh 'grunt build-beta'
            }
        }
        stage('deploy') {
            steps {
                echo 'here will use pm2 to start node app'
            }
        }
        stage('archive') {
            steps {
              stash includes: 'builds/**', name: 'slave-artifacts'
              node('master'){
                  unstash 'slave-artifacts'
              }
            }
        }
    }
    }