如何从参数值替换jenkins job scm中的github分支

时间:2018-02-13 10:25:28

标签: jenkins github groovy jenkins-job-dsl

我有一个jenkins工作groovy脚本,如下所示。我希望Github分支取自参数值。

Groovy脚本:

 git_url = "git@github.deere.com:ABC/XYZ.git" jenkins_node = "master"
    freeStyleJob('myjob') {
    logRotator(numToKeep = 100)
    parameters {    stringParam("GIT_BRANCH", "master" , "master cert dev")   }
    label(jenkins_node)
    scm {
     git {
      remote { url(git_url) }
      branch($GIT_BRANCH)  
      extensions { }
         }
     }

1 个答案:

答案 0 :(得分:3)

您必须将$GIT_BRANCH变量放入单引号中,以便它不会被作业DSL脚本解析。将您的脚本粘贴到此Job DSL playground app,您将获得例外:

javaposse.jobdsl.dsl.DslScriptException: (script, line 12) No such property: $GIT_BRANCH for class: javaposse.jobdsl.dsl.helpers.scm.GitContext
    at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScriptEngine(AbstractDslScriptLoader.groovy:112)
    at javaposse.jobdsl.dsl.AbstractDslScriptLoader$_runScripts_closure1.doCall(AbstractDslScriptLoader.groovy:59)
    at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScripts(AbstractDslScriptLoader.groovy:46)
    at javaposse.jobdsl.dsl.AbstractDslScriptLoader$runScripts$0.callCurrent(Unknown Source)

但是如果你添加单引号:

branch('$GIT_BRANCH')

然后您将正确生成您的作业XML文件:

<!-- 1. myjob -->
<project>
    <actions></actions>
    <description></description>
    <keepDependencies>false</keepDependencies>
    <properties>
        <hudson.model.ParametersDefinitionProperty>
            <parameterDefinitions>
                <hudson.model.StringParameterDefinition>
                    <name>GIT_BRANCH</name>
                    <defaultValue>master</defaultValue>
                    <description>master cert dev</description>
                </hudson.model.StringParameterDefinition>
            </parameterDefinitions>
        </hudson.model.ParametersDefinitionProperty>
    </properties>
    <canRoam>false</canRoam>
    <disabled>false</disabled>
    <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
    <triggers></triggers>
    <concurrentBuild>false</concurrentBuild>
    <builders></builders>
    <publishers></publishers>
    <buildWrappers></buildWrappers>
    <logRotator>
        <daysToKeep>100</daysToKeep>
        <numToKeep>-1</numToKeep>
        <artifactDaysToKeep>-1</artifactDaysToKeep>
        <artifactNumToKeep>-1</artifactNumToKeep>
    </logRotator>
    <assignedNode>master</assignedNode>
    <scm class='hudson.plugins.git.GitSCM'>
        <userRemoteConfigs>
            <hudson.plugins.git.UserRemoteConfig>
                <url>git@github.deere.com:ABC/XYZ.git</url>
            </hudson.plugins.git.UserRemoteConfig>
        </userRemoteConfigs>
        <branches>
            <hudson.plugins.git.BranchSpec>
                <name>$GIT_BRANCH</name>
            </hudson.plugins.git.BranchSpec>
        </branches>
        <configVersion>2</configVersion>
        <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
        <gitTool>Default</gitTool>
    </scm>
</project>

以下是我在沙盒中使用的作业DSL:

git_url = "git@github.deere.com:ABC/XYZ.git" 
jenkins_node = "master"
freeStyleJob('myjob') {
  logRotator(numToKeep = 100)
  parameters {
    stringParam("GIT_BRANCH", "master" , "master cert dev")   
  }

  label(jenkins_node)
  scm {
    git {
      remote { url(git_url) }
      branch('$GIT_BRANCH')  
      extensions { }
    }
  }
}

现在当您运行从此DSL生成的作业时,它将询问您GIT_BRANCH参数,并且您传递的值将用于设置分支。