我有一个具有多个阶段的 Jenkins 管道,所有阶段都需要相同的环境变量,我像这样运行:
script {
withCredentials([usernamePassword(credentialsId: 'COMPOSER_REPO_MAGENTO', passwordVariable: 'MAGE_REPO_PASS', usernameVariable: 'MAGE_REPO_USER')]) {
def composerAuth = """{
"http-basic": {
"repo.magento.com": {
"username": "${MAGE_REPO_USER}",
"password": "${MAGE_REPO_PASS}"
}
}
}""";
// do some stuff here that uses composerAuth
}
}
我不想重新声明composerAuth
每次,所以我想将凭据存储在全局变量中,这样我就可以执行以下操作:
script {
// do some stuff here that uses global set composerAuth
}
我尝试将其放入环境部分:
environment {
DOCKER_IMAGE_NAME = "magento2_website_sibo"
withCredentials([usernamePassword(credentialsId: 'COMPOSER_REPO_MAGENTO', passwordVariable: 'MAGE_REPO_PASS', usernameVariable: 'MAGE_REPO_USER')]) {
COMPOSER_AUTH = """{
"http-basic": {
"repo.magento.com": {
"username": "${MAGE_REPO_USER}",
"password": "${MAGE_REPO_PASS}"
}
}
}""";
}
}
但是(像我这样的菜鸟)那是行不通的。那么,设置具有凭据的全局可访问变量但只需声明一次的最佳方法是什么?
您可以使用credentials
的辅助方法environment
部分。对于“用户名和密码”类型的凭据,它分配 2 个附加环境变量。例子:
environment {
MAGE_REPO_CREDENTIALS = credentials('COMPOSER_REPO_MAGENTO')
COMPOSER_AUTH = """{
"http-basic": {
"repo.magento.com": {
"username": "${env.MAGE_REPO_CREDENTIALS_USR}",
"password": "${env.MAGE_REPO_CREDENTIALS_PSW}"
}
}
}"""
}
阅读更多 https://jenkins.io/doc/book/pipeline/syntax/#environment
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)