将变量传递给scriptText中的JobDsl种子作业(Jenkins)?

时间:2020-10-19 19:46:43

标签: jenkins jenkins-job-dsl jcasc

我正在一个项目上,我必须使用JCasC(配置为代码插件)来配置jenkins。 我必须创建一个作业,但无法在脚本中传递变量。

我的代码:

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

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

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

编辑:此处为最佳解决方案:

我正在用“”“引号编写普通代码,因此,如果我要评估变量:我不必输入$ {},只需编写变量名即可: 解决方案:

  btnTag.onclick = function(e) {
    if(btnTag.textContent === "light mode") {
        document.body.style.backgroundColor = "pink";
        btnTag.style.backgroundColor = "white";
        btnTag.textContent = "dark mode";
        btnTag.style.color = "black";
    } else 
    if (btnTag.textContent === "dark mode") {
        document.body.style.backgroundColor = "black";
        btnTag.style.backgroundColor = "gray";
        btnTag.textContent = "light mode";
        btnTag.style.color = "white";
    }
  }

容易!

1 个答案:

答案 0 :(得分:1)

可以将其写入文件吗?您将在步骤中得到类似的东西:

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

您还可以使用.properties文件来更正确地进行操作。