如何将用户名密码传递给 Gradle 以从 Jenkins 管道将工件上传到 Nexus

How to pass username password to Gradle to upload artifacts to Nexus from a Jenkins pipeline

我需要从 Jenkins 声明式管道将工件上传到 Nexus。有一个 Nexus plugin 我无法使用,因为它不允许发布 SNAPSHOT。因此我必须通过 Gradle 上传任务直接上传它们。问题是我从 Nexus 收到 401 错误代码,似乎 Nexus 用户和密码没有从管道传递到 ./gradlew upload 任务。

这是我当前的配置:

管道

withCredentials([usernamePassword(credentialsId: 'asdf23432-fe69-1234-2222-ffa65yteec2c', passwordVariable: 'NEXUS_PASSWORD', usernameVariable: 'NEXUS_USER')]) {
    sh "./gradlew upload --debug"
}

Gradle:在 ext 部分,我尝试从 withCredentials 步骤访问 NEXUS_USER 和 NEXUS_PASSWORD 变量。

ext{
    nexusUser = System.properties['NEXUS_USER']
    nexusPassword = System.properties['NEXUS_PASSWORD']
}

uploadArchives {
    repositories {
        mavenDeployer {
            snapshotRepository(url: 'http://somerepo'){
                authentication(userName: nexusUser, password: nexusPassword)
            }
        }
    }
}

您可以使用以下方法将变量作为系统属性传递:

withCredentials([usernamePassword(credentialsId: 'asdf23432-fe69-1234-2222-ffa65yteec2c', passwordVariable: 'NEXUS_PASSWORD', usernameVariable: 'NEXUS_USER')]) {
    sh "./gradlew -DNEXUS_PASSWORD=$NEXUS_PASSWORD -DNEXUS_USER=$NEXUS_USER upload --debug"
}