简体   繁体   English

Heroku 带有 CNB 生命周期创建者的 Buildpacks - 如何指定自定义图像标签?

[英]Heroku Buildpacks with the CNB lifecycle creator - how to specifiy custom image labels?

We're using Heroku Buildpacks inside a GitLab CI/CD pipeline by triggering a job running the heroku builder image and then invoking the /cnb/lifecycle/creator directly, instead of using the pack CLI.我们在 GitLab CI/CD 管道中使用 Heroku Buildpacks,方法是触发运行 heroku 构建器映像的作业,然后直接调用 /cnbLI/生命周期。 This is because be can't use docker in docker on our container orchestrator.这是因为不能在我们的容器编排器上使用 docker 中的 docker。

This all works fine and dandy, but we're facing the problem of not being able to specify any custom labels that will be added to the produced image.这一切都很好,很漂亮,但我们面临的问题是无法指定将添加到生成的图像中的任何自定义标签。 By default, these labels are added:默认情况下,会添加这些标签:

  • io.buildpacks.lifecycle.metadata io.buildpacks.lifecycle.metadata
  • io.buildpacks.build.metadata io.buildpacks.build.metadata
  • io.buildpacks.project.metadata io.buildpacks.project.metadata

But we'd also like to add the following labels:但我们还想添加以下标签:

  • org.opencontainers.image.revision org.opencontainers.image.revision
  • org.opencontainers.image.url org.opencontainers.image.url
  • org.opencontainers.image.source org.opencontainers.image.source
  • org.opencontainers.image.version org.opencontainers.image.version
  • org.opencontainers.image.licenses org.opencontainers.image.licenses
  • org.opencontainers.image.authors org.opencontainers.image.authors

Unfortunately there seems to be no way to specify this.不幸的是,似乎没有办法指定这一点。 The creator doesn't offer any configuration parameter to do this, neither the pack CLI as far as I could tell.据我所知,创建者没有提供任何配置参数来执行此操作,pack CLI 也没有。 When using Paketo buildpacks, there is indeed a designated buildpack to solve this: https://github.com/paketo-buildpacks/image-labels在使用 Paketo buildpacks 时,确实有一个指定的 buildpack 可以解决这个问题: https://github.com/paketo-buildpacks/image-labels

Is there any way to do something similiar when using Heroku buildpacks?使用 Heroku buildpacks 时有什么方法可以做类似的事情吗?

You can use the Paketo image-labels buildpack with the Heroku buildpacks.您可以将Paketo 图像标签构建包与 Heroku 构建包一起使用。 Here's an example of doing this from the pack CLI:以下是从pack CLI 执行此操作的示例:

$ pack build --builder heroku/buildpacks:20 --buildpack paketo-buildpacks/image-labels my-app

With the CLI you can add more --buildpack options as needed.使用 CLI,您可以根据需要添加更多--buildpack选项。

If you want to codify this, you can create a project.toml and use the io.buildpacks.post.group to always run the image-labels buildpack at the end.如果您想对此进行编码,您可以创建一个project.toml并使用io.buildpacks.post.group始终在最后运行 image-labels buildpack。

If you're using creator binary, you can provide it a group.toml https://github.com/buildpacks/spec/blob/main/platform.md#inputs-3如果您使用的是creator二进制文件,您可以为其提供group.toml https://github.com/buildpacks/spec/blob/main/platform.md#inputs-3

Here's a more detailed answer on how to achieve this:这是有关如何实现此目的的更详细的答案:

  1. Create a custom builder image which contains heroku buildpacks as well as the desired image labels buildpack by paketo创建一个自定义构建器映像,其中包含 heroku 构建包以及 paketo 所需的图像标签构建包
  2. Run the lifecycle executables in the specified order manually手动按指定顺序运行生命周期可执行文件
  3. Add an entry for the image labels buildpack to the group.toml AFTER the detector has run检测器运行后,将图像标签 buildpack 的条目添加到 group.toml

Heres an example Dockerfile for building a custom builder image:下面是一个用于构建自定义构建器映像的示例 Dockerfile:

# paketo
FROM paketobuildpacks/builder:base as paketo

# heroku
FROM heroku/builder-classic:22

USER root

COPY --from=paketo /cnb/buildpacks/paketo-buildpacks_image-labels /cnb/buildpacks/paketo-buildpacks_image-labels

COPY group.toml /opt/group.toml

SHELL ["/bin/bash", "-c"]

USER 1000

This is what the file under /opt/group.toml looks like:这是/opt/group.toml下的文件的样子:

[[group]]
    description = "A Cloud Native Buildpack that enables configuration of labels on the created image"
    homepage = "https://github.com/paketo-buildpacks/image-labels"
    id = "paketo-buildpacks/image-labels"
    keywords = ["image-labels", "labels"]
    name = "Paketo Image Labels Buildpack"
    version = "4.2.0"
    sbom-formats = ["application/vnd.cyclonedx+json", "application/vnd.syft+json"]
    api = "0.7"

Then you can run the lifecycle as follows:然后您可以按如下方式运行生命周期:

export CNB_LAYERS_DIR=${BP_LAYERS_PATH}
export CNB_GROUP_PATH=${BP_LAYERS_PATH}/group.toml

/cnb/lifecycle/detector -layers=${BP_LAYERS_PATH} -platform=${BP_PLATFORM_PATH} -app=.

# add additional logic for image-label-buildpack
[ -f /opt/group.toml ] && echo "$(cat /opt/group.toml)" >> ${CNB_GROUP_PATH}

export BP_LAST_VERSION=${BP_IMAGE_VERSION}
/cnb/lifecycle/analyzer -uid=$(id -u) -gid=0 -cache-dir=${BP_CACHE_PATH} -layers=${BP_LAYERS_PATH} -analyzed=${BP_LAYERS_PATH}/analyzed.toml ${BP_REGISTRY}/${BP_IMAGE_NAME}:${BP_LAST_VERSION}

/cnb/lifecycle/restorer -uid=$(id -u) -gid=0 -cache-dir=${BP_CACHE_PATH} -layers=${BP_LAYERS_PATH} -group=${BP_LAYERS_PATH}/group.toml

/cnb/lifecycle/builder -layers=${BP_LAYERS_PATH} -platform=${BP_PLATFORM_PATH} -app=. -group=${BP_LAYERS_PATH}/group.toml

/cnb/lifecycle/exporter -uid=$(id -u) -gid=0 -cache-dir=${BP_CACHE_PATH} -layers=${BP_LAYERS_PATH} -app=. -analyzed=${BP_LAYERS_PATH}/analyzed.toml -run-image=${BP_RUN_IMAGE} ${BP_REGISTRY}/${BP_IMAGE_NAME}:${BP_IMAGE_VERSION}

Make sure to fill the env variables with sane values as defined by the platform spec .确保使用平台规范定义的合理值填充 env 变量。

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

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