environmentVariables() 不适用于 Jenkins Job DSL 和 Pipeline Jobs

environmentVariables() not working for Jenkins Job DSL and Pipeline Jobs

我们使用Jenkins Job DSL for our CI setup. Since we are using a special command only available in the traditional Jenkinsfile syntax, we need to use a pipeline job.

在管道作业中,我们从 Git 检查我们的项目。我们正在为多个项目使用管道作业,因此我们希望将 git url 注入管道脚本。

这是生成管道作业的脚本的简短版本:

def createPipelineJob(def jobName, def gitUrl) {
    pipelineJob(jobName) {
        environmentVariables(GIT_URL: gitUrl)
        definition {
            cps {
                script('''
                    node {
                        sh 'env | sort' 
                    }
                ''')
                sandbox(true)
            }
        }       
    }   
}

这将创建以下 XML 配置:

<flow-definition>
    <actions/>
    <description/>
    <keepDependencies>false</keepDependencies>
    <properties>
        <EnvInjectJobProperty>
            <info>
                <propertiesContent>GIT_URL=my-git.url</propertiesContent>
                <loadFilesFromMaster>false</loadFilesFromMaster>
            </info>
            <on>true</on>
            <keepJenkinsSystemVariables>true</keepJenkinsSystemVariables>
            <keepBuildVariables>true</keepBuildVariables>
            <overrideBuildParameters>false</overrideBuildParameters>
            <contributors/>
        </EnvInjectJobProperty>
    </properties>
    <triggers/>
    <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition">
        <script>
            node { sh 'env | sort' }
        </script>
        <sandbox>true</sandbox>
    </definition>
</flow-definition>

但是如果我运行这个,GIT_URL环境变量没有列出(其他环境变量是)。但是,如果我改为使用此 setup 手动创建管道作业,则 GIT_URL 环境变量打印得很好。手动创建作业几乎会创建相同的 xml 配置:

<flow-definition plugin="workflow-job@2.15">
    <actions>
        <io.jenkins.blueocean.service.embedded.BlueOceanUrlAction plugin="blueocean-rest-impl@1.3.1">
            <blueOceanUrlObject class="io.jenkins.blueocean.service.embedded.BlueOceanUrlObjectImpl">
                <mappedUrl>blue/organizations/jenkins/test-jobname</mappedUrl>
                <modelObject class="flow-definition" reference="../../../.."/>
            </blueOceanUrlObject>
        </io.jenkins.blueocean.service.embedded.BlueOceanUrlAction>
    </actions>
    <description/>
    <keepDependencies>false</keepDependencies>
    <properties>
        <com.sonyericsson.rebuild.RebuildSettings plugin="rebuild@1.27">
            <autoRebuild>false</autoRebuild>
            <rebuildDisabled>false</rebuildDisabled>
        </com.sonyericsson.rebuild.RebuildSettings>
        <EnvInjectJobProperty plugin="envinject@2.1.5">
            <info>
                <propertiesContent>GIT_URL=my-git.url</propertiesContent>
                <secureGroovyScript plugin="script-security@1.35">
                <script/>
                <sandbox>false</sandbox>
            </secureGroovyScript>
            <loadFilesFromMaster>false</loadFilesFromMaster>
        </info>
        <on>true</on>
        <keepJenkinsSystemVariables>true</keepJenkinsSystemVariables>
        <keepBuildVariables>true</keepBuildVariables>
        <overrideBuildParameters>false</overrideBuildParameters>
    </EnvInjectJobProperty>
        <org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
            <triggers/>
        </org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
    </properties>
    <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps@2.41">
        <script>
            node { sh 'env | sort' }
        </script>
        <sandbox>true</sandbox>
    </definition>
    <triggers/>
    <disabled>false</disabled>
</flow-definition>

我们很迷茫,因为我们是 jenkins 的新手,这个问题已经困扰我们好几天了。

编辑:

这更像是评论而不是答案,但我修改并测试了您的 DSL 代码并且它工作正常。

我使用脚本创建了一个 DSL 作业:

def createPipelineJob(def jobName, def gitUrl) {
    pipelineJob(jobName) {
        environmentVariables(GIT_URL: gitUrl)
        definition {
            cps {
                script('''
                    node {
                        sh "echo $GIT_URL" 
                    }
                ''')
                sandbox(true)
            }
        }       
    }   
}
createPipelineJob('new-job-2','my-git.url')

生成的管道作业与您 post 编辑的作业具有相同的 XML(减去 shell 脚本),构建管道作业会打印 [=26] 的值=].

[new-job-1] Running shell script
+ echo my-git.url
my-git.url

我的推荐:

  • 如果您 post 编辑的简短版本(或者尝试我的)不起作用,我会尝试查看升级 Jenkins 或插件是否有任何不同。
  • 如果您 post 或我的简短版本确实有效,也许 post 完整版本可能存在错误。

事实证明环境注入器插件没有安装成功,因此脚本没有正确运行。所以我所要做的就是重新启动 Jenkins,一切正常。特别感谢 确保我的脚本确实正确。