Lab: Build ASP.NET Web App
Introduction
In this lab, you will create a GitHub Actions workflow that builds and publishes an ASP.NET Core Web App. The workflow triggers on code changes in the src/dotnet/WebApp/
directory and can also be run manually. You will also learn how to change the runner environment to macOS, Windows, or a self-hosted runner.
Estimated Duration: 20–30 minutes
Instructions
Step 1: Set up your repository
-
Navigate to the GitHub repository where your ASP.NET Web App source code is located.
- The web app should reside in the path:
src/dotnet/WebApp
. - If you don’t have this structure yet, create the folders and add a sample
.csproj
file and Program.cs to simulate the project.
- The web app should reside in the path:
Step 2: Create the workflow file
-
In the repository, go to the Code tab.
-
Click Add file > Create new file.
-
Name the file:
.github/workflows/aspnet-webapp-build.yml
-
Paste the following workflow YAML:
name: ASP.NET Web App Build on: push: paths: - 'src/dotnet/WebApp/**' workflow_dispatch: jobs: build: 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"
-
Click Commit new file to save the workflow.
Step 3: Understand the workflow
-
Triggers:
- On
push
to any file undersrc/dotnet/WebApp/
- On manual execution via
workflow_dispatch
- On
- Environment: Runs on
ubuntu-latest
by default -
Jobs:
- Checkout the code
- Set up .NET 8 SDK
- Build the project in
Release
mode - Publish the output to a temporary directory
Step 4: Test the workflow
- Make a small change to any file under
src/dotnet/WebApp/
and push it to GitHub. - Alternatively, go to the Actions tab and run the workflow manually using Run workflow.
- View the workflow logs in the Actions tab.
- Confirm successful execution of all steps.
Step 5: Change the runner environment
You can change the runner by updating the runs-on
property in the workflow.
a. Use a Windows runner
runs-on: windows-latest
b. Use a macOS runner
runs-on: macos-latest
c. Use a self-hosted runner
To use a self-hosted runner, make sure you’ve registered a runner in your repo/org. Then change:
runs-on: self-hosted
You can also target a specific label if you’ve added one during runner setup:
runs-on: [self-hosted, windows, x64]
✅ Tip: Self-hosted runners require manual setup. Refer to GitHub Docs - Self-hosted runners for guidance.
Step 6: Try with different runners (Optional)
- Modify the workflow to use one of the alternative runners.
- Commit the changes.
-
Re-run the workflow to observe platform-specific behavior.
- On macOS or Windows, the behavior of SDK setup and file paths may differ slightly.
Summary
In this lab, you created a workflow to build and publish an ASP.NET Core Web App using GitHub Actions. You learned how to:
- Trigger workflows on push and manual dispatch.
- Use different GitHub-hosted runners or a self-hosted runner.
- Automate your .NET project build process.