In this workshop we're going to cover GitHub Actions. For a tangentially related workshop of using GitHub Actions to deploy to Azure using Bicep, check out this repo: https://github.com/scottsauber/workshop-dotnet-azure-github-bicep
- Basic Git Knowledge
- GitHub Account
- Recommended: an editor with a GitHub Actions extension installed for intellisense (ie VS Code with the GitHub Actions extension)
- Optional: .NET 9 installed locally to run the app locally
No hands-on work, just going over slides
No hands-on work, just going over slides
-
Fork this repository if you haven't already.
-
Create a new branch
-
Create a
.github
folder in the root -
Under the
.github
folder, create aworkflows
folder -
Under the
workflows
folder, create apr-verify.yml
file -
Paste the following into that file:
name: PR Verify on: pull_request: branches: ["main"] workflow_dispatch: jobs: build: name: PR Verify runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v4 - name: Set up .NET Core uses: actions/setup-dotnet@v4 with: dotnet-version: 9.0.x - name: Build with dotnet run: dotnet build --configuration Release - name: Test run: dotnet test --configuration Release --no-build
-
Commit, Push, and Open a PR
-
Do you think the GitHub Action will trigger when it's not committed on the
main
branch? -
Scroll down to the bottom of the PR and notice you have a new status check called "PR Verify" and click on it
-
Watch the build succeed
-
Make a change that makes the code not compile. ie open the src/WorkshopDemo/Program.cs file and add a letter to the first line
-
Commit and push and watch it fail
-
Undo that change and let's make a test fail.
-
Open /src/WorkshopDemo.Core.Tests/Common/FileServiceTests.cs and change the assertion on line 19 to something like
result.Should().Be("Hello world");
-
Commit and push and watch it fail
-
Undo that change and commit and push
-
Do not merge it in yet
No hands-on work, just going over slides
- Merge the PR from Module 3
- Setup a required rule set
- Ruleset name: Main
- Add Target: Include Default Branch
- Check Restrict Deletions
- Check Require Pull Request Before Merging
- Allow only Squash Merges (🌶️)
- Check Require Status Checks to Merge
- Click Require Branches to be up to date
- Click Add Checks
- Search for PR Verify and check it
- Check Block Force Pushes
- Save Changes
- Based on the PR Verify workflow, create one for the CI workflow (running after merge) called
ci.yml
. Make sure thatdotnet build
anddotnet test
run as part of that.- Hint: you want the
push
trigger notpull_request
- Hint: you want the
- Pssstttt... do we need to do that if we require branches to be up to date? Let's talk about it
No hands-on work, just going over slides
-
Let's create a reusable workflow for PR Verify and CI to reduce duplication
-
Note: I don't normally do this for PR + CI, but will do it for deploys (see here for more details).
-
Create a file called
reusable-build.yml
-
Add the following to it:
name: Reusable - Build on: workflow_call: inputs: dotnet_version: required: true type: string jobs: build: name: Reusable - Build runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v4 - name: Set up .NET Core uses: actions/setup-dotnet@v4 with: dotnet-version: ${{ inputs.dotnet_version }} - name: Build with dotnet run: dotnet build --configuration Release - name: Test run: dotnet test --configuration Release --no-build
-
In your pr-verify.yml, change the YAML to this:
name: PR Verify on: pull_request: branches: ["main"] workflow_dispatch: jobs: build: name: PR Verify uses: ./.github/workflows/reusable-build.yml with: dotnet_version: 9.0.x
-
Make a similar change for
ci.yml
-
Congratulations - now we have shared code between CI and PR
-
But what if we wanted this in a reusable repo...? 🤔
No hands-on work, just going over slides
- Make a new repository
- Create a
.github/workflows/reusable-build.yml
file and copy it over - Change your
pr-verify
andci.yml
to point at the new repository instead of your local one
No hands-on work, just going over slides
-
Create a
secrets-test.yml
file with the following YAMLname: Echo on: pull_request: branches: ["main"] jobs: echo: name: Echo runs-on: ubuntu-latest steps: - name: Hello world run: echo ${{ secrets.SOME_SECRET }}
-
Go to the repository's Settings tab
-
Click on Secrets and Variables => Secrets
-
Click "New Repository Secret"
-
Give the name of SOME_SECRET and then a random value
-
Commit and push the workflow above and watch how it masks the value