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



我需要从 Jenkins 声明式管道将工件上传到 Nexus。有一个我无法使用的 Nexus 插件,因为它不允许发布快照。因此,我必须直接通过 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"
}

最新更新