【问题标题】:Unable to push image to GCR from Jenkins Pipeline无法从 Jenkins Pipeline 将图像推送到 GCR
【发布时间】:2018-05-04 03:32:32
【问题描述】:

我正在运行 Jenkins 服务器(在 docker 容器内)的谷歌云中运行虚拟机。我正在尝试为我的应用程序构建一个 Docker 映像,并使用 Jenkins 管道将其推送到 Google Container Registry。

  1. 我安装了所有必需的 Jenkins 插件: Google OAuth 凭据插件, Docker管道插件, Google Container Registry 身份验证插件

  2. 创建了一个具有存储管理员和对象查看者角色的服务帐户 + 密钥。已下载 json 文件。

  3. 使用 google 项目名称作为 id 和 json 键在 Jenkins 中创建了一个凭据。

  4. 我的构建管道代码如下所示:

    stage('Build Image') {
        app = docker.build("<gcp-project-id>/<myproject>")
    }
    
  5. 我的构建管道代码如下所示:

    stage('Push Image') {
    docker.withRegistry('https://us.gcr.io', 'gcr:<gcp-project-id>') {
        app.push("${commit_id}")
        app.push("latest")
    }
    

    }

但是,构建在最后一步失败并出现此错误:

unauthorized: You don't have the needed permissions to perform this operation, and you may have invalid credentials. To authenticate your request, follow the steps in: https://cloud.google.com/container-registry/docs/advanced-authentication

我花了几个小时试图弄清楚这一点。任何帮助将不胜感激!

【问题讨论】:

    标签: jenkins-pipeline google-container-registry


    【解决方案1】:

    我也有同样的问题。我发现 Jenkins 似乎没有使用这些凭据:在使用情况下,它显示“此凭据尚未被记录为在任何地方使用过。” .当与 gcloud util 一起使用时,服务帐户和密钥工作正常,所以问题出在 Jenkins 的某个地方。

    【讨论】:

    • 谢谢。您能否分享您的代码 sn-p 以显示构建和推送步骤?我在 GCP 机器上将 Jenkins 作为 Docker 容器运行。
    • 我在 GCP 之外运行 jenkins。对于重要部分,我的管道看起来像这样:pastebin.com/mPkPTSbL
    • 谢谢。我必须添加一个 docker login 步骤才能工作。 google auth 插件根本不起作用。
    • 已确认。我也让它与一个额外的 docker login 步骤一起工作。插件运气不好。
    • 如果其他人对此有疑问,您是否可以添加 docker 登录步骤的详细信息作为答案?
    【解决方案2】:

    在GCP中创建一个服务账号,有权限推送镜像,然后将凭证json文件复制并保存为Jenkins内部的凭证;在管道中调用凭证 id,如下所示,它应该将图像推送到 gcr

                withCredentials([file(credentialsId: 'gcr', variable: 'GC_KEY')]){
                  sh "cat '$GC_KEY' | docker login -u _json_key --password-stdin https://eu.gcr.io"
                  sh "gcloud auth activate-service-account --key-file='$GC_KEY'"
                  sh "gcloud auth configure-docker"
                  GLOUD_AUTH = sh (
                        script: 'gcloud auth print-access-token',
                        returnStdout: true
                    ).trim()
                  echo "Pushing image To GCR"
                  sh "docker push eu.gcr.io/${google_projectname}/${image_name}:${image-tag}"
              }

    另外我定义了一些上面用到的变量

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-21
      • 2020-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-22
      相关资源
      最近更新 更多