Search code examples
jenkinsjenkins-pipelinejenkins-plugins

how to access folder variables across pipeline stages?


I am trying to create multiple pipeline jobs under a folder. Under this folder I have created some folder properties. I am having a hard time to use this folder properties across multiple stages in a job.

plugin used : https://wiki.jenkins.io/display/JENKINS/Folder+Properties+Plugin

def region
pipeline {
    agent any
    stages {
        stage('Assign values to global properties') {
            steps {
                withFolderProperties{
                    region = "${env.appRegion}"
                }       
            }
        }
        stage('Print') {
            steps {
                print(region)      
            }
        }
    }
}

Error:

Expected a step @ line 8, column 21.
                       region = "${env.appRegion}"

Thanks in Advance


Solution

  • region = "${env.appRegion}" is not pipeline reserved name of step or directive. It's groovy statement. You should put them inside script step. If you used Scripted Pipeline you can put any kinds of groovy statement in anywhere. But for Declarative Pipeline any groovy statement should wrapped in script step.

    steps {
        script {
            withFolderProperties{
                region = "${env.appRegion}"
            }
        }
    }
    
    steps {
        withFolderProperties{
            script {
                region = "${env.appRegion}"
            }
        }
    }
    

    I'm not sure which one code block above is work, but you can give a try.