Github 操作在作业之间共享工作区/工件?

问题描述 投票:0回答:6

尝试使用 Github 的 beta 操作,我有两项工作,一项是构建代码,另一项是部署代码。但是,我似乎无法在部署作业中获取构建工件。

我最新的尝试是为每个作业手动设置具有相同卷的容器映像,根据文档,这应该是解决方案:https://help.github.com/en/articles/workflow-syntax-for-github-操作#jobsjob_idcontainervolumes

设置容器要使用的卷数组。您可以使用卷在服务或作业中的其他步骤之间共享数据。您可以在主机上指定命名 Docker 卷、匿名 Docker 卷或绑定安装。

工作流程

name: CI
on:
  push:
    branches:
    - master
    paths:
    - .github/workflows/server.yml
    - server/*
jobs:
  build:
    runs-on: ubuntu-latest
    container:
      image: docker://node:10
      volumes:
      - /workspace:/github/workspace
    steps:
    - uses: actions/checkout@master
    - run: yarn install
      working-directory: server
    - run: yarn build
      working-directory: server
    - run: yarn test
      working-directory: server
    - run: ls
      working-directory: server
  deploy:
    needs: build
    runs-on: ubuntu-latest
    container:
      image: docker://google/cloud-sdk:latest
      volumes:
      - /workspace:/github/workspace
    steps:
      - uses: actions/checkout@master
      - run: ls
        working-directory: server
      - run: gcloud --version

第一个作业(构建)有一个构建目录,但是当第二个作业(部署)运行时它没有,并且只包含源代码。

这个项目是一个单一存储库,我尝试在路径下部署代码

server
因此所有
working-directory
标志。

github continuous-integration github-actions
6个回答
164
投票

您可以使用 Github Actions upload-artifact 和 download-artifact 在作业之间共享数据。

在工作1中:

steps:
- uses: actions/checkout@v1

- run: mkdir -p path/to/artifact

- run: echo hello > path/to/artifact/world.txt

- uses: actions/upload-artifact@master
  with:
    name: my-artifact
    path: path/to/artifact

还有工作2:

steps:
- uses: actions/checkout@master

- uses: actions/download-artifact@master
  with:
    name: my-artifact
    path: path/to/artifact
    
- run: cat path/to/artifact/world.txt

https://github.com/actions/upload-artifact
https://github.com/actions/download-artifact


44
投票

对于那些有兴趣在两个作业之间共享 Docker 映像的人,我是这样做的:

jobs:
  docker-build:
    name: Docker build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Build Docker image
        run: |
          docker build -t foo/bar:$GITHUB_SHA
          mkdir -p path/to/artifacts
          docker save foo/bar:$GITHUB_SHA > path/to/artifacts/docker-image.tar
          
      - name: Temporarily save Docker image
        uses: actions/upload-artifact@v2
        with:
          name: docker-artifact
          path: path/to/artifacts
          retention-days: 1

  docker-deploy:
    name: Deploy to Docker Hub
    runs-on: ubuntu-latest
    needs: docker-build
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Retrieve saved Docker image
        uses: actions/download-artifact@v2
        with:
          name: docker-artifact
          path: path/to/artifacts

      - name: Docker load
        run: |
          cd path/to/artifacts
          docker load < docker-image.tar
          # docker_build_push.sh

深受https://github.com/unfor19/install-aws-cli-action/actions/runs/400601222/workflow

的启发

谢谢@unfor19


26
投票

使用缓存或工件上传/下载

缓存用于在作业或工作流程之间重用数据/文件,而工件用于在工作流程结束后保存文件。


25
投票

GitHub Actions – Artifacts v4 现已全面发布(2023 年 12 月)

我们发布了

actions/upload-artifact
actions/download-artifact 的新版本 (v4)。
我们鼓励客户更新其工作流程,以便尽早使用
v4
工件操作。

此版本的工件操作包括性能改进和新功能,但是与以前的版本也存在一些关键差异,可能需要更新您的工作流程。

  • 工件的范围将限于作业而不是工作流程。
    这使得工件在上传后可以立即从 API 下载
  • 同一运行中的多个作业无法上传到同一个工件。
  • 单个作业最多可以上传 10 个工件。
  • Artifacts
    v4
    与以前的版本不交叉兼容。
    例如,使用
    v3
    上传的工件不能与
    actions/download-artifact@v4
    一起使用。

此版本的工件目前仅适用于 GitHub.com 客户,但我们将来将向 GitHub Enterprise Server (GHES) 客户提供支持。

我们将在新的一年中发布一篇博客文章,详细介绍我们为何进行这些改进以及接下来会发生什么。
要了解有关

v4
中包含的内容的更多信息,请访问 actions/upload-artifactactions/download-artifact 存储库。


如果您使用上传/下载 GitHub Actions,请注意工件的结构。

从 2020 年 1 月开始,请参阅“GitHub Actions:工件下载体验的更改”:

我们更改了 GitHub Actions 中的工件下载体验,因此 不再向下载的存档添加额外的根目录

以前,如果您将以下文件和文件夹上传为名为

foo
的工件,则下载的存档将包含以下结构:

foo/
|-- file1.txt
|-- dir1/
     |-- dir1-file1.txt

现在,您将获得一个仅包含您上传的文件和文件夹的存档:

file1.txt
  dir1/
  |-- dir1-file1.txt

1
投票

每个作业都在单独的运行器上运行。

Upload artifact action implementation

Github 操作“actions/upload-artifact@v3”将文件从提供的路径上传到存储容器位置。

在下一个作业中,当您运行操作“actions/download-artifact@v3”时,它会从“存储容器位置”下载工件,上一个作业将工件上传到提供的路径。

Implementation for download artifact and displaying download path

欲了解更多信息,请参阅以下链接,

Github 上传工件的操作

下载工件的 Github 操作


0
投票

基于使用缓存的答案和 docker 镜像的答案,以下是我如何让它工作的:

  get-docker-image:
    steps:
      - name: Create cache for docker image
        uses: actions/cache@v4
        with:
          path: images
          key: image-key

      - name: Download docker image from source
        run: |
          mkdir -p images
          cd images
          docker login registry
          docker pull path/image:tag
          docker save path/image:tag | gzip > docker-image.tgz

  user-docker-image:
    needs:
      - get-docker-image
    steps:
      - name: Restore cached docker image
        uses: actions/cache/restore@v4
        with:
          path: images
          key: image-key

      - name: Use the image
        working-directory: images
        run: |
          docker load < docker-image.tgz
          // do something with the image

请注意,使用缓存预计会比上传/下载快一些。

© www.soinside.com 2019 - 2024. All rights reserved.