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

时间:2018-01-09 15:41:49

标签: jenkins gradle jenkins-pipeline nexus

我需要从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)
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

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