如何配置Jenkins 2管道,以便Jenkinsfile使用预定义的变量 [英] How to configure a Jenkins 2 Pipeline so that Jenkinsfile uses a predefined variable

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

问题描述

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

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"
    ...

在作业创建过程中是否可以将git位置预先配置为变量,以便我可以重用相同的Jenkinsfile?

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:

此项目已参数化->字符串参数-> GIT_REPO_LOCATION,默认= http://xxxx ,并使用env访问它.GIT_REPO_LOCATION.

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?

推荐答案

您可以使用管道共享Groovy库插件,以使您的所有项目在git存储库中共享一个库.在文档中,您可以详细了解它.

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.

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

If you have a lot of Pipelines that are mostly similar, the global variable mechanism provides a handy tool to build a higher-level DSL that captures the similarity. For example, all Jenkins plugins are built and tested in the same way, so we might write a step named buildPlugin:

// 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: "..."
    }
}

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

Assuming the script has either been loaded as a Global Shared Library or as a Folder-level Shared Library the resulting Jenkinsfile will be dramatically simpler:

Jenkinsfile(脚本管道)

Jenkinsfile (Scripted Pipeline)

buildPlugin {
    name = 'git'
}

该示例显示jenkinsfile如何将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使用预定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆