Jenkins : use withCredentials in global environment section

后端 未结 4 1514
星月不相逢
星月不相逢 2021-02-13 01:58

I have a Jenkins pipeline with multiple stages that all require the same environment variables, I run this like so:

script {
    withCredentials([usernamePasswor         


        
4条回答
  •  一个人的身影
    2021-02-13 02:22

    I found this and it is helpful: Source: https://wiki.jenkins.io/display/JENKINS/Credentials+Binding+Plugin

       // Basic example
    withCredentials([usernamePassword(credentialsId: 'amazon',
                         usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
        //available as an env variable, but will be masked if you try to print it out any which way
        sh 'echo $PASSWORD'
        echo "${env.USERNAME}"
    }
    
    // You can also request multiple credentials in a single call
    withCredentials([usernamePassword(credentialsId: 'amazon',
                         usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD'),
                     string(credentialsId: 'slack-url',
                         variable: 'SLACK_URL'),]) {
        sh 'echo $PASSWORD'
        echo "${env.SLACK_URL}"
    }
    
    // Older code might not use the new syntax (usernamePassword, string, ...) yet, and directly call the class:
    withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'amazon',
                      usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
        //available as an env variable, but will be masked if you try to print it out any which way
        sh 'echo $PASSWORD'
        echo "${env.USERNAME}"
    }
    

提交回复
热议问题