【问题标题】:How do I invoke credentials stored in Jenkins to deploy to a custom Nexus from within a Jenkinsfile?如何调用存储在 Jenkins 中的凭据以从 Jenkinsfile 中部署到自定义 Nexus?
【发布时间】:2017-12-14 16:30:10
【问题描述】:

我有一个用于项目相关实用程序 jar 的自定义 Nexus。我有一个 Jenkins 来构建这些罐子。

我想在项目中使用 gradle 和 Jenkinsfile 来定义管道。作为最后一个阶段,我希望构建作业将工件部署到我的 Nexus。

由于我不想在我的源存储库中使用 Nexus 的凭据(因此既不在 gradle 脚本中也不在 Jenkinsfile 中),我在 Jenkins Credentials 插件中设置了一个凭据条目,其中包含用于连结。

如何从 Jenkinsfile 中引用这些凭据,以便可以使用它们部署到 Nexus?

目前,我的部署阶段如下所示:

stage('Publish') {
    sh "gradle upload"
}

我可以为此目的使用 gradle 吗?我知道我可以在本地使用它,但我可以在 Jenkinsfile 中使用不同的命令。

在 gradle 中,我使用它来定义任务:

def nexusUser = "$System.env.NEXUS_USER"
def nexusPass = "$System.env.NEXUS_PASS"

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "http://nexus.example.com/repository/maven-releases/") {
                authentication(userName: nexusUser, password: nexusPass)
            }
        }
    }
}

(我在本地将用户和密码设置为环境变量。当然,这不是 Jenkins 的选项。)

【问题讨论】:

  • 为什么使用环境变量不起作用?
  • 完全同意您可以在 jenkins 安装中包含环境变量。
  • 你也可以使用Jenkins提供的全局凭证
  • @mkobit:环境变量肯定会起作用。它在本地也是如此。我只是不想将凭据放入对其他人可见的环境变量中。但也许有一种方法可以用我看不到的环境变量来解决这个问题,因此我犹豫要不要使用它们。 @Carlos:您能举例说明Global Credentials provided by Jenkins 的含义吗?你能把它作为一个答案吗?感谢您的快速回复!
  • @inedible 如果你使用withCredentials(){} env vriabels 将仅在其块内可见{}

标签: jenkins gradle jenkins-pipeline nexus


【解决方案1】:

您可以在~/.gradle/gradle.properties中设置外部变量 nexusUser=superuser nexusPass=superpassword

【讨论】:

  • 是的,我猜这是最优解,记住需要在jenkins用户下更改(实际上是在执行CI)
【解决方案2】:

您可以使用withCredentials(){} 步骤注入仅在{} 块内可见的环境变量。

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-04
  • 2017-08-18
  • 2014-07-26
  • 2014-12-28
  • 1970-01-01
  • 2013-05-31
  • 1970-01-01
相关资源
最近更新 更多