简体   繁体   English

Gitlab 带有 Kaniko 和 gcloud 的跑步者 sdk

[英]Gitlab runner with Kaniko and gcloud sdk

I want to build docker images on Gitlab and use Kaniko for it.我想在 Gitlab 上构建 docker 图像并使用 Kaniko。

It's working great when I just have Dockerfile and code from repo.当我只有 Dockerfile 和来自 repo 的代码时,它工作得很好。 Problem starts when before building I want to access GCP Secret Manager and get values for this build.当在构建之前我想访问 GCP Secret Manager 并获取此构建的值时,问题就开始了。

We are building images directly on Gitlab and storing them in GCR.io.我们直接在 Gitlab 上构建图像并将它们存储在 GCR.io 中。

Below example gitlab-ci.yml config.下面的示例 gitlab-ci.yml 配置。 When we are using image: Docker it will work as we can use curl etc and install cloud sdk. But with Kaniko it's not possible.当我们使用图像时:Docker它将工作,因为我们可以使用 curl 等并安装云 sdk。但是对于 Kaniko,这是不可能的。

dev-build-docker:
  stage: build-docker-image
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
  environment: Develop
  only:
    changes:
      - xxxxxxxxxxxx
    refs:
      - develop
  services:
    - docker:dind
  before_script:
    - source vars/.variables
    - echo $CICD_SA_KEY > ${CI_PROJECT_DIR}/service_key.json
    - export GOOGLE_APPLICATION_CREDENTIALS=${CI_PROJECT_DIR}/service_key.json
  script:
    - apk --no-cache add curl
    - apk add bash
    - curl https://sdk.cloud.google.com > install.sh
    - apt install -y python3
    - ./install.sh --disable-prompts
    - export PATH=$PATH:/root/google-cloud-sdk/bin
    - gcloud auth activate-service-account --key-file ${CI_PROJECT_DIR}/service_key.json
    - mkdir creds
    - gcloud secrets versions access latest --project=$projectid_dev --secret=xxxxxxxx > creds/dbpass
    - DB_PASS=$(cat creds/dbpass)
    - gcloud secrets versions access latest --project=$projectid_dev --secret=xxxxxxxxxx-key > creds/creds.2.json
    - gcloud secrets versions access latest --project=$projectid_dev --secret=zzzzzzzzzzzzzz-key > creds/creds.1.json

    # end of gcloud
    - /kaniko/executor --context "$(pwd)" --dockerfile "$(pwd)/Dockerfile" --destination eu.gcr.io/$projectid_dev/xxxxxxxxxxxx:$TAG --destination eu.gcr.io/$projectid_dev/xxxxxxxxxxxx:latest --build-arg NODE_ENV=production --build-arg DB_PASS=$DB_PASS

Kaniko is using busybox and I don't see a way to install gcp sdk and access secrets. Kaniko 正在使用 busybox,我看不到安装 gcp sdk 和访问机密的方法。 Did anyone managed to use gcloud commands before Kaniko executor?在 Kaniko executor 之前,有人设法使用 gcloud 命令吗?

Because of the nature of kaniko , the approach you are looking for is not possible.由于kaniko的性质,您正在寻找的方法是不可能的。 The mainly reason as you have already noticed is that busybox has a limited tool set and is not possible to install gcloud with its dependencies.正如您已经注意到的,主要原因是 busybox 的工具集有限,无法安装gcloud及其依赖项。

For example, let's say you download the archive version of gcloud and you create your own kaniko version copying the Cloud SDK binaries but then you notice that you will also need python which implies to compile it and this leads you to need more libraries and dependencies which at the end seems not to be too convenient and making kaniko a very big image.例如,假设您下载了gcloud的存档版本,并创建了自己的kaniko版本,复制了 Cloud SDK 二进制文件,但随后您注意到您还需要 python,这意味着要编译它,这导致您需要更多的库和依赖项最后似乎不太方便,让kaniko成为一个非常大的形象。

So at the end seems like the best option is to use the Docker in Docker approach.所以最后似乎最好的选择是在 Docker 方法中使用 Docker 。 Another workaround which implies an additional step is to build your own kaniko image which contains your secrets on it, for example:另一种解决方法需要额外的步骤,即构建您自己的 kaniko 图像,其中包含您的秘密,例如:

FROM gcr.io/google.com/cloudsdktool/cloud-sdk as secrets
WORKDIR /creds
COPY ./key.json .
RUN gcloud auth activate-service-account --key-file=/secrets/key.json
WORKDIR /secrets
RUN gcloud secrets versions access latest --project=PROJECT_ID --secret=SECRET > creds_2.json


FROM gcr.io/kaniko-project/executor:debug
WORKDIR /build
COPY --from=secrets /secrets/creds_2.json .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM