Jenkins : use withCredentials in global environment section

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

提交回复
热议问题