我想在声明性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获取给定的credentials
。 USERPASS
变量为null
。