首页 文章

使用GCP凭据的Gitlab Runner图像

提问于
浏览
1

我正在尝试教我的Gitlab Runner图像以从我的私有Docker Registry(在Google Cloud中运行的GCR)获取自定义构建器映像 .

What did not work out? 我创建了一个自定义的Gitlab Runner图像,并正确设置了ServiceAccount . 我开始处于非特权模式但是虫洞模式(通过 docker.sock ) . 在执行到该容器(基于 gitlab/gitlab-runner:v11.3.0 )时,我必须认识到我不能在那里执行任何docker命令(既不是root用户也不是gitlab用户) . gitlab-runner之后如何启动构建器容器远远超过我的认知能力 . ;)

# got started via eu.gcr.io/my-project/gitlab-runner:0.0.5 which got taught the GCR credentials

stages:
    - build

build:
    image: docker pull eu.gcr.io/my-project/gitlab-builder-docker:0.0.2
    stage: build
    script: 
        # only for test if I have access to private docker registry
        - docker pull eu.gcr.io/my-project/gitlab-builder-docker:0.0.1

What worked out? 根据此tutorial,您可以在 .gitlab-ci.yml 文件的 before_script 块中进行身份验证 . 结果很好 .

# got started via gitlab/gitlab-runner:v11.3.0 

stages:
    - build

before_script:
    - apk add --update curl python which bash
    - curl -sSL https://sdk.cloud.google.com | bash
    - export PATH="$PATH:/root/google-cloud-sdk/bin"
    - gcloud components install docker-credential-gcr
    - gcloud auth activate-service-account --key-file=/key.json
    - gcloud auth configure-docker --quiet

build:
    image: docker:18.03.1-ce
    stage: build
        # only for test if I have access to private docker registry
        - docker pull eu.gcr.io/my-project/gitlab-builder-docker:0.0.1

The Question 这意味着我必须在每次构建运行中执行此操作(安装gcloud和authenticate) - 我更愿意在gitlab-runner图像中完成此操作 . 你知道如何实现这个目标吗?

2 回答

  • 1

    最后,我找到了一种方法来完成这项工作 .

    教授香草gitlab-runner如何从您的私人GCR Docker Repo中取出

    GCP

    • IAM & Admin 中创建没有权限的服务帐户

    • 下载Json Key

    • Storage Browser 中添加权限

    • 选择存放图像的存储桶(例如eu.artifacts.my-project.appspot.com)

    • 向服务帐户授予权限 Storage Object Admin

    Local Docker Container

    • 启动一个 library/docker 容器并执行它(使用Docker Wormhole Pattern docker.sock volume mount)

    • 登录 GCR via(检查您的仓库的网址,在我的情况下,它位于欧洲,因此网址中的eu前缀) docker login -u _json_key --password-stdin https://eu.gcr.io < /etc/gitlab-runner/<MY_KEY>.json

    • 验证是否通过某些工作 docker pull <MY_GCR_IMAGE>

    • 复制〜/ .docker / config.json的内容

    Gitlab config.toml configuration

    • 将以下内容添加到config.toml文件中 [[runners]] environment = ["DOCKER_AUTH_CONFIG={ \"auths\": { \"myregistryurl.com:port\": { \"auth\": \"<TOKEN-FROM-DOCKER-CONFIG-FILE>\" } } }"]

    Vanilla Gitlab Runner Container

    • 像这样运行跑步者 docker run -it \ --name gitlab-runner \ --rm \ -v <FOLDER-CONTAININNG-GITLAB-RUNNER-CONFIG-FILE>:/etc/gitlab-runner:ro \ -v /var/run/docker.sock:/var/run/docker.sock \ gitlab/gitlab-runner:v11.3.0

    Your .gitlab-ci.yml file

    • 通过.gitlab-ci.yml验证完成的工作

    • 使用位于您私人网站的图片 GCP Container Registry

    教您的构建器图像如何推送到您的私人GCR Docker Repo

    GCP

    • 为您的服务帐户添加权限

    • 将权限 Storage Legacy Bucket Reader 授予 Storage Browser 中的服务帐户

    Custom Docker Builder Image

    • 将您的服务帐户密钥文件添加到您的自定义图像 FROM docker:18.03.1-ce ADD key.json /<MY_KEY>.json

    Your .gitlab-ci.yml file

    • 将以下脚本添加到 before_script 部分 docker login -u _json_key --password-stdin https://eu.gcr.io < /key.json

    最后的想法

    现在,vanilla gitlab-runner可以从您的私人GCR Docker Repo中提取自定义图像 . 此外,这些可拉动的自定义图像还能够与您的私人GCR Docker Repo交谈,例如,推送构建管道的结果图像 .

    那是非常复杂的事情 . 也许Gitlab将来会增强对这个用例的支持 .

  • 0

    你试过用谷歌cloudbuild吗?我遇到了同样的问题并解决了这个问题:

    echo ${GCR_AUTH_KEY} > key.json
    gcloud auth activate-service-account --key-file key.json
    gcloud auth configure-docker
    
    gcloud builds submit . --config=cloudbuild.yaml --substitutions _CI_PROJECT_NAME=$CI_PROJECT_NAME,_CI_COMMIT_TAG=${CI_COMMIT_TAG},_CI_PROJECT_NAMESPACE=${CI_PROJECT_NAMESPACE}
    

    cloudbuild.yaml:

    steps:
     - name: gcr.io/cloud-builders/docker
        id: builder
        args: 
          - 'build'
          - '-t'
          - 'eu.gcr.io/projectID/$_CI_PROJECT_NAMESPACE-$_CI_PROJECT_NAME:$_CI_COMMIT_TAG'
          - '.'
     - name: gcr.io/cloud-builders/docker
        id: tag-runner-image
        args: 
          - 'tag'
          - 'eu.gcr.io/projectID/$_CI_PROJECT_NAMESPACE-$_CI_PROJECT_NAME:$_CI_COMMIT_TAG'
          - 'eu.gcr.io/projectID/$_CI_PROJECT_NAMESPACE-$_CI_PROJECT_NAME:latest'
    images:
     - 'eu.gcr.io/projectID/$_CI_PROJECT_NAMESPACE-$_CI_PROJECT_NAME:$_CI_COMMIT_TAG'
     - 'eu.gcr.io/projectID/$_CI_PROJECT_NAMESPACE-$_CI_PROJECT_NAME:latest'
    

    只需使用google / cloud-sdk:alpine作为gitlab-ci阶段的图像

相关问题