GitHub Actions

Table of contents
  1. Add a workflow
  2. Basic workflow syntax
  3. Repository secrets
  4. Self-hosted runner
  5. Add a self-hosted runner
  6. Run self-hosted runner
    1. To run as a service

Add a workflow

Navigate to a GitHub repo.

Go to Actions and click on New workflow.

New Workflow

You can either create a new workflow from scratch or use a template recommended for your project.

You will have to commit your workflow yaml to the main branch.


Basic workflow syntax

See details here.

Example:

name: Workflow Name

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  my-job:
    name: Job Name
    timeout-minutes: 10
    runs-on: [self-hosted, macOS, X64]
    env:
      ENV_NAME: ${{ secrets.MyEnv }}

    steps:
    - uses: actions/checkout@v2

    - name: Step Name
      run: |
        echo $ENV_NAME

Repository secrets

In order to prevent sensitive environment variables from being committed with the workflow file, you can use Actions secrets.

Navigate to Settings -> Secrets: Actions

Actions secrets

Click on New repository secret.

Naming for secrets:

  • Must not start with GITHUB_ prefix
  • Must not start with numbers
  • Must be alphanumeric + underscores (a-z, A-Z, 0-9, _)
  • Are not case sensitive

Created secrets can then be used in workflow files as

${{ secrets.MySecretName }}
# Since secrets are not case sensitive, you could've just used
# ${{ secrets.mysecretname }} as well.

Self-hosted runner

By default, GitHub Action Runners are machines managed by the GitHub.

However, because you are borrowing a shared resource, your workflow may take a longer time to execute due to the wait time.

Or you may be wanting to use GitHub Actions to automate on-prem deployment.

To solve any one of these issues, you can add your own machine as a self-hosted runner on GitHub.

See details here.


Add a self-hosted runner

Navigate to a GitHub repo.

Go to Settings -> Actions: Runners.

Actions: Runners

Click on New self-hosted runner and follow the instructions.

New self-hosted runner

While running ./config.sh --url <repo> --token <token>, you will be asked to configure a label. This label is used to identify a specific runner, in the case you have multiple self-hosted runners. This value can be changed later in GitHub.


Run self-hosted runner

The simplest way to have the runner listening for jobs is to

./run.sh

To run as a service

To have the runner listening as a background job and have it restart itself upon machine failure, install it as a service and start it.

sudo ./svc.sh install
sudo ./svc.sh start
sudo ./svc.sh stop
sudo ./svc.sh status
sudo ./svc.sh uninstall

To see this usage do:

sudo ./svc.sh