如何将 Jenkins 凭据传递给 gradle? [英] How do I pass Jenkins credentials to gradle?

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

问题描述

我正在使用 jib 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 配置):

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

Jenkins 文件:

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(jib 配置):

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

Jenkins 文件:

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

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

这篇关于如何将 Jenkins 凭据传递给 gradle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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