詹金斯:密码保护阶段

时间:2018-08-06 20:52:21

标签: jenkins-pipeline

我有一个Jenkinsfile,我需要用密码保护一个舞台。管道将提示用户输入密码,如果与“凭据”下的密码匹配,则将执行该阶段。

我可以使用输入密码

def password = input message: 'Please enter the password', parameters: [string(defaultValue: '', description: '', name: 'password')]

以及何时进行

sh 'echo $password'

显示正确的值,但是与

结合使用时,所有操作都会失败
withCredentials([string(credentialsId: '9234419f-01e2-542d-b0e9-5ead17275eac', variable: 'CREDPASS')]) 

像semple测试一样

script
{ 
  def password = input message: 'Please enter the password', parameters: [string(defaultValue: '', description: '', name: 'password')]
  withCredentials([string(credentialsId: '9655419f-01e2-442d-b0e9-5ead17375eac', variable: 'CREDPASS')])
  {
    sh "echo ${password}"
  }
}

显示null****,我也尝试过$password == $CREDPASS进行确认,但没有显示相同的:(

任何想法都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

不确定,我是否正确回答了您的问题,但是

withCredentials([string(credentialsId: '9655419f-01e2-442d-b0e9-5ead17375eac', variable: 'CREDPASS')])
{
    // ...
}

不会将密码存储在CREDPASS中的Jenkinsfile中,而是存储在env.CREDPASS中。

因此,与其比较password == CREDPASS(此处不使用$),不如使用password == env.CREDPASS来查看两个密码是否匹配:

def password = input message: 'Please enter the password', parameters: [string(defaultValue: '', description: '', name: 'password')]
withCredentials([string(credentialsId: '9655419f-01e2-442d-b0e9-5ead17375eac', variable: 'CREDPASS')]) {
    if (password == env.CREDPASS) {
        echo "passwords match"
    } else {
        echo "passwords do not match"
    }
}

仅当您想在shell命令中使用变量时(例如,与sh一起使用),您才能通过$CREDPASS访问它,因为env.CREDPASS将作为变量传递给shell命令。外壳环境变量。

此外,詹金斯将始终输出****而不是您的密码,因为它将被隐藏在您的日志中。因此,当您看到****时,便知道withCredentials确实有效!