Skip to main content
  1. 🔰Posts/
  2. 🗂️My Trainings/
  3. VCS Trainings and Certifications/
  4. GitHub Actions - The Complete Guide/

GitHub Actions: Job Artifacts & Outputs

📚 Part 2 of 5: "GitHub Actions The Complete Guide" series.

Job artifacts in GitHub Actions are files generated during a workflow run that can be stored and shared between jobs. They are useful for persisting data like test results or build outputs, allowing you to access them after the workflow has completed.
External Resources »
GitHub Actions official DocumentationGitHub Actions MarketplaceGitHub.com

Job Artifacts #

In simple terms, Job Artifacts are the outputs generated by Jobs. Those could be App binaries, compiled website files but also log files and any other Outputs.

Those Outputs can be used in Other jobs OR can be downloaded.

flowchart LR B@{ shape: braces, label: "Example: Build app" } D@{ shape: braces, label: "Example: App binary, website files, etc" } classDef redclass fill:#EB4925 classDef yellowclass stroke:#EBAC25 classDef greenclass stroke:#C7EB25   A("fa:fa-cog Job"):::redclass --> B   A a1@--> C(Output Assets):::yellowclass   a1@{ animate: true }   C --> D   C -->|Via GitHub UI or REST API| E(Download & Use Manually):::greenclass   C -->|Via Action| F(Download & Use in other Jobs):::greenclass  

Uploading Job Artifacts #

Upload Actions Artifacts from your Workflow Runs.

📄 File: cicd-gh-actions-course/.github/workflows/04-01-artifacts-outputs.yml

  build:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - name: Get code
        uses: actions/checkout@v3
      - name: Install dependencies
        run: npm ci
        working-directory: 04-react-exercise
      - name: Build website
        run: npm run build
        working-directory: 04-react-exercise
      
      - name: Upload artifact
        uses: actions/upload-artifact@v5
        with:
          name: dist-files
          path: 04-react-exercise/dist

Official Marketplace GitHub Action for uploading the artifacts: https://github.com/marketplace/actions/upload-a-build-artifact

Downloading Job Artifacts #

Download Actions Artifacts from your Workflow Runs (uploaded earlier by the job described in Uploading Job Artifacts).

Every job gets its own runner - it’s own Virtual Machine that’s totally isolated from other machines and jobs. For that reason Artifact must be first uploaded and then downloaded by another job.

📄 File: cicd-gh-actions-course/.github/workflows/04-01-artifacts-outputs.yml

  deploy:
    needs: build # Needs build jobe to complete first for the Artifact to be ready
    runs-on: ubuntu-latest
    steps:
      - name: Download artifact
        uses: actions/download-artifact@v5
        with:
          name: dist-files

Official Marketplace GitHub Action for downloading the artifacts: https://github.com/actions/download-artifact

More info: Store and share data with workflow artifacts

Job Outputs #

In addition to Artifacts, we also have the Job Outputs.

Difference between Artifacts and Job Outputs #

ArtifactsOutput files and folders.Typically used for sharing log files, app binaries, etc.
Job OutputsOutput simple values.Typically used for re-using a value in a different jobs.

Example: Name of a file generated in a previous step.

Defining Job Outputs #

📄 File: cicd-gh-actions-course/.github/workflows/04-01-artifacts-outputs.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
  build:
    needs: test
    runs-on: ubuntu-latest
    outputs: # Define job outputs
      script-file: ${{ steps.publish-filename.outputs.filename-output }} # Define output from step
    steps:
      - name: Get code
        uses: actions/checkout@v3
      - name: Install dependencies
        run: npm ci
        working-directory: 04-react-exercise
      - name: Build website
        run: npm run build
        working-directory: 04-react-exercise
      
      - name: Publish JS filename
        id: publish-filename # Define step ID to reference output
        run: find 04-react-exercise/dist/assets/*.js -type f -execdir echo 'filename-output={}' >> $GITHUB_OUTPUT ';' # Set output value

Using Job Outputs in other jobs #

📄 File: cicd-gh-actions-course/.github/workflows/04-01-artifacts-outputs.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
  deploy:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Download artifact
        uses: actions/download-artifact@v5
        with:
          name: dist-files
  
      - name: Output filename-output
        run: echo "${{needs.build.outputs.script-file}}" # Use job output
      - name: Deploy
        run: echo "Deploying..."

Caching dependencies #

Dependency caching in GitHub Actions helps speed up workflow execution by storing and reusing dependencies and build outputs, reducing the need to download them each time a workflow runs. You can set this up using the actions/cache action in your workflow configuration.
flowchart LR COMMENT1@{ shape: braces, label: "Often repeated" } COMMENT2@{ shape: braces, label: "Dependencies don’t change frequently" } classDef redclass fill:#EB4925 classDef yellowclass stroke:#EBAC25 classDef greenclass stroke:#C7EB25 A[Get Code]:::yellowclass --> subB D[Get Code]:::yellowclass --> subE subgraph Install Dependencies direction TB subB[Install Dependencies]:::redclass subE[Install Dependencies]:::redclass end subB --> C[Test App]:::greenclass subE --> F[Test App]:::greenclass subB ~~~ COMMENT1 subB ~~~ COMMENT2 subE ~~~ COMMENT1 subE ~~~ COMMENT2

IMPORTANT:

  • Caching can be used not only between Jobs but also between Workflows.
  • Caching block is placed before the block that will be cached. In below example, it is - name: Install dependencies.
  • Caching block must be used in every job to which this particular cache applies to.

See - name: Install dependencies block in below example.

📄 File: cicd-gh-actions-course/.github/workflows/04-01-artifacts-outputs.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
    build:
    needs: test
    runs-on: ubuntu-latest
    outputs: # Define job outputs
      script-file: ${{ steps.publish-filename.outputs.filename-output }} # Define output from step
    steps:
      - name: Get code
        uses: actions/checkout@v3

      - name: Cache dependencies
        uses: actions/cache@v3
        with:
          path: ~/.npm
          key: deps-node-modules-${{ hashFiles('**/package-lock.json') }} # Generating cache key based on package-lock.json
      - name: Install dependencies
        run: npm ci
        working-directory: 04-react-exercise
      - name: Build website
        run: npm run build
        working-directory: 04-react-exercise

IMPORTANT: Every programming language and framework has its own way of caching.

More info:

Official Marketplace GitHub Action for caching: https://github.com/actions/cache


» Sources « #

» Disclaimer « #

This series draws heavily from Maximilian Schwarzmüller’s GitHub Actions - The Complete Guide course on Udemy.

About the instructor:
🌐 Website📺 YouTube
💼 LinkedIn🗃️ GitHub
My Repos for this section:
cicd-gh-actions-courseLearnings from "GitHub Actions - The Complete Guide" on Udemy.

ℹ️Shared for educational purposes only, no rights reserved.