Jenkins DSL 作业脚本:如何在下游参数化步骤中访问环境变量(通过属性文件注入)



我的DSL作业脚本简介

job('test') {
  steps {
    shell('echo VERSION=$VERSION > version.txtn' +
          'echo VERSION_SUFFIX=$VERSION_SUFFIX >> version.txtn' + 
          'echo GROUP_ID=$GROUP_ID >> version.txt')
    // EnvInject Plugin 
    environmentVariables {
      propertiesFile('version.txt')
    }
  }
  publishers {
    postBuildScripts {
        steps {
            shell('echo ${VERSION}')
        }
        onlyIfBuildSucceeds(false)
        onlyIfBuildFails(false)
    }
    downstreamParameterized {
      trigger('next-job') {
        parameters {
          predefinedProp('relVersion', '${VERSION}')
        }
      }
    }
  }  
}

我需要$VERSION数字将参数传递给下游作业。

我试过了,${env。版本} 并且还尝试了很多选项,但我无法抓住版本.任何帮助不胜感激,提前感谢。

您可以使用在

SCM 之前执行的选项Prepare an environment for the run

选项Prepare an environment for the run不属于pre-build/ build /post build,而是作业properties

DSL API 不支持配置此选项。但是我们可以使用配置块。

job('next-job') {
  configure { project -> 
    project / 'properties' << 'EnvInjectJobProperty' {
        info {
          loadFilesFromMaster false
          propertiesContent 'Branch=${relVersion}'
        }
        keepBuildVariables true
        keepJenkinsSystemVariables true
        overrideBuildParameters false
        on true
    }
  } // end of configure block
  scm { 
    git { 
      remote { 
        url("ssh://git@bitbucket.rl.git") 
      } 
        branches('${branch}') 
    } 
  } // end of scm
  steps {}
  publishers {}
}

上面的作业DSL可以生成以下xml作为种子作业配置的内容.xml

<project>
    <actions></actions>
    <description></description>
    <keepDependencies>false</keepDependencies>
    <properties>
        <EnvInjectJobProperty>
            <info>
                <loadFilesFromMaster>false</loadFilesFromMaster>
                <propertiesContent>Branch=${relVersion}</propertiesContent>
            </info>
            <keepBuildVariables>true</keepBuildVariables>
            <keepJenkinsSystemVariables>true</keepJenkinsSystemVariables>
            <overrideBuildParameters>false</overrideBuildParameters>
            <on>true</on>
        </EnvInjectJobProperty>
    </properties>
    <canRoam>true</canRoam>
    <disabled>false</disabled>
    <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
    <triggers></triggers>
    <concurrentBuild>false</concurrentBuild>
    <builders></builders>
    <publishers></publishers>
    <buildWrappers></buildWrappers>
    <scm class='hudson.plugins.git.GitSCM'>
        <userRemoteConfigs>
            <hudson.plugins.git.UserRemoteConfig>
                <url>ssh://git@bitbucket.rl.git</url>
            </hudson.plugins.git.UserRemoteConfig>
        </userRemoteConfigs>
        <branches>
            <hudson.plugins.git.BranchSpec>
                <name>${branch}</name>
            </hudson.plugins.git.BranchSpec>
        </branches>
        <configVersion>2</configVersion>
        <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
        <gitTool>Default</gitTool>
    </scm>
</project>

您可以在 http://job-dsl.herokuapp.com/上尝试jod DSL,以检查从中生成的xml是否按预期进行。

相关内容

  • 没有找到相关文章

最新更新