How to share artifact files between jobs
  • 1 Minute to read
  • Dark
    Light
  • PDF

How to share artifact files between jobs

  • Dark
    Light
  • PDF

Article summary

Intro

It is very common to want to save an artifact file in one workflow job and then retrieve it from another job.

How to share artifact files between jobs

Before saving or loading an artifact file, we need to ensure the order of operation for the jobs.

Add needs: previous-job to a job that is reading the artifact from a previous job. It needs to run second and needs the previous job to run first to save the artifact file.

This syntax allows Job-Run-Second to require that Job-Run-First runs first.

  Job-Run-Second:
    runs-on: ubuntu-latest
    needs: Job-Run-First

Here is the complete workflow file

name: Workflow-hello-world

on:
  push:
    # Trigger the workflow on any push event to the repository
  issues:
    # Trigger the workflow on issue open, close, or reopen events
    types: [opened, closed, reopened]

jobs:
  Job-Run-First:
    runs-on: ubuntu-latest
    steps:
      - name: "Print Run 1st 🔎"
        run: | 
          # Print "Run 1st" and the current date and time
          echo "Run 1st"
          date +"%Y-%m-%d %H:%M:%S"
          # Create a file named example.txt with the content "Example artifact file"
          echo "Example artifact file" > example.txt
      - name: Upload example artifact file
        uses: actions/upload-artifact@v3
        with: 
          # Upload the example.txt file as an artifact named Artifact-file
          name: Artifact-file
          path: example.txt

  Job-Run-Second:
    runs-on: ubuntu-latest
    needs: Job-Run-First
    steps:
      - name: "Print Run Second 🔎"
        run: |
          # Print "Run 2nd" and the current date and time
          echo "Run 2nd"
          date +"%Y-%m-%d %H:%M:%S"          
      - name: Download example artifact file
        uses: actions/download-artifact@v3
        with:
          # Download the artifact named Artifact-file to the specified path
          name: Artifact-file
          path: path/to/artifact
      - name: Print content of example-file.txt
        run: 
          # Display the content of the downloaded example.txt file
          cat path/to/artifact/example.txt

Example output

View this interactive demo to see the job order and the output of saving and loading an artifact file.


Was this article helpful?