如何通过Jenkins凭据进行毕业?

时间:2019-10-25 16:16:40

标签: jenkins gradle jenkins-pipeline

我正在使用三角帆Gradle插件创建docker映像并将其推送到Azure容器注册表。 到目前为止,我已经向Jenkins添加了用户名/密码凭据,并且需要将它们传递给Gradle。 访问或将凭据传递给Gradle,它们会被屏蔽。希望您能够帮助我。 这是代码段:

build.gradle(臂架配置):

jib {
    to {
        image = "myacr.azurecr.io/" + project.name
        tags = ["latest"]
        auth {
            // retrieve from Jenkins
            username System.properties['ACR_CREDENTIALS_USR']
            password System.properties['ACR_CREDENTIALS_PSW']
        }
    }
    container {
        jvmFlags = ["-Xms512M",  "-Xmx1G"]
        ports = ["5000/tcp", "8080/tcp"]
    }    
}

Jenkinsfile:

pipeline {
...
    environment {
        ACR_CREDENTIALS = credentials('myproject-acr') 
    }

    stages {
        ...
        stage('Push Docker Image to Registry') {
            steps {
                sh "./gradlew jib -PACR_CREDENTIALS_USR=${env.ACR_CREDENTIALS_USR} -PACR_CREDENTIALS_PSW=${env.ACR_CREDENTIALS_PSW}"
            }
        }
...

编辑: 我的用户名中有错字

1 个答案:

答案 0 :(得分:1)

我在用户名中输入错误。将Jenkins凭据作为环境变量传递可以按预期方式进行。这是我的代码: build.gradle(臂架配置):

jib {
    to {
        image = "myacr.azurecr.io/" + project.name
        tags = ["latest"]
        auth {
            // retrieve from Jenkins
            username "${System.env.ACR_CREDENTIALS_USR}"
            password "${System.env.ACR_CREDENTIALS_PSW}"
        }
    }
    container {
        jvmFlags = ["-Xms512M",  "-Xmx1G"]
        ports = ["5000/tcp", "8080/tcp"]
    }    
}

Jenkinsfile:

pipeline {
...
    environment {
        ACR_CREDENTIALS = credentials('myproject-acr') 
    }

    stages {
        ...
        stage('Push Docker Image to Registry') {
            steps {
                sh "./gradlew jib"
            }
        }
...