---

# Please see contrib/buildahimage/README.md for details on the intentions
# of this workflow.

name: build multi-arch images

on:
  # Upstream buildah tends to be very active, with many merges per day.
  # Only run this daily via cron schedule, or manually, not by branch push.
  schedule:
    - cron:  '0 8 * * *'
  # allows to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  multi:
    name: multi-arch Buildah build
    env:
      BUILDAH_QUAY_REGISTRY: quay.io/buildah
      CONTAINERS_QUAY_REGISTRY: quay.io/containers
      # list of architectures for build
      PLATFORMS: linux/amd64,linux/s390x,linux/ppc64le,linux/arm64

    # build several images (upstream, testing, stable) in parallel
    strategy:
      # By default, failure of one matrix item cancels all others
      fail-fast: false
      matrix:
        # Builds are located under contrib/buildahimage/<source> directory
        source:
          - upstream
          - testing
          - stable
    runs-on: ubuntu-latest
    # internal registry caches build for inspection before push
    services:
      registry:
        image: quay.io/libpod/registry:2
        ports:
          - 5000:5000
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
        with:
          driver-opts: network=host
          install: true

      - name: Build and locally push Buildah
        uses: docker/build-push-action@v2
        with:
          context: contrib/buildahimage/${{ matrix.source }}
          file: ./contrib/buildahimage/${{ matrix.source }}/Dockerfile
          platforms: ${{ env.PLATFORMS }}
          push: true
          tags: localhost:5000/buildah/${{ matrix.source }}

      # Simple verification that stable images work, and
      # also grab version number use in forming the FQIN.
      - name: amd64 container sniff test
        if: matrix.source == 'stable'
        id: sniff_test
        run: |
          VERSION_OUTPUT="$(docker run localhost:5000/buildah/${{ matrix.source }} \
                            buildah --storage-driver=vfs version)"
          echo "$VERSION_OUTPUT"
          VERSION=$(grep -Em1 '^Version: ' <<<"$VERSION_OUTPUT" | awk '{print $2}')
          test -n "$VERSION"
          echo "::set-output name=version::${VERSION}"

      - name: Generate buildah reg. image FQIN(s)
        id: buildah_reg
        run: |
          if [[ "${{ matrix.source }}" == 'stable' ]]; then
            # The `buildah version` in image just built
            VERSION='v${{ steps.sniff_test.outputs.version }}'
            # workaround vim syntax-highlight bug: '
            # Image tags previously pushed to quay
            ALLTAGS=$(skopeo list-tags \
                      docker://$BUILDAH_QUAY_REGISTRY/stable | \
                      jq -r '.Tags[]')

            # New image? Push quay.io/buildah/stable:vX.X.X and :latest
            if ! fgrep -qx "$VERSION" <<<"$ALLTAGS"; then
              # Assume version-tag is also the most up to date (i.e. "latest")
              FQIN="$BUILDAH_QUAY_REGISTRY/stable:$VERSION,$BUILDAH_QUAY_REGISTRY/stable:latest"
            else # Not a new version-tagged image
              # Assume other contents changed, so this is the "new" latest.
              FQIN="$BUILDAH_QUAY_REGISTRY/stable:latest"
            fi
          elif [[ "${{ matrix.source }}" == 'testing' ]]; then
            # Assume some contents changed, always push latest testing.
            FQIN="$BUILDAH_QUAY_REGISTRY/testing:latest"
          elif [[ "${{ matrix.source }}" == 'upstream' ]]; then
            # Assume some contents changed, always push latest upstream.
            FQIN="$BUILDAH_QUAY_REGISTRY/upstream:latest"
          else
            echo "::error::Unknown matrix item '${{ matrix.source }}'"
            exit 1
          fi
          echo "::warning::Pushing $FQIN"
          echo "::set-output name=fqin::${FQIN}"
          echo '::set-output name=push::true'

      # This is substantially the same as the above step, except the
      # $CONTAINERS_QUAY_REGISTRY is used and the "testing"
      # flavor is never pushed.
      - name: Generate containers reg. image FQIN(s)
        if: matrix.source != 'testing'
        id: containers_reg
        run: |
          if [[ "${{ matrix.source }}" == 'stable' ]]; then
            VERSION='v${{ steps.sniff_test.outputs.version }}'
            # workaround vim syntax-highlight bug: '
            ALLTAGS=$(skopeo list-tags \
                      docker://$CONTAINERS_QUAY_REGISTRY/buildah | \
                      jq -r '.Tags[]')

            # New image? Push quay.io/containers/buildah:vX.X.X and :latest
            if ! fgrep -qx "$VERSION" <<<"$ALLTAGS"; then
              FQIN="$CONTAINERS_QUAY_REGISTRY/buildah:$VERSION,$CONTAINERS_QUAY_REGISTRY/buildah:latest"
            else # Not a new version-tagged image, but contents may be updated
              FQIN="$CONTAINERS_QUAY_REGISTRY/buildah:latest"
            fi
          elif [[ "${{ matrix.source }}" == 'upstream' ]]; then
            FQIN="$CONTAINERS_QUAY_REGISTRY/buildah:latest"
          else
            echo "::error::Unknown matrix item '${{ matrix.source }}'"
            exit 1
          fi
          echo "::warning::Pushing $FQIN"
          echo "::set-output name=fqin::${FQIN}"
          echo '::set-output name=push::true'

      - name: Define LABELS multi-line env. var. value
        run: |
          # This is a really hacky/strange workflow idiom, required
          # for setting multi-line $LABELS value for consumption in
          # a future step.
          # https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#multiline-strings
          cat << EOF | tee -a $GITHUB_ENV
          LABELS<<DELIMITER
          org.opencontainers.image.source=https://github.com/${{ github.repository }}.git
          org.opencontainers.image.revision=${{ github.sha }}
          org.opencontainers.image.created=$(date -u --iso-8601=seconds)
          DELIMITER
          EOF

      # Separate steps to login and push for buildah and containers quay
      # repositories are required, because 2 sets of credentials are used and `docker
      # login` as well as `buildah login` do not support having 2 different
      # credential sets for 1 registry.
      # At the same time reuse of non-shell steps is not supported by Github Actions
      # via anchors or composite actions

      # Push to 'buildah' Quay repo for stable, testing. and upstream
      - name: Login to 'buildah' Quay registry
        uses: docker/login-action@v1
        if: steps.buildah_reg.outputs.push == 'true'
        with:
          registry: ${{ env.BUILDAH_QUAY_REGISTRY }}
          # N/B: Secrets are not passed to workflows that are triggered
          #      by a pull request from a fork
          username: ${{ secrets.BUILDAH_QUAY_USERNAME }}
          password: ${{ secrets.BUILDAH_QUAY_PASSWORD }}

      - name: Push images to 'buildah' Quay
        uses: docker/build-push-action@v2
        if: steps.buildah_reg.outputs.push == 'true'
        with:
          cache-from: type=registry,ref=localhost:5000/buildah/${{ matrix.source }}
          cache-to: type=inline
          context: contrib/buildahimage/${{ matrix.source }}
          file: ./contrib/buildahimage/${{ matrix.source }}/Dockerfile
          platforms: ${{ env.PLATFORMS }}
          push: true
          tags: ${{ steps.buildah_reg.outputs.fqin }}
          labels: |
            ${{ env.LABELS }}

      # Push to 'containers' Quay repo only stable buildah
      - name: Login to 'containers' Quay registry
        if: steps.containers_reg.outputs.push == 'true'
        uses: docker/login-action@v1
        with:
          registry: ${{ env.CONTAINERS_QUAY_REGISTRY}}
          username: ${{ secrets.CONTAINERS_QUAY_USERNAME }}
          password: ${{ secrets.CONTAINERS_QUAY_PASSWORD }}

      - name: Push images to 'containers' Quay
        if: steps.containers_reg.outputs.push == 'true'
        uses: docker/build-push-action@v2
        with:
          cache-from: type=registry,ref=localhost:5000/buildah/${{ matrix.source }}
          cache-to: type=inline
          context: contrib/buildahimage/${{ matrix.source }}
          file: ./contrib/buildahimage/${{ matrix.source }}/Dockerfile
          platforms: ${{ env.PLATFORMS }}
          push: true
          tags: ${{ steps.containers_reg.outputs.fqin }}
          labels: |
            ${{ env.LABELS }}
