0

I would like to set a variable in one stage and use that in a following stage in a pure declarative Jenkins pipeline.

I DO NOT want to:

  • Use a script block to set the variable.
  • Use a temporary file to store the value.

Is it possible?

Things I have already tried:

  • Usage of environment block: can't override variables set in global environment block inside a stage. Variables inside stage specific environment blocks don't outlive the scope of a specific stage.
  • Exporting an environment variable (using 'sh' step) doesn't got beyond the scope of stage.
  • Direct variable assignment inside "steps" doesn't seem to be possible.

Please help !

Arnab
  • 1,308
  • 1
  • 13
  • 18
  • Why don't you want to use a `script` block ? It would solve your problem – Arnaud Claudel Aug 28 '19 at 14:10
  • @ArnaudClaudel I am as of now using script block only... however, I am trying to align towards declarative syntax as a standard, as much as I can. – Arnab Sep 17 '19 at 02:49

1 Answers1

0

I think one way to solve this, is to create a temp variable via a shared library. This way, you can keep it declarative. Well, at least in the pipeline.

https://jenkins.io/doc/book/pipeline/shared-libraries/

There is probably an easier way, but this is the only thing I can think of, to keep it declarative.

  1. create a shared lib.
  2. Create for the shared lib a vars/tempVar.goovy
    def temp = null

    def call(args) {
        temp = args.value
    }
  1. import it to your pipeline and use it.
        stages {
            stage('Stage - 1') {
                steps {
                    tempVar value: 'Test Temp Var'
                }
            }
            stage('Stage - 2') {
                steps {
                    echo "${tempVar.temp}"
                }
            }
        }
Tai Ly
  • 341
  • 1
  • 2
  • 10