GitHub Actions Workflow

I needed to understand the fundamentals to setup a GitHub Actions workflow, the links in the references explain the costs, Linux builds are the clear favorite.

The workflow structure is: workflow -> jobs -> steps -> run

Simple version check workflow

This will build out a workflow that just checks the version of your developer sdk on the build agent.

  1. Create and clone a new repository, mine was https://github.com/carlpaton/github-actions-workflow-demo
  2. Create a workflow as .github/workflows/build.yml, where build.yml can be named anything related to your workflow
1
2
3
4
5
6
7
on: push                          ~ the trigger event is a push to this repository
jobs: ~ the collection of jobs to run
first-job: ~ first job in the collection, call it something meaningful
runs-on: windows-latest ~ agent label, so where the job will run
steps:
- run: node --version ~ shell command
- run: npm --version ~ shell command

These run steps are shell commands but can also be actions (see Actions below)

  1. Commit and push
  2. Inspect the workflow outcome for the commit by clicking the actions tab
  3. Here I can see the jobs, following the demo I called it first-job

first-job

  1. Clicking first-job I can see the steps

first-job-steps

  1. On closer inspection I can see the machine it ran on was Image: windows-2022 and the results of my version steps

first-job-steps-closer

This can then be adapted to perform more complex workflows simliar to the actions below.

Actions

“Actions are custom applications that run complex but repetitive tasks. For example, we can have a docker action which builds a docker image and runs a container that executes certain commands.”

Example Restore, Build and Test for an example .Net project

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
on: push
jobs:
third-job:
runs-on: ubuntu-latest
steps:
- name: Checkout the repository
uses: actions/checkout@v4.1.7
- name: Setup .NET SDK
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- name: Restore
working-directory: src
run: dotnet restore
- name: Build
working-directory: src
run: dotnet build
- name: Test
working-directory: src
run: dotnet test

This will restore, build and test the .Net project

third-job

The actions I used here are

Triggers

There are many, see https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows

These are common ones I’ve used so far:

  • pull_request - Runs your workflow when activity on a pull request in the workflow’s repository occurs
  • push - Runs your workflow when you push a commit or tag, or when you create a repository from a template
  • workflow_dispatch - To enable a workflow to be triggered manually

References