小编典典

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

jenkins

我正在使用三角帆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}"
            }
        }
...

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


阅读 245

收藏
2020-07-25

共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"
            }
        }
...
2020-07-25