【问题标题】:How to import a class with constants in Jenkins Pipeline?如何在 Jenkins Pipeline 中导入具有常量的类?
【发布时间】:2020-07-22 09:16:00
【问题描述】:

我在/src/ru/foo/bar/Const.groovy 中有一些只有常量而没有任何方法的类

package ru.foo.bar

class Const {
    public static final String BITBUCKET_LOGIN = "DEV_LOGIN"
    public static final String BITBUCKET_PASS = "DEV_PASS"
{

在 Jenkins 管道中,我这样使用它:

import ru.foo.bar.Const

pipeline {
    stages {
        stage {
            script {
                println("${Const.BITBUCKET_LOGIN}")
                doSomeThingWith(PASS: "${Const.BITBUCKET_PASS}")
            }
        }
    }
}

一切正常!

现在我需要为不同的环境制作不同的文件并在管道中动态加载它,但这样管道中的其他内容就不会发生任何变化。

我尝试评论//import ru.foo.bar.Const 并将类似的东西添加到管道中

sh 'cp environments/${Stand}/Const.groovy jenkins/Const.groovy'
def Const = load 'jenkins/Const.groovy'
println("${Const.BITBUCKET_LOGIN}")
doSomeThingWith(PASS: "${Const.BITBUCKET_PASS}")

我也试过了

load 'jenkins/Const.groovy'

但这并不像谷歌的其他想法那样奏效。

如何在管道中正确加载我的类,以便我可以访问像 ${Const.SOMEVARIABLE} 这样的常量?

【问题讨论】:

    标签: class jenkins groovy import load


    【解决方案1】:

    我自己解决了这个问题。

    /src/ru/foo/bar/Const.groovy:

    class Const {
        public static final String BITBUCKET_LOGIN = "DEV_LOGIN"
        public static final String BITBUCKET_PASS = "DEV_PASS"
    {
    return new Const()
    

    在管道中使用如下:

    sh 'cp environments/${Stand}/Const.groovy jenkins/Const.groovy'
    Const = load 'jenkins/Const.groovy'
    println("${Const.BITBUCKET_LOGIN}")
    doSomeThingWith(PASS: "${Const.BITBUCKET_PASS}")
    

    一切正常!

    【讨论】:

      猜你喜欢
      • 2017-10-03
      • 1970-01-01
      • 2011-12-27
      • 2019-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-23
      • 2020-04-06
      相关资源
      最近更新 更多