---
name: Nightly

on:
  workflow_dispatch:
    inputs:
      branch:
        description: 'Claircore branch to reference'
        required: false
      go_version:
        description: 'Go version to be used throughout'
        required: false
      tag:
        description: 'Tag to push resulting image to'
        required: false
  schedule:
    - cron: '30 5 * * *'

jobs:
  build:
    name: Build and Push container
    runs-on: 'ubuntu-latest'
    steps:
      - name: Setup
        id: setup
        env:
          QUAY_TOKEN: ${{ secrets.QUAY_TOKEN }}
          QUAY_API_TOKEN: ${{ secrets.QUAY_API_TOKEN }}
        # This step uses defaults written in the shell script instead of the
        # nicer workflow inputs so that the cron trigger works.
        run: |
          br=$(test -n "${{github.event.inputs.branch}}" && echo "${{github.event.inputs.branch}}" || echo main)
          gv=$(test -n "${{github.event.inputs.go_version}}" && echo "${{github.event.inputs.go_version}}" || echo 1.17.1)
          : "${repo:=$GITHUB_REPOSITORY}"
          test "${repo%%/*}" = quay && repo="projectquay/${repo##*/}" ||:
          printf '::set-output name=push::%s\n' "${{ env.QUAY_TOKEN != '' }}"
          printf '::set-output name=api::%s\n' "${{ env.QUAY_API_TOKEN != '' }}"
          printf '::set-output name=date::%s\n' "$(date -u '+%Y-%m-%d')"
          printf '::set-output name=tag::%s\n' "$(test -n "${{github.event.inputs.tag}}" && echo "${{github.event.inputs.tag}}" || echo nightly)"
          printf '::set-output name=claircore_branch::%s\n' "${br}"
          printf '::set-output name=go_version::%s\n' "${gv}"
          printf '::set-output name=repo::%s\n' "${repo}"
          # Environment variables
          printf 'CLAIRCORE_BRANCH=%s\n' "${br}" >> $GITHUB_ENV
          printf 'GO_VERSION=%s\n' "$(test "${#gv}" -gt 4 && echo "${gv%.*}" || echo "${gv}")" >> $GITHUB_ENV
          printf '%s/.local/go/bin\n' "$HOME" >> $GITHUB_PATH
      - uses: docker/setup-qemu-action@v1
        with:
          platforms: all
      - uses: docker/setup-buildx-action@v1
      - uses: actions/setup-go@v2
        with:
          go-version: ${{ steps.setup.outputs.go_version }}
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - uses: ./.github/actions/go-cache
        with:
          go: ${{ steps.setup.outputs.go_version }}
      - id: mod
        run: ./.github/script/nightly-module.sh
      - id: novelty
        uses: actions/cache@v2
        with:
          path: go.sum
          key: novelty-${{ github.sha }}-${{ hashFiles('./go.*') }}
      - uses: docker/login-action@v1
        if: steps.setup.outputs.push && steps.novelty.outputs.cache-hit != 'true'
        with:
          registry: quay.io
          username: ${{ secrets.QUAY_USER }}
          password: ${{ secrets.QUAY_TOKEN }}
      - uses: docker/build-push-action@v2
        with:
          build-args: |
            GO_VERSION=${{ env.GO_VERSION }}
            CLAIR_VERSION=${{ steps.mod.outputs.clair_version }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
          context: .
          platforms: linux/amd64,linux/arm64
          push: ${{ steps.setup.outputs.push && steps.novelty.outputs.cache-hit != 'true' }}
          tags: |
            quay.io/${{ steps.setup.outputs.repo }}:${{ steps.setup.outputs.tag }}
            quay.io/${{ steps.setup.outputs.repo }}:${{ steps.setup.outputs.tag }}-${{ steps.setup.outputs.date }}
      - uses: ./.github/actions/set-image-expiration
        if: steps.setup.outputs.push && steps.setup.outputs.api && steps.novelty.outputs.cache-hit != 'true'
        with:
          repo: ${{ steps.setup.outputs.repo }}
          tag: ${{ steps.setup.outputs.tag }}-${{ steps.setup.outputs.date }}
          token: ${{ secrets.QUAY_API_TOKEN }}
