使用预定义的变量

使用预定义的变量

本文介绍了如何配置 Jenkins 2 流水线以便 Jenkinsfile 使用预定义的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个项目使用几乎相同的 Jenkinsfile.唯一的区别是它必须签出的 git 项目.这迫使我每个项目都有一个 Jenkinsfile,尽管他们可以共享同一个:

节点{def mvnHome = 工具'M3'def artifactId默认版本阶段('提交阶段'){echo '从 Git 下载...'git 分支:'develop',credentialsId:'xxx',url:'https://bitbucket.org/xxx/yyy.git'echo '正在构建项目并生成 Docker 映像...'sh "${mvnHome}/bin/mvn clean install docker:build -DskipTests"...

有没有办法在作业创建期间将 git 位置预配置为变量,以便我可以重用相同的 Jenkinsfile?

...阶段('提交阶段'){echo '从 Git 下载...'git 分支:'develop',credentialsId:'xxx',url:env.GIT_REPO_LOCATION...

我知道我可以这样设置:

这个项目是参数化的 -> String Parameter -> GIT_REPO_LOCATION, default= http://xxxx,通过env访问.GIT_REPO_LOCATION.

缺点是提示用户使用默认值开始构建或更改它.我需要它对他的用户是透明的.有办法吗?

解决方案

你可以使用 管道共享 Groovy 库插件 拥有一个库,您的所有项目都在 git 存储库中共享.在 文档中,您可以详细阅读.

如果您有很多相似的管道,全局变量机制提供了一个方便的工具来构建捕获相似性的更高级别的 DSL.例如,所有 Jenkins 插件都是以相同的方式构建和测试的,所以我们可以编写一个名为 buildPlugin 的步骤:

//vars/buildPlugin.groovydef 调用(正文){//评估 body 块,并将配置收集到对象中默认配置 = [:]body.resolveStrategy = Closure.DELEGATE_FIRSTbody.delegate = 配置身体()//现在构建,基于提供的配置节点{git url:https://github.com/jenkinsci/${config.name}-plugin.git"sh "mvn 安装"邮寄至:...",主题:${config.name} 插件构建",正文:..."}}

假设脚本已作为全局共享库加载或作为文件夹级共享库,生成的 Jenkinsfile 将是非常简单:

Jenkinsfile(脚本流水线)

buildPlugin {名称 = 'git'}

该示例显示了 jenkinsfile 如何将 name = git 传递给库.我目前使用类似的设置,对此非常满意.

I have several projects that use a Jenkinsfile which is practically the same. The only difference is the git project that it has to checkout. This forces me to have one Jenkinsfile per project although they could share the same one:

node{
    def mvnHome = tool 'M3'
    def artifactId
    def pomVersion

    stage('Commit Stage'){
        echo 'Downloading from Git...'
        git branch: 'develop', credentialsId: 'xxx', url: 'https://bitbucket.org/xxx/yyy.git'
        echo 'Building project and generating Docker image...'
        sh "${mvnHome}/bin/mvn clean install docker:build -DskipTests"
    ...

Is there a way to preconfigure the git location as a variable during the job creation so I can reuse the same Jenkinsfile?

...
    stage('Commit Stage'){
        echo 'Downloading from Git...'
        git branch: 'develop', credentialsId: 'xxx', url: env.GIT_REPO_LOCATION
    ...

I know I can set it up this way:

This project is parameterized -> String Parameter -> GIT_REPO_LOCATION, default= http://xxxx, and access it with env.GIT_REPO_LOCATION.

The downside is that the user is promted to start the build with the default value or change it. I would need that it were transparent to he user. Is there a way to do it?

解决方案

You can use the Pipeline Shared Groovy Library plugin to have a library that all your projects share in a git repository. In the documentation you can read about it in detail.

// vars/buildPlugin.groovy
def call(body) {
    // evaluate the body block, and collect configuration into the object
    def config = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config
    body()

    // now build, based on the configuration provided
    node {
        git url: "https://github.com/jenkinsci/${config.name}-plugin.git"
        sh "mvn install"
        mail to: "...", subject: "${config.name} plugin build", body: "..."
    }
}
buildPlugin {
    name = 'git'
}

The example shows how a jenkinsfile passes name = git to the library.I currently use a similar setup and am very happy with it.

这篇关于如何配置 Jenkins 2 流水线以便 Jenkinsfile 使用预定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 02:39