如何通过Jenkins凭据进行毕业? [英] How do I pass Jenkins credentials to gradle?

查看:105
本文介绍了如何通过Jenkins凭据进行毕业?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the jib Gradle plugin to create a docker image and push it to the Azure Container Registry. I've added username/password credentials to Jenkins so far and need to pass them to Gradle. Accessing or passing the credentials to Gradle, they get masked. Hope you can help me. Here're the code snippets:

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

我的用户名中有错字

推荐答案

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

I had a typo in the username. Passing Jenkins credentials as environment variables works as expected. Here's my code: build.gradle (jib configuration):

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

这篇关于如何通过Jenkins凭据进行毕业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆