【问题标题】:Passing Environment variables from Jenkins file to Shared Library which has the pipeline as code将环境变量从 Jenkins 文件传递​​到具有管道即代码的共享库
【发布时间】:2020-01-29 07:58:27
【问题描述】:

我正在尝试拥有一个具有实际管道即代码的公共共享库,类似于 vars/demo.groovy

下的内容
def call(Map pipelineParams) {

pipeline {
  agent {
    docker { image 'centos:latest' }
  }

    stages {
        stage("Env Variables") {
            steps {
                sh "printenv"
                //echo ${SERVICE_NAME}
            }
        }

        stage("test") {
            steps {
                sh "printenv"
            }
    }
  }
    }   
}

我将像这样从我的 Jenkinsfile 访问共享库。

library identifier: 'mylibraryname@master', 
    //'master' refers to a valid git-ref
    //'mylibraryname' can be any name
    retriever: modernSCM([
      $class: 'GitSCMSource',
      //credentialsId: 'your-credentials-id',
      remote: 'GIT URL'
    ])

demo()

它按预期工作,但我想发送一个额外的环境变量或覆盖我的 Jenkinsfile 中的现有变量,而不更改将我的管道作为代码的共享库。你能帮我解决这个问题吗?

我尝试给出如下变量:

demo {
    service = 'test'
    var1 = 'value'
}

并尝试他们以这种方式访问​​:

def call(Map pipelineParams) {

pipeline {
  agent {
    docker { image 'centos:latest' }
  }
    stages {
        stage("Env Variables") {
            steps {
                sh "printenv"
                echo "pipelineParams.service"
            }
        }

        stage("test") {
            steps {
                sh ' echo "hello pipelineParams.service '
            }
    }
  }
    }   
}

但出现以下错误:

[Pipeline] End of Pipeline

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: demo.call() is applicable for argument types: (org.jenkinsci.plugins.workflow.cps.CpsClosure2) values: [org.jenkinsci.plugins.workflow.cps.CpsClosure2@cad5c94]

Possible solutions: call(java.util.Map), wait(), any(), wait(long), main([Ljava.lang.String;), each(groovy.lang.Closure)

    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)

    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:64)

    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:54)

    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)

    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)

    at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:160)

    at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)

    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:157)

    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:142)

    at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:158)

【问题讨论】:

  • 我做了一个库来满足这个特定的需求,它还自动处理创建作业新作业,并且不需要执行管道来获取它的参数github.com/SAP/jenkins-pipelayer

标签: jenkins jenkins-pipeline shared-libraries


【解决方案1】:

签名是此处定义的地图

def call(Map pipelineParams)

所以你需要使用地图。 (https://www.baeldung.com/groovy-maps) 这看起来像:

demo([
    service: 'test',
    var1: 'value'
])

你的echo语句也不太对,应该是:

echo(pipelineParams.service)

根本不需要使用 GString 或字符串,因为它是一个 groovy 变量。

注意:我在示例中使用 ()。严格来说,你不需要它们。我只是更喜欢这样编码,所以很明显这些是方法参数。

您也可以只创建环境变量。虽然我可能不推荐它,并且会更像你目前计划的方式。

withEnv([SERVICE='test', VAR1='value]) {
    demo([:]) // I put an empty map here, if you did this you would change your call method to not have the map though
}

上面的这种方式确保环境变量只存在到闭包结束(最后一个 } 大括号)

另一种方式,但这只是设置它们,它们将全局存在并在运行的其余部分存在。不太好。

env.SERVICE = 'test'
env.VAR1 = 'value'

demo([:])

在任何一种情况下,您都将在共享库中检查:

echo env.SERVICE

虽然我认为第一种方式更好。接受 map 参数并使用它们。 env vars 可能是全局的,有时会根据您的运行情况引起问题。

【讨论】:

    猜你喜欢
    • 2021-05-20
    • 1970-01-01
    • 2018-06-22
    • 2020-12-05
    • 2023-02-18
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    相关资源
    最近更新 更多