0

i have a question on declarative pipeline scripting,am trying to set the variables dynamically depends on parameters passed before the stages start,say in environment block or node block

Just with one parameter, i wanted to construct the other variables dynamically with if condition at this moment,tried on both blocks(Environment ,Node )but no luck,as this needs to global i need this initialized before entering stages

  pipeline {
    environment {
    stream_name = "${stream_name}"
    user_id = "${user_id}"
    currentBuild_displayName = "${currentBuild_displayName}"
    GBE_ViewTag = "${DevWorkspace_name}"
    script {
        if ( ${Stream_name} == 'Allura_Main_Infra_PreInt') {
        loadrule = "Infra-uInfra/Infra.loadrule"
        } 
    }
}
agent   {
    node {
        label 'CP'
        customWorkspace 'D:\\RTC'

    }
  }

2 Answers2

1

Hi you can use environment{} block at pipeline level or at each stage level. In Environment block you can set your variables check below example:

pipeline {
    agent {label 'master'}
    environment{
        env1 = 'value0' // these are environment variables for all stages
    }
    stages{
        stage('stage 1') {
            environment{
                    env1 = 'value1' // these are environment variables for 'stage 1'
                    }
            steps{

                echo "$env1"
              }
            }
        stage('stage 2') {
            environment{
                    env1 = 'value2' // these are environment variables for 'stage 2'
                    }
            steps{

                echo "$env1"
              }
            }
        stage('stage 3') {
            steps{
                echo "$env1"
              }
            }
    }
}
Amit Nanaware
  • 3,203
  • 1
  • 6
  • 19
0

It also worked,if i moved all the logical conditions outside of the pipeline and the variables are available global in all stages

def user_id = currentBuild.rawBuild.getCause(Cause.UserIdCause).getUserId()
def full_name = currentBuild.rawBuild.getCause(Cause.UserIdCause).getUserName()
DevWorkspace_name = "${Developer_workspace}"
if ( DevWorkspace_name ==~ /(?s).*Allura_Main_Infra_PreInt.*/) {
        loadrule = "Infra-uInfra/Infra.loadrule"
        subsystem = "Infra"
        stream_name = "Allura_Main_Infra_PreInt"  
 } 
 pipeline {
  .....
 }