Jenkins管道:共享库无法解析凭据Id

时间:2019-01-28 05:46:35

标签: jenkins groovy jenkins-pipeline jenkins-declarative-pipeline

我想在声明性Jenkins管道中使用共享库。该库提供了从Artifactory工件中检索信息的功能。为此,我使用以下代码:

class ArtifactInfoResolver implements Serializable {

    private org.jenkinsci.plugins.workflow.cps.CpsScript script
    ...


    ArtifactInfoResolver(org.jenkinsci.plugins.workflow.cps.CpsScript script) {
        this.script = script
    }

    void retrieveArtifacts(String url, String credentials, String repoName, String pathRegex) {

        script.node {
            script.withCredentials([script.usernameColonPassword(credentialsId: credentials, variable: 'USERPASS')]) {

                String cmd = "curl " +
                        "-u ${USERPASS} " +
                        ...

                        String responseJson = script.sh(returnStdout: true, script: cmd).trim()
                        ...
                    }
                }
            }
        }
    }
}

声明性管道的相关部分:

pipeline {
...

    stages {        
        stage('Gather information') {
            steps {
                script {
                    snapshotResolver = new ArtifactInfoResolver(this)
                    snapshotResolver.retrieveArtifacts(
                                "${env.RB_ARTIFACTORY_URL}",
                                'BuildUserCredentials',
                                "<repoName>",
                                "<pathRegex>")
                    ...
                }
            }
        }
        ...
    }
}

问题在于,retrieveArtifacts方法无法从Jenkins Keystore获取给定的credentialsUSERPASS变量为null

0 个答案:

没有答案