jenkinsjenkins-job-dsljcasc

Pass variable to JobDsl seed job (Jenkins) in scriptText?


I am working on a project and i have to configure a jenkins using JCasC (config as code plugin). I have to create a job BUT i can't pass variables in the script.

My code:

      freeStyleJob("SEED") {
        parameters {
          stringParam("MY_PARAMETER", "defaultValue", "A parameter")
        }

        steps {
          jobDsl {
            scriptText('''
            job("seedJOB") {
              displayName('${MY_PARAMETER}') // don't work
              description("${MY_PARAMETER}") // don't work
            //description("$MY_PARAMETER")   // don't work
            //description('$MY_PARAMETER')   // don't work
            // i tried to use triple full quotes instead of triple single quote but it's not working...

              ... here the job...
            '''.stripIndent())
          }
        }

EDIT: BEST SOLUTION HERE:

i'm writing groovy code in """ quotes so if I want to evaluate variable : I don't have to put ${} just write your variable name: With the solution:

      freeStyleJob("SEED") {
        parameters {
          stringParam("MY_PARAMETER", "defaultValue", "A parameter")
        }

        steps {
          jobDsl {
            scriptText('''
            job("seedJOB") {
              displayName('MY_PARAMETER) // solution

              ... here the job...
            '''.stripIndent())
          }
        }

easy!


Solution

  • May you could write it to a file ? You'll get something like that in your step:

    steps {
      shell('echo $DISPLAY_NAME > display_name.txt')
      jobDsl {
        scriptText('''
          job("seedjob") {
            String jobname = readFileFromWorkspace('display_name.txt').trim()
            displayName(jobname)
          }
        '''.stripIndent())
      }
    }
    

    You could also use a .properties file to do it more properly.