如何在 GitHub Actions 的构建步骤后,在下一步中使用 Docker 构建。

huangapple go评论63阅读模式
英文:

How to use docker build in next step of github actions after build step

问题

以下是我使用的GHA工作流程YAML文件示例。当前,我正在构建并推送Docker镜像到存储库。在接下来的步骤中,我会在Docker镜像上运行pytest。但在下一步中,它必须拉取上一步构建的镜像才能运行pytest。是否有办法可以在不从存储库拉取Docker镜像的情况下运行pytest以节省时间。

- name: Build
  uses: docker/build-push-action@v4
  with:
    context: .
    push: true
    tags: my_registry:${{ github.run_number }}
    cache-from: |
            type=registry,ref=my_registry:cache
    cache-to: |
            type=registry,ref=my_registry:cache,mode=max
- name: Pytest
  run: docker run my_registry:${{ github.run_number }} pytest

(Note: The code section is not translated as per your request.)

英文:

following is the example GHA workflow yaml file I'm using. Currently I'm building an pushing docker image to artifact registry. And in the next step I run pytest on the docker image. But for next step it has to pull image which was build in the last step to run pytest. Is there any way I can run pytest without pulling the docker image from registry to save time.


      - name: Build
        uses: docker/build-push-action@v4
        with:
          context: .
          push: true
          tags: my_registry:${{ github.run_number }}
          cache-from: |
            type=registry,ref=my_registry:cache
          cache-to: |
            type=registry,ref=my_registry:cache,mode=max
      - name: Pytest
        run: docker run my_registry:${{ github.run_number }} pytest

答案1

得分: 2

请看操作示例,了解如何在作业之间共享Docker镜像。

该操作提供了一个outputs参数:

输出目标列表(格式:type=local,dest=path)。

注:目前不支持多个outputs

您可以使用它将您的镜像输出到一个tar文件,然后在下一步中加载它。

示例:

- name: 构建
  uses: docker/build-push-action@v4
  with:
    context: .
    push: true
    tags: my_registry:${{ github.run_number }}
    cache-from: |
            type=registry,ref=my_registry:cache
    cache-to: |
            type=registry,ref=my_registry:cache,mode=max
    outputs: type=docker,dest=./myimage.tar
- name: Pytest
  run: |
    docker load --input ./myimage.tar
    docker run my_registry:${{ github.run_number }} pytest    
英文:

Have a look at the actions's example on how to share a docker image between jobs.

The action offers an outputs¹ argument:

> List of output destinations (format: type=local,dest=path).
>
> ¹ multiple outputs are not yet supported

You can use it to output your image to a tar file which you can load in the next step.

Example:

- name: Build
  uses: docker/build-push-action@v4
  with:
    context: .
    push: true
    tags: my_registry:${{ github.run_number }}
    cache-from: |
      type=registry,ref=my_registry:cache
    cache-to: |
      type=registry,ref=my_registry:cache,mode=max
    outputs: type=docker,dest=./myimage.tar
- name: Pytest
  run: |
    docker load --input ./myimage.tar
    docker run my_registry:${{ github.run_number }} pytest

huangapple
  • 本文由 发表于 2023年3月4日 08:43:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定