name: build

on:
  workflow_dispatch:
  push:
    branches:
      - 'master'
      - 'v[0-9]*'
    tags:
      - 'v*'
  pull_request:
    branches:
      - 'master'
      - 'v[0-9]*'

env:
  REPO_SLUG: "docker/buildx-bin"
  RELEASE_OUT: "./release-out"

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout
        uses: actions/checkout@v3
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v2
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
        with:
          version: latest
      -
        name: Test
        run: |
          make test
      -
        name: Send to Codecov
        uses: codecov/codecov-action@v3
        with:
          file: ./coverage/coverage.txt
      -
        name: Expose GitHub Runtime
        uses: crazy-max/ghaction-github-runtime@94085cef04e835448ac610090128515e781b4939
      -
        name: Build binaries
        run: |
          make release
        env:
          CACHE_FROM: type=gha,scope=release
          CACHE_TO: type=gha,scope=release
      -
        name: Upload artifacts
        uses: actions/upload-artifact@v3
        with:
          name: buildx
          path: ${{ env.RELEASE_OUT }}/*
          if-no-files-found: error
      -
        name: Docker meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            ${{ env.REPO_SLUG }}
          tags: |
            type=ref,event=branch
            type=ref,event=pr
            type=semver,pattern={{version}}
          bake-target: meta-helper
      -
        name: Login to DockerHub
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Build and push image
        uses: docker/bake-action@v2
        with:
          files: |
            ./docker-bake.hcl
            ${{ steps.meta.outputs.bake-file }}
          targets: image-cross
          push: ${{ github.event_name != 'pull_request' }}
      -
        name: GitHub Release
        if: startsWith(github.ref, 'refs/tags/v')
        uses: softprops/action-gh-release@1e07f4398721186383de40550babbdf2b84acfc5
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          draft: true
          files: ${{ env.RELEASE_OUT }}/*

  buildkit-edge:
    runs-on: ubuntu-latest
    continue-on-error: true
    steps:
      -
        name: Checkout
        uses: actions/checkout@v3
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v2
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
        with:
          version: latest
          driver-opts: image=moby/buildkit:master
          buildkitd-flags: --debug
      -
        # Just run a bake target to check eveything runs fine
        name: Build
        uses: docker/bake-action@v2
        with:
          targets: binaries-cross
