如何在不获取 groovy.lang.MissingPropertyException 的情况下访问管道中的 Jenkins 凭据?

How can I access Jenkins credentials in pipeline without getting groovy.lang.MissingPropertyException?

我已经为我的管道创建了名为 GITHUB_TOKEN 的 Jenkins 凭据。我正在从管道的以下代码访问该凭据。

def downloadGitAssets(String owner, String repo_name, String tag, String assert_name) {
    dir("scripts") {
        withGithubCredentials(GITHUB_TOKEN){
            return sh(
                    script: "./git_assets_download.sh ${GITHUB_TOKEN} ${owner} ${repo_name} ${tag} ${assert_name}",
                    returnStdout: true,
            ).trim()
        }
    }
}

def withGithubCredentials(credentialsId, body) {
    withCredentials([
            string(
                    credentialsId: credentialsId,
                    variable: "GITHUB_TOKEN"
            ),
    ]) {
        body()
    }
}

但我在 运行 期间遇到异常。我该如何解决?

hudson.remoting.ProxyException: groovy.lang.MissingPropertyException: No such property: GITHUB_TOKEN for class: WorkflowScript
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
    at org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:355)
    at org.kohsuke.groovy.sandbox.GroovyInterceptor.onGetProperty(GroovyInterceptor.java:68)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:355)
    at org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:353)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:357)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:333)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
    at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
    at WorkflowScript.downloadGitAssets(WorkflowScript:402)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
    at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
    at sun.reflect.GeneratedMethodAccessor450.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
    at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
    at com.cloudbees.groovy.cps.Next.step(Next.java:83)
    at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:174)
    at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:163)
    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access[=12=]1(SandboxContinuable.java:18)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:185)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:400)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access0(CpsThreadGroup.java:96)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:312)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:276)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.call(CpsVmExecutorService.java:67)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService.run(SingleLaneExecutorService.java:139)
    at jenkins.util.ContextResettingExecutorService.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService.run(ImpersonatingExecutorService.java:68)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

我猜你错过了 '' credentialsId

应该是这样的

withCredentials([string(credentialsId: 'YOUR_CREDENTIAL_ID', variable: 'GITHUB_TOKEN')] 然后使用 $GITHUB_TOKEN.

在任何命令中访问令牌

或者另一种方法是像这样在环境部分中包含凭据

pipeline {
  environment {

    GITHUB_TOKEN = credentials('YOUR_CREDENTIAL_ID')

  }
}

并访问 jenkisnfile 中任何地方的 $GITHUB_TOKEN 如果你使用这个方法那么我猜你不需要这个功能 def withGithubCredentials(credentialsId, body)

所以你可以这样使用它

pipeline {
  environment {

    GITHUB_TOKEN = credentials('YOUR_CREDENTIAL_ID')

  }
}


def downloadGitAssets(String owner, String repo_name, String tag, String assert_name) {
    dir("scripts") {
        withGithubCredentials(body){
            return sh(
                    script: "./git_assets_download.sh ${GITHUB_TOKEN} ${owner} ${repo_name} ${tag} ${assert_name}",
                    returnStdout: true,
            ).trim()
        }
    }
}

def withGithubCredentials(body) {
    withCredentials([
            string(
                    credentialsId: 'YOUR_CREDENTIAL_ID',
                    variable: 'GITHUB_TOKEN'
            ),
    ]) {
        body()
    }
}