Jenkins : use withCredentials in global environment section

后端 未结 4 1504
星月不相逢
星月不相逢 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: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
                    }
                }
            }
        }
    }
    

提交回复
热议问题