name: build

on:
  push:
    branches:
      - main

  pull_request:
    branches:
      - main

  schedule:
    # Re-build all images at 2am UTC every Monday (8pm CST/9pm CDT)
    #
    # This ensures we always start with a recent base image, which
    # reduces wasted space due to written-over files in the writable
    # layer, ensures packages are up-to-date (since many of these
    # images install the latest versions of packages available at
    # build time), and allow us to ensure that images continue to
    # be buildable from source (no removed packages).
    #
    # See: https://crontab.guru/#0_2_*_*_1
    - cron: "0 2 * * 1"

  workflow_dispatch:

permissions:
  actions: none
  checks: none
  contents: read
  deployments: none
  issues: none
  packages: none
  pull-requests: none
  repository-projects: none
  security-events: none
  statuses: none

jobs:
  # Quick checks, running linters, checking formatting, etc
  quick:
    runs-on: ubuntu-20.04
    steps:
      - name: Cancel previous runs
        if: github.event_name == 'pull_request'
        uses: styfle/cancel-workflow-action@0.9.1

      - name: Checkout
        uses: actions/checkout@v2

      - name: Install Node.js
        uses: actions/setup-node@v2
        with:
          node-version: 12.x

      - name: Install Node.js modules
        run: yarn install

      - name: Check formatting
        run: yarn format:check

  images:
    runs-on: ubuntu-20.04
    strategy:
      matrix:
        job:
          - arch
          - centos
          - ubuntu
    name: images/${{ matrix.job}}
    steps:
      - name: Cancel previous runs
        if: github.event_name == 'pull_request'
        uses: styfle/cancel-workflow-action@0.9.1

      - name: Checkout
        uses: actions/checkout@v2

      - name: Build ${{ matrix.job }} images
        run: |
          ${{ github.workspace }}/scripts/build_images.sh \
            --tag=${{ matrix.job }}

      - name: Authenticate to Docker Hub
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Push images to Docker Hub
        if: github.event_name != 'pull_request'
        run: |
          ${{ github.workspace}}/scripts/push_images.sh \
            --tag=${{ matrix.job}}
