首页 > 解决方案 > Github 操作在作业之间共享工作区/工件?

问题描述

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

我最近的尝试是为每个作业手动设置具有相同卷的容器映像,根据文档,这应该是解决方案:https ://help.github.com/en/articles/workflow-syntax-for-github-actions#工作job_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

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

这个项目是一个单声道存储库,其中的代码我试图部署在路径下,server因此所有的working-directory标志。

标签: githubcontinuous-integrationgithub-actions

解决方案


您可以使用 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


推荐阅读