【问题标题】:How to pass private repository credentials to maven docker image when using Google Cloud Build使用 Google Cloud Build 时如何将私有存储库凭据传递给 maven docker 映像
【发布时间】:2019-09-15 19:22:36
【问题描述】:

我正在尝试使用 Google Cloud Build 来构建我的 Java 应用。它允许使用所谓的云构建器——不同构建器的 docker 镜像。我正在使用 Maven。所以问题是我必须使用私有存储库(工件)来部署工件。此代表受密码保护,我不知道如何将这些凭据传递给 GC maven docker 容器。

我看到唯一可能的方法是:

  1. 运行 shell 脚本,该脚本将更新 maven 容器 settings.xml,如下所示:

    <servers>
        <server>
            <id>myRepoName</id>
            <username>${server.username}</username>
            <password>${server.password}</password>
        </server>
    </servers>
    
  2. 在 cloudbuild.yml 中设置环境变量

还有其他优雅的方法来实现我想要的吗?

【问题讨论】:

  • 据我所知,您的存储库需要使用 SSH 密钥进行访问控制。这是 GitHub 私有存储库的示例。 cloud.google.com/cloud-build/docs/access-private-github-repos
  • @JohnHanley 我认为 OP 是在询问 Maven 存储库而不是 github 中的 git 存储库。
  • @KarthikeyanVaithilingam - OP 正在询问 Cloud Build。我的链接是访问私有存储库的示例。
  • @JohnHanley 您的链接提供了访问 github 私有存储库的示例,但 OP 需要访问私有 Artifactory 存储库。
  • 你也可以看看this

标签: maven docker google-cloud-platform google-cloud-build docker-maven-plugin


【解决方案1】:

我通过以下方式解决了这个问题:

  1. 创建一个 Google Cloud Storage 存储桶并上传您想要的 settings.xml。我正在使用 GitHub Packages,关注 their documentation

  2. 使用以下内容设置您的 cloudbuild.yaml:

steps:
  - name: gcr.io/cloud-builders/gsutil
    args: ['cp', 'gs://ci-maven/settings.xml', 'settings.xml']
  - name: maven:3.6.3-jdk-11-openj9
    entrypoint: 'mvn'
    args: ['--settings', '/workspace/settings.xml', 'install']
images: ['gcr.io/schemata-np/scheduler']

首先,它将 settings.xml 复制到当前目录 (/workspace)。然后,直接使用 Docker Maven 镜像,我们将 --settings /workspace/settings.xml 添加到我们的 args 以指定 settings.xml 位置。从那里,Google Cloud Build 能够提取我的私有 GitHub 包以正确安装我的项目。

可能可以在第一步中复制到/usr/share/maven/ref/ 以允许默认的 Maven Docker 行为,但我无法让它工作。如果有人知道,请告诉我!

基于this answer to a slightly different question about caching artifactsGoogle Cloud Build documentation

【讨论】:

    猜你喜欢
    • 2022-09-13
    • 2015-09-03
    • 1970-01-01
    • 2021-08-03
    • 2019-04-07
    • 1970-01-01
    • 2020-10-08
    • 2021-03-26
    • 2020-01-17
    相关资源
    最近更新 更多