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.
In this lab, you will learn how to create a GitHub Actions workflow to build and deploy an ASP.NET web application to Azure. The workflow will automatically trigger when changes are pushed to the src/dotnet/WebApp
directory and will deploy the web app to an Azure App Service.
Duration: 30-45 minutes
Certainly! Here’s the updated Prerequisites section with the new point related to creating an ASP.NET Web App:
src/dotnet/WebApp
directory.GitHub Actions
repository in Visual Studio Code or your preferred IDE..github/workflows
directory and create a new file named aspnet-webapp-deploy.yml
.Add the following YAML code to define the workflow trigger. The workflow will run automatically on a push
event when there are changes to the src/dotnet/WebApp
directory or when manually triggered (workflow_dispatch
).
name: ASP.NET Web App Deploy
on:
push:
paths:
- '.github/workflows/aspnet-webapp-deploy.yml'
- 'src/dotnet/WebApp/**'
env:
AZURE_WEBAPP_NAME: github-actions-workshop-aspnet-webapp
AZURE_WEBAPP_PACKAGE_PATH: ./published
CONFIGURATION: Release
DOTNET_CORE_VERSION: 8.0.x
WORKING_DIRECTORY: 'src/dotnet/WebApp'
This configuration ensures the workflow will be triggered when there are changes in the src/dotnet/WebApp
directory or if the workflow file itself is modified.
Add the following code to define the build
job that will run on the ubuntu-latest
runner. This job will build and publish the ASP.NET web app.
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup .NET SDK
uses: actions/setup-dotnet@v4
with:
dotnet-version: $
- name: Verify Working Directory
run: |
echo "Current Directory: $(pwd)"
ls $
- name: Restore dependencies
run: dotnet restore $
- name: Build
run: dotnet build "$" --configuration $ --no-restore
- name: Publish
run: dotnet publish "$" --configuration $ --output "$"
- name: Publish Artifacts
uses: actions/upload-artifact@v4
with:
name: webapp
path: $
Explanation:
actions/checkout
step checks out the code.actions/setup-dotnet
action sets up the .NET SDK environment.dotnet restore
step restores any dependencies for the app.dotnet build
command builds the app in Release
configuration.dotnet publish
step publishes the app to the output directory (./published
).actions/upload-artifact
step uploads the published output as an artifact for later use in the deployment job.Below the build job, define the deploy
job. This job will deploy the artifact from the build job to the Azure App Service.
deploy:
runs-on: ubuntu-latest
needs: build
steps:
- name: Download artifact from build job
uses: actions/download-artifact@v4
with:
name: webapp
path: $
- name: Azure Login
uses: azure/login@v2
with:
creds: $
- name: Deploy to Azure WebApp
uses: azure/webapps-deploy@v2
with:
app-name: $
package: $
Explanation:
actions/download-artifact
step downloads the published artifact from the build job.azure/login
action authenticates the workflow to Azure using a service principal stored in the GitHub secrets.azure/webapps-deploy
action deploys the web app to the Azure App Service using the package from the build job.After adding the workflow file, commit the changes to your repository.
git add .github/workflows/aspnet-webapp-deploy.yml
git commit -m "Add ASP.NET Web App Build and Deploy Workflow"
git push
src/dotnet/WebApp
directory.In this lab, you have learned to:
In the next lab, you will add advanced deployment steps and explore reusable workflows for future use.