【问题标题】:What is the proper way to manage credentials in Jenkins shared libraries?在 Jenkins 共享库中管理凭据的正确方法是什么?
【发布时间】:2019-12-11 18:51:51
【问题描述】:

我已经开始为我的团队管理多个 Jenkins 流水线。在这些管道中,有很多重复的代码。也就是说,我已经开始更好地遵循Jenkins documentation 并将重复的代码移动到共享库中,以便在多个作业中使用。

我面临的问题是决定如何管理作业和共享库之间的凭据传递。许多库将处理通过不同 Web 服务器交换数据。与这些 Web 服务器的每次交互都需要用户名 + 密码或一些 API 令牌来进行身份验证/授权。

我知道可行的解决方案是让共享库中的每个函数都将凭据作为参数。有了它,我可以在我的 Jenkinsfile 中使用 withCredentials(...) { } 指定我的凭据。虽然我相信这会奏效,但我想知道这是否是最佳解决方案。

由于每个 API(和相应的库)都将绑定到一个凭据,我想知道我是否可以以某种安全方式将库链接到单个凭据。下面是两个管道示例,说明我知道什么会起作用以及我想探索什么。

我知道会起作用的:

stage('promote') {
    steps {
        // known working solution
        interactWithApi1(content1, credentials)
        interactWithApi2(content2, credentials)
    }
}

我想从可行性的角度探讨:

stage('promote') {
    steps {
        // something I'm inclined to look into
        // where these library(s) already know the credentials
        interactWithApi1(content1)
        interactWithApi2(content2)
    }
}

【问题讨论】:

  • 您不会编写“每个服务器的库”。您很可能只有一个库。给定的服务器没有“各自的”库。
  • 如果没有看到一些显示代码基本结构的示例代码,很难回答这个问题。纯粹的文字描述是模糊的。
  • @MaratC,我的意思是说有 API 而不是服务器。我已经更新了语言。
  • @zett42,我添加了一些示例管道代码以更好地说明我想要探索的内容。

标签: jenkins jenkins-pipeline shared-libraries credentials


【解决方案1】:

这样的事情对我们有用:

//vars/interactWithAws.groovy in your library

def call(Map params) {
   withAWS(credentials: 'jenkins-aws-credentials') { 
       s3upload bucket: "myBucket", 
           file: params.file // etc.
   }

凭证在主 Jenkins > 凭证中定义。

它对你也有用吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-14
    • 2020-08-19
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 1970-01-01
    相关资源
    最近更新 更多