0

Here is a pseudo-code :
wherein "env1" and "env2" are two env variables which I want to use in some definition in the later stage

pipeline {
        agent {
    .
    .
    .
  environment {
        BUILD_CAUSE = getCause(currentBuild)
        env1 = (to be set to true when BUILD_CAUSE is Gerritcause and should be set to false when build cause is Timetriggercause)
        env2 = "true"
  }

 stage('use the env'){
       steps{
          node('master') {
           build.useenv("$env1","$env2")
          }
       }
     }
   .
   .
  
  • 1
    Have a look at this answer https://stackoverflow.com/a/53541813/10721630. It describes how env variables function and how to set/overwrite them. – Melkjot Sep 23 '21 at 15:23
  • script { if ( '${env.BUILD_CAUSE}' == 'GerritCause' ){ env.env2 = "false" } else if ( '${env.BUILD_CAUSE}' == 'TimerTriggerCause' ) { env.env2 = "true" } } sh 'echo '${env1}'' -> is not printing anything, I have not declared any env in the environment section. – Question Mark Sep 23 '21 at 15:55
  • `env1` wasn't set anywhere. – Melkjot Sep 24 '21 at 09:33
  • Ohh it's sh echo env2 only. Typed wrong by mistake. – Question Mark Sep 25 '21 at 10:22
  • What if `env.BUILD_CAUSE` is neither `GerritCause` nor `TimerTriggerCause`? Then `env1` is not set and `sh "echo ${env1}"` will not print anything. – Melkjot Sep 26 '21 at 16:57
  • It is GerritCaue, I am printing that on the top. – Question Mark Oct 04 '21 at 07:02

0 Answers0