githubcontinuous-integrationgithub-actions

Github actions share workspace/artifacts between jobs?


Trying to use Github's beta actions, I have two jobs, one that builds the code and then one that will deploy code. However, I can't seem to get the build artifact in deploy job.

My latest attempt is to manually set a container image with the same volumes for each job, according to docs this should be solution: https://help.github.com/en/articles/workflow-syntax-for-github-actions#jobsjob_idcontainervolumes

Sets an array of volumes for the container to use. You can use volumes to share data between services or other steps in a job. You can specify named Docker volumes, anonymous Docker volumes, or bind mounts on the host.

Workflow

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

The first job (build) has a build directory, but when the second job (deploy) runs it doesn't and only contains the source code.

This project is a mono repo with code I'm trying to deploy being under path server hence all the working-directory flags.


Solution

  • You can use the Github Actions upload-artifact and download-artifact to share data between jobs. For example:

    Job 1:

        steps:
          - uses: actions/checkout@v4
          - run: mkdir -p path/to/artifact
          - run: echo hello > path/to/artifact/world.txt
          - uses: actions/upload-artifact@v4
            with:
              name: my-artifact
              path: path/to/artifact
    

    Job 2:

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