What are GitHub Actions?

GitHub Actions Events

  • GitHub Actions is an event-driven declarative job scheduling system.
  • Actions are declared through workflow files, containing event triggers and jobs.
  • Repositories can have multiple workflows.

Custom workflow language

  • YAML
  • Shell scripts

Anatomy of a workflow

---
on:
  push:
    branches: ['master']
jobs:
  have-faith:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Good luck! 🤞"

Event triggers

---
on:
  push:
  pull_request:
  workflow_dispatch:
  schedule: '0 12 * * *'

Python example

jobs:
  test:
    steps:
      - name: Checkout
	uses: actions/checkout@v2
      - name: Install Python
	uses: actions/setup-python@v2
	with:
	 python-version: 3.9
      - name: Install dependencies
	run: pip install -r requirements.txt
      - name: Run tests
	run: pytest -vv

Test matrix

jobs:
  test:
    strategy:
      matrix:
	python-version: ['3.7', '3.8', '3.9']
    steps:
      - name: Checkout
	uses: actions/checkout@v2
      - name: Install Python
	uses: actions/setup-python@v2
	with:
	  python-version: ${{ matrix.python-version }}
      - name: Install dependencies
	run: pip install -r requirements.txt
      - name: Run tests
	run: pytest -vv

Artifacts

jobs:
  build:
    steps:
      - name: Create container image
	run: >
	  guix pack -f docker
	  --root=nivlheim-image.tar.gz
	  --entry-point=bin/nivlheim
	  -S /etc/profile=etc/profile
	  --with-source=nivlheim=$PWD
	  nivlheim

      - name: Upload artifact
	uses: actions/upload-artifact@v2
	with:
	  name: nivlheim-image.tar.gz
	  path: nivlheim-image.tar.gz

Conditionals

jobs:
  build:
    steps:
      [...]
      - name: Upload artifact
	uses: actions/upload-artifact@v2
	with:
	  name: nivlheim-image.tar.gz
	  path: nivlheim-image.tar.gz
  publish:
    if: ${{ github.ref == 'refs/heads/master' }}
    needs: build
    steps:
      - name: Download artifact

Service containers

runs-on: ubuntu-latest
services:
  postgres:
    image: postgres:latest
  mreg:
    image: ghcr.io/unioslo/mreg:latest
    env:
      MREG_DB_HOST: postgres
    ports:
      - 8000:8000
steps:
  - run: >
      echo -ne 'test\nsource testsuite\nquit\n'
      | mreg-cli -u ci --url http://localhost:8000

The End

The source of this presentation is available at https://github.com/mbakke/github-actions-intro.

Code samples were taken from:

Official GitHub documentation can be found at https://docs.github.com/en/actions/reference.