Github Actions Basics

14 Mar 2022

GitHub actions is a CI/CD platform just like Jenkins, CircleCI and others. Its very easy to use since it directly integrates with the repository. Just create a file inside your repo .github/workflows/workflow_name.yml and put the appropriate config. Here’s an example workflow:

name: ci-test # workflow name

# triggers
on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:

  test: # job name
    runs-on: ubuntu-latest # a GitHub hosted runner

    services:
      postgres:
        # Docker Hub image
        image: postgres:11
        # Provide the password for postgres
        env:
          POSTGRES_USER: postgres_user
          POSTGRES_PASSWORD: postgres_password
          POSTGRES_DB: db_name
        ports:
          # Maps tcp port 5432 on service container to the host
          - 5432:5432
        # Set health checks to wait until postgres has started
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
    - uses: actions/checkout@v2 # a reusable action, created by GitHub

    - name: Set up Go
      uses: actions/setup-go@v2 # another reusable action
      with:
        go-version: 1.17

    - name: Install golang-migrate
      run: |
        curl -L https://github.com/golang-migrate/migrate/releases/download/v4.15.1/migrate.linux-amd64.tar.gz | tar xvz
        sudo mv migrate /usr/bin/
        which migrate

    - name: Run migrations
      run: make migrateup

    - name: Test
      run: make test # a custom action where we're running custom commands