在带有Kubernetes Executor&;非特权运行者的GitLab配置项中使用paktool.io/CloudNativeBuildPack(Cnb)(没有包CLI和amp;docker) [英] Use Paketo.io / CloudNativeBuildpacks (CNB) in GitLab CI with Kubernetes executor & unprivileged Runners (without pack CLI & docker)
问题描述
我们希望以最简单的方式使用Paketo.io/CloudNativeBuildpacks (CNB)GitLab CI。我们的GitLab设置使用AWS EKS群集,无权限的GitLab CI运行人员利用the Kubernetes executor。我们也不希望通过using Docker in our builds引入安全风险。因此,我们既没有公开主机的/var/run/docker.sock
,也不想使用docker:dind
。
我们找到了一些关于如何将Paketo与GitLab配置项配合使用的指南,如下所示https://tanzu.vmware.com/developer/guides/gitlab-ci-cd-cnb/。但正如标题Use Cloud Native Buildpacks with GitLab in GitLab Build Job WITHOUT Using the GitLab Build Template
下面所述,该方法依赖于Docker和PackCLI。我们尝试在我们的.gitlab-ci.yml
中类似于此,如下所示:
image: docker:20.10.9
stages:
- build
before_script:
- |
echo "install pack CLI (see https://buildpacks.io/docs/tools/pack/)"
apk add --no-cache curl
(curl -sSL "https://github.com/buildpacks/pack/releases/download/v0.21.1/pack-v0.21.1-linux.tgz" | tar -C /usr/local/bin/ --no-same-owner -xzv pack)
build-image:
stage: build
script:
- pack --version
- >
pack build $REGISTRY_GROUP_PROJECT/$CI_PROJECT_NAME:latest
--builder paketobuildpacks/builder:base
--path .
但如上所述,我们的设置不支持docker,并且在日志中最终出现以下错误:
...
$ echo "install pack CLI (see https://buildpacks.io/docs/tools/pack/)" # collapsed multi-line command
install pack CLI (see https://buildpacks.io/docs/tools/pack/)
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/community/x86_64/APKINDEX.tar.gz
(1/4) Installing brotli-libs (1.0.9-r5)
(2/4) Installing nghttp2-libs (1.43.0-r0)
(3/4) Installing libcurl (7.79.1-r0)
(4/4) Installing curl (7.79.1-r0)
Executing busybox-1.33.1-r3.trigger
OK: 12 MiB in 26 packages
pack
$ pack --version
0.21.1+git-e09e397.build-2823
$ pack build $REGISTRY_GROUP_PROJECT/$CI_PROJECT_NAME:latest --builder paketobuildpacks/builder:base --path .
ERROR: failed to build: failed to fetch builder image 'index.docker.io/paketobuildpacks/builder:base': Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
Cleaning up project directory and file based variables 00:01
ERROR: Job failed: command terminated with exit code 1
有没有关于如何在GitLab Kubernetes运行器中不使用Docker的情况下将Paketo Buildpack与GitLab CI一起使用的想法(这似乎是一种最佳实践)?我们也不希望我们的设置变得复杂-例如,通过添加kpack。
推荐答案
tldr;
在.gitlab-ci.yml
here's a fully working example)中直接使用生成包的生命周期:
image: paketobuildpacks/builder
stages:
- build
# We somehow need to access GitLab Container Registry with the Paketo lifecycle
# So we simply create ~/.docker/config.json as stated in https://stackoverflow.com/a/41710291/4964553
before_script:
- mkdir ~/.docker
- echo "{"auths":{"$CI_REGISTRY":{"username":"$CI_REGISTRY_USER","password":"$CI_JOB_TOKEN"}}}" >> ~/.docker/config.json
build-image:
stage: build
script:
- /cnb/lifecycle/creator -app=. $CI_REGISTRY_IMAGE:latest
详细信息:直接使用生命周期(&Q;)
关于此主题的讨论正在进行中。特别是https://github.com/buildpacks/pack/issues/564和https://github.com/buildpacks/pack/issues/413#issuecomment-565165832。如下所述:
如果您希望在CI(而不是本地)中构建映像,我建议 您可以直接使用生命周期来实现这一点,这样您就不需要 多克。这里有个例子:指向示例的链接断开,但它引用了how to use buildpacks in a Kubernetes environment上的Tekton实现。在这里,我们可以初步了解斯蒂芬·莱文所说的
"to use the lifecycle directly"
。在它里面,关键点是command: ["/cnb/lifecycle/creator"]
的用法。这就是每个人都在谈论的生命周期!可以找到有关此命令的很好的文档in this CNB RFC。选择好的映像:paketobuildpack/builder:base
那么如何开发一个工作的.gitlab-ci.yml
呢?让我们从简单的开始吧。挖掘into the Tekton implementation,您会发现LIFECYCLE命令是在BUILDER_IMAGE
中定义的环境中执行的,该环境本身记录为The image on which builds will run (must include lifecycle and compatible buildpacks).
,声音很熟悉!难道我们不能简单地从我们的pack CLI命令中选择构建器映像paketobuildpacks/builder:base
吗?在向GitLab投入大量噪音之前,让我们在工作站上本地尝试一下。选择要构建的项目(如果您愿意,我在gitlab.com/jonashackt/microservice-api-spring-boot创建了一个示例Spring Boot应用程序),然后运行:
docker run --rm -it -v "$PWD":/usr/src/app -w /usr/src/app paketobuildpacks/builder bash
现在,在paketobuildpacks/builder
镜像驱动的容器中,尝试使用以下命令直接运行paketo生命周期:
/cnb/lifecycle/creator -app=. microservice-api-spring-boot:latest
我只使用了the many possible parameters for the creator
command的-app
参数,因为它们中的大多数都有非常好的缺省值。但是由于默认的app目录路径不是默认的/workspace
,而是当前的目录,所以我对其进行了配置。此外,我们还需要在末尾定义一个<image-name>
,它将简单地用作结果容器镜像名称。
第一个.gitlab-ci.yml
这两个命令在我的本地工作站上都有效,所以让我们最终使用这种方法(here's a fully working example .gitlab-ci.yml
)创建一个.gitlab-ci.yml
:
image: paketobuildpacks/builder
stages:
- build
build-image:
stage: build
script:
- /cnb/lifecycle/creator -app=. $CI_REGISTRY_IMAGE:latest
不使用docker登录docker
因为我们的Kubernetes运行器中没有docker
可用,所以我们不能login into GitLab Container Registry as described in the docs。因此,在使用第一种方法时,我遇到了以下错误:
===> ANALYZING
ERROR: failed to get previous image: connect to repo store "gitlab.yourcompanyhere.cloud:4567/yourgroup/microservice-api-spring-boot:latest": GET https://gitlab.yourcompanyhere.cloud/jwt/auth?scope=repository%3Ayourgroup%2Fmicroservice-api-spring-boot%3Apull&service=container_registry: DENIED: access forbidden
Cleaning up project directory and file based variables 00:01
ERROR: Job failed: command terminated with exit code 1
使用方法described in this so answer解决了该问题。我们需要创建包含GitLab容器注册表登录信息的~/.docker/config.json
,然后the Paketo build will pick them up, as stated in the docs:
如果CNB_REGISTRY_AUTH
未设置,并且docker config.json文件
当前,生命周期应该使用此文件的内容来
使用任何匹配的注册表进行身份验证。
在我们的.gitlab-ci.yml
中,可能如下所示:
# We somehow need to access GitLab Container Registry with the Paketo lifecycle
# So we simply create ~/.docker/config.json as stated in https://stackoverflow.com/a/41710291/4964553
before_script:
- mkdir ~/.docker
- echo "{"auths":{"$CI_REGISTRY":{"username":"$CI_REGISTRY_USER","password":"$CI_JOB_TOKEN"}}}" >> ~/.docker/config.json
我们最后的.gitlab-ci.yml
当我们在.gitlab-ci.yml
的顶部使用image: paketobuildpacks/builder
时,我们现在可以直接利用生命周期。这是我们一开始就想做的事。只需记住使用正确的GitLab配置项变量来描述您的<image-name>
,如下所示:
/cnb/lifecycle/creator -app=. $CI_REGISTRY_IMAGE:latest
否则,Buildpack过程分析器步骤将中断,最终不会将其推送到GitLab容器注册表。最后,我们的.gitlab-ci.yml
如下所示(here's the fully working example):
image: paketobuildpacks/builder
stages:
- build
# We somehow need to access GitLab Container Registry with the Paketo lifecycle
# So we simply create ~/.docker/config.json as stated in https://stackoverflow.com/a/41710291/4964553
before_script:
- mkdir ~/.docker
- echo "{"auths":{"$CI_REGISTRY":{"username":"$CI_REGISTRY_USER","password":"$CI_JOB_TOKEN"}}}" >> ~/.docker/config.json
build-image:
stage: build
script:
- /cnb/lifecycle/creator -app=. $CI_REGISTRY_IMAGE:latest
我们的构建现在应该可以在不使用Pack CLI和Docker的情况下使用Paketo/BuildPack成功运行:
请参阅the full log of the example project here。
这篇关于在带有Kubernetes Executor&;非特权运行者的GitLab配置项中使用paktool.io/CloudNativeBuildPack(Cnb)(没有包CLI和amp;docker)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!