GitHub actions 101

Tags

  • eng
  • automation

What are GitHub actions? Sets of steps (= workflows) triggered by events. GitHub hosts their virtual machines dedicated to serving GitHub-related workflows in public repos (private cost $$$). Alternatively, you can also self-host action runners (written in C#). Some terminology based on the tutorial:

Basic YAML to test Python (+ some unnecessary details for showcase)

name: Python package
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    env:
      ADDITIONAL_INSTALL: pytorch
      FAKE_DB: ${{ secrets.DB_CREDENTIALS }}
    strategy:
      matrix:
        python-version: [3.6, 3.7, 3.8, 3.9]
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install flake8 pytest ${{ env.ADDITIONAL_INSTALL }}
          if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
      - name: Lint with flake8
        run: |
          # stop the build if there are Python syntax errors or undefined names
          flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
          # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
          flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
      - name: Test with pytest
        run: |
          pytest

Tidbits from the tutorial

In complex workflows tutorial:

jobs:
  build:
    runs-on: ubuntu-latest
    strategy.matrix.node: [6, 8, 10]
    steps:
      - uses: actions/setup-node@v2
        with.node-version: ${{ matrix.node }}
  • caching dependencies across workflows in a repo (actions/cache@v2)

You can share workflow templates within an org in a .github repository. Create a workflow-templates directory and put name-ci.yaml and name-ci.properties.json in it. Templates can be inited from the “Actions” tab.

Environment variables via env.X and ${{ env.X }}.

Also see guides for specific use-cases.