Jenkins : use withCredentials in global environment section

后端 未结 4 1505
星月不相逢
星月不相逢 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}"
    }
    
    0 讨论(0)
  • 2021-02-13 02:28

    You can use credentials helper method of the environment section. For "Username and passwrd" type of credentials it assigns 2 additional environment variables. Example:

    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}"
              }
          }
      }"""
    }
    

    Read more

    0 讨论(0)
  • 2021-02-13 02:32

    After a lot of search (and struggle), i came up with an easy workaround:

    As better explained in the jenkins docs for Handling Credentials, when injecting a usernamePassword type credential into an environment variable named VAR_NAME, jenkins automatically generates two other variables ending with _USR and _PSW respectively for usernameVariable and passwordVariable parameters.

    What i did was to inject my variables with the values from both USR and PSW new variables.

    In @Giel Berkers case, it should be something like this:

    environment {
        DOCKER_IMAGE_NAME = "magento2_website_sibo"
        COMPOSER_REPO_MAGENTO_CREDENTIAL = credentials('COMPOSER_REPO_MAGENTO')
        COMPOSER_AUTH = """{
            "http-basic": {
                "repo.magento.com": {
                    "username": "${COMPOSER_REPO_MAGENTO_CREDENTIAL_USR}",
                    "password": "${COMPOSER_REPO_MAGENTO_CREDENTIAL_PSW}"
                }
            }
        }""";
    }
    
    0 讨论(0)
  • 2021-02-13 02:41

    Here is how you can accomplish that

    pipeline {
        agent any
        stages {
            stage('first') {
                steps {
                    script {
                        withCredentials([usernamePassword(credentialsId: 'COMPOSER_REPO_MAGENTO', passwordVariable: 'MAGE_REPO_PASS', usernameVariable: 'MAGE_REPO_USER')]) {
                            def user = env.MAGE_REPO_USER
                            def password = env.MAGE_REPO_PASS
                            //Initializing a global variable. Notice there is no def here 
                            composerAuth = """{
                                "http-basic": {
                                    "repo.magento.com": {
                                        "username": "${user}",
                                        "password": "${password}"
                                    }
                                }
                            }"""
                        }
                    }
                }
            }
            stage('second') {
                steps {
                    script {
                        println composerAuth
                    }
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题