0
votes

I'm trying to get the following features to work in Jenkins' Declarative Pipeline syntax:

  • Conditional execution of certain stages only on the master branch
  • input to ask for user confirmation to deploy to a staging environment
  • While waiting for confirmation, it doesn't block an executor

Here's what I've ended up with:

pipeline {
  agent none
  stages {
    stage('1. Compile') {
      agent any
      steps {
        echo 'compile'
      }
    }
    stage('2. Build & push Docker image') {
      agent any
      when {
        branch 'master'
      }
      steps {
        echo "build & push docker image"
      }
    }
    stage('3. Deploy to stage') {
      when {
        branch 'master'
      }
      input {
        message "Deploy to stage?"
        ok "Deploy"
      }
      agent any
      steps {
        echo 'Deploy to stage'
      }
    }
  }
}

The problem is that stage 2 needs the output from 1, but this is not available when it runs. If I replace the various agent directives with a global agent any, then the output is available, but the executor is blocked waiting for user input at stage 3. And if I try and combine 1 & 2 into a single stage, then I lose the ability to conditionally run some steps only on master.

Is there any way to achieve all the behaviour I'm looking for?

1

1 Answers

1
votes

You need to use the stash command at the end of your first step and then unstash when you need the files

I think these are available in the snippet generator

As per the documentation

Saves a set of files for use later in the same build, generally on another node/workspace. Stashed files are not otherwise available and are generally discarded at the end of the build. Note that the stash and unstash steps are designed for use with small files. For large data transfers, use the External Workspace Manager plugin, or use an external repository manager such as Nexus or Artifactory