How to create jenkinsfile properties to run groovy code on

I’m trying to create properties that can be set in the jenkins file but I can’t find any details on how to do it. I’m looking to set a property in the jenkinsfile so that users can set a value I can create pipeline groovy for. I haven’t been able to find anything on creating and how to use that property value.

@jpat There are many ways to define properties inside a Jenkins file

  1. Using def

    def property = "propety-value"
    
  2. If you want it as build parameters, you need to look at options like build with parameters, or active choice parameter for dynamic decision parameters.

properties([
  parameters([
   [$class: 'ChoiceParameter', 
            choiceType: 'PT_SINGLE_SELECT', 
            description: 'Select the Env Name from the Dropdown List', 
            filterLength: 1, 
            filterable: true, 
            name: 'SoftEnv', 
            randomName: 'choice-parameter-5631314439613978', 
            script: [
                $class: 'GroovyScript', 
                fallbackScript: [
                    classpath: [], 
                    sandbox: false, 
                    script: 
                        'return[\'Could not get Env\']'
                ], 
                script: [
                    classpath: [], 
                    sandbox: false, 
                    script: 
                        'return["Dev","QA","Stage","Prod"]'
                ]
            ]
        ]
  ])
])
node() {
       ansiColor('xterm') {
           stage("Clone Terrafrom project") {
                    echo "Selected env is ${params.SoftEnv}"
                    }
        }
}

You can ask users to provide input and can take decision on the basis of that in your pipeline.

1 Like