GitHub Actions Workshop

Master GitHub Actions with hands-on labs and exercises. Learn how to automate workflows, run tests, deploy applications, and more using GitHub's powerful automation platform. This repository has everything you need to get started with continuous integration and continuous deployment.


Project maintained by prasadhonrao Hosted on GitHub Pages — Theme by mattgraham

Lab: ASP.NET Web App Download Artifact

Introduction

In this lab, you will create a GitHub Actions workflow to download artifacts generated by a previous workflow. This demonstrates the use of the actions/download-artifact action to retrieve stored artifacts for further use, such as deploying an ASP.NET Web Application.

Duration: 15-20 minutes


Prerequisites

Before starting this lab, ensure that:

  1. You have completed the ASP.NET Web App Upload Artifact Lab.
  2. The ASP.NET Web App Build - Upload Artifact Workflow is functional and uploads the artifact named aspnet-web-app.

Refer to the ASP.NET Web App Download Artifact Starter file for the initial workflow content.


Instructions

Step 1: Create a YAML Workflow Using the Starter File

  1. Refer to the ASP.NET Web App Download Artifact Starter File.
  2. Copy the content of the starter file:

    name: ASP.NET Web App Download Artifact
    
    on:
      workflow_dispatch:
    
    jobs:
      build-and-upload:
        runs-on: ubuntu-latest
        defaults:
          run:
            working-directory: ./src/dotnet/WebApp
        steps:
          - name: Checkout Code
            uses: actions/checkout@v4.1.7
    
          - name: Set up .NET Core
            uses: actions/setup-dotnet@v4.0.1
            with:
              dotnet-version: '8.x'
    
          - name: Build Code
            run: dotnet build --configuration Release
    
          - name: Publish Code
            run: dotnet publish -c Release --property:PublishDir="$/webapp"
    
          - name: Upload Artifact
            uses: actions/upload-artifact@v4.3.6
            with:
              name: aspnet-web-app # Artifact name
              path: $/webapp
    
  3. In your repository, create a new workflow file under .github/workflows and name it aspnet-webapp-download-artifact.yml.
  4. Paste the copied content into the new file.
  5. Commit and push the changes to your repository.

Step 2: Add Additional Steps for Verification

  1. Open the aspnet-webapp-download-artifact.yml file.
  2. Add new job to download the artifact and list its contents:

    download:
      runs-on: ubuntu-latest
      needs: build-and-upload
      steps:
        - name: Download Artifact
          uses: actions/download-artifact@v4.1.8
          with:
            name: aspnet-web-app # Artifact name
        - name: List Files in Artifact
          run: |
            ls -al
          shell: bash
    

Step 3: Verify the Complete Workflow File

Ensure your aspnet-webapp-download-artifact.yml file matches the following:

name: ASP.NET Web App - Download Artifact

on:
  push:
    paths:
      - '.github/workflows/aspnet-webapp-download-artifact.yml'
      - 'src/dotnet/WebApp/**'
  workflow_dispatch:

jobs:
  build-and-upload:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./src/dotnet/WebApp
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4.1.7

      - name: Set up .NET Core
        uses: actions/setup-dotnet@v4.0.1
        with:
          dotnet-version: '8.x'

      - name: Build Code
        run: dotnet build --configuration Release

      - name: Publish Code
        run: dotnet publish -c Release --property:PublishDir="$/webapp"

      - name: Upload Artifact
        uses: actions/upload-artifact@v4.3.6
        with:
          name: aspnet-web-app # Artifact name
          path: $/webapp
          retention-days: 7 # Number of days to keep the artifact. Default is 90 days or configured value in the repository settings -> Actions permissions -> Artifact and logs retention

  download:
    runs-on: ubuntu-latest
    needs: build-and-upload
    steps:
      - name: Download Artifact
        uses: actions/download-artifact@v4.1.8
        with:
          name: aspnet-web-app # Artifact name
      - name: List Files in Artifact
        run: |
          ls -al
        shell: bash

Step 4: Commit and Push the Workflow File

  1. Save the changes to the aspnet-webapp-download-artifact.yml file.
  2. Commit the updated file with a descriptive message, e.g., "Added workflow for downloading build artifact".
  3. Push the changes to your repository.

Step 5: Trigger and Run the Workflow

  1. Navigate to the Actions tab in your GitHub repository.
  2. Manually trigger the ASP.NET Web App Download Artifact workflow via the workflow_dispatch event.
  3. Monitor the workflow run and ensure the download job executes successfully.

Step 6: Verify the Downloaded Artifact

  1. Review the logs of the List Downloaded Files step to confirm that the artifact contents are correctly saved in the downloaded-artifacts directory.
  2. Use the Verify Artifact Content step logs to ensure the artifact matches the expected build output.

Summary

In this lab, you created a GitHub Actions workflow to download artifacts uploaded by a previous workflow using the actions/download-artifact action. You verified the downloaded files and learned how to use them for subsequent steps in a CI/CD pipeline.