JJB(Jenkins Job Builder)如何将变量传播到管道脚本中?

问题描述 投票:0回答:1

我想重用一个 Groovy 脚本,基于它创建多个作业。
当你有类似的工作时,看起来不错。
我有这样的东西:

我的定义.yml

- project:
    name: my-project
    jobs:
        - my-jobs

- job-group:
    name: my-jobs
    jobs:
        - "{job-name}_job":
            job-name: '1'
            file-path: "./path/to/my_job.groovy"
            var1: "smth" <-- here
- job-template:
    name: "{job-name}_job"
    project-type: pipeline
    concurrent: true
    dsl: !include-raw: "{file-path}"

我的工作.groovy

pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                echo '{var1}' <-- here
            }
        }
    }
}

输出:

...
    <script>pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                echo '{var1}'
            }
        }
    }
}
</script>
...

预期:

...
    <script>pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                echo 'smth'
            }
        }
    }
}
</script>
...

JJB 有没有办法做到这一点?

JJB版本:

jjb --version

Jenkins Job Builder version: 3.11.0

jenkins groovy jenkins-pipeline jenkins-job-builder
1个回答
0
投票

它适用于 5.1.0 版本,但不适用于 4.3.0。

但是,您还必须转义“{}”字符,因为它们是针对字符串格式进行处理的。所以你的脚本需要是这样的:

pipeline {{
    agent any
    stages {{
        stage('Example') {{
            steps {{
                echo '{var1}'
            }}
        }}
    }}
}}

使用 5.1.0 和此更改,输出将如下所示:

<flow-definition plugin="workflow-job">
  <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps">
    <script>pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                echo 'smth'
            }
        }
    }
}
</script>
© www.soinside.com 2019 - 2024. All rights reserved.