name: Tests and Build

on:
  push:
    branches: [ "*" ]
    tags: [ "v*" ]
  pull_request:
    branches: [ main ]
jobs:
  tests:
    name: Tests
    runs-on: ubuntu-latest

    steps:
    - name: Checkout
      uses: actions/checkout@v3
      with:
        # fetching all tags is required for the Makefile to compute the right version
        fetch-depth: 0

    - name: Set up Go
      uses: actions/setup-go@v2
      with:
        go-version: 1.18

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

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v1

    - name: Setup dependencies
      run: sudo apt update && sudo apt install -y util-linux udev parted e2fsprogs mount tar extlinux qemu-utils

    - name: Share cache with other actions
      uses: actions/cache@v2
      with:
        path: |
          ~/go/pkg/mod
          /tmp/.buildx-cache
        key: ${{ runner.os }}-tests-${{ github.sha }}
        restore-keys: |
          ${{ runner.os }}-tests-

    - name: Run tests
      run: make tests

  build:
    name: Build
    runs-on: ubuntu-latest

    steps:
    - name: Checkout
      uses: actions/checkout@v3
      with:
        # fetching all tags is required for the Makefile to compute the right version
        fetch-depth: 0

    - name: Set up Go
      uses: actions/setup-go@v2
      with:
        go-version: 1.18

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

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v1

    - name: Login to Docker Hub
      uses: docker/login-action@v1
      if: startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main'
      with:
        username: ${{ secrets.DOCKER_HUB_USERNAME }}
        password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

    - name: Share cache with other actions
      uses: actions/cache@v2
      with:
        path: |
          ~/go/pkg/mod
          /tmp/.buildx-cache
        key: ${{ runner.os }}-build-${{ github.sha }}
        restore-keys: |
          ${{ runner.os }}-build-

    - name: Ensure all files were well formatted
      run: make check-fmt

    - name: Import GPG key
      id: import_gpg
      uses: crazy-max/ghaction-import-gpg@v4
      with:
        gpg_private_key: ${{ secrets.GPG_KEY }}
        passphrase: ${{ secrets.GPG_PASSWORD }}

    - name: Build Snapshot
      run: make build-snapshot

    - name: Release Snapshot
      env:
        GITHUB_TOKEN: ${{ secrets.REPOSITORIES_ACCESS_TOKEN }}
        GPG_PASSWORD: ${{ secrets.GPG_PASSWORD }}
        GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }}
      run: make release-snapshot

  build-image:
    name: Build Docker Image
    runs-on: ubuntu-18.04
    steps:
    - name: Checkout
      uses: actions/checkout@v3
      with:
        # fetching all tags is required for the Makefile to compute the right version
        fetch-depth: 0

    - name: Set up Go
      uses: actions/setup-go@v2
      with:
        go-version: 1.18

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

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v1

    - name: Login to Docker Hub
      uses: docker/login-action@v1
      if: startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main'
      with:
        username: ${{ secrets.DOCKER_HUB_USERNAME }}
        password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

    - name: Share cache with other actions
      uses: actions/cache@v2
      with:
        path: |
          ~/go/pkg/mod
          /tmp/.buildx-cache
        key: ${{ runner.os }}-build-image-${{ github.sha }}
        restore-keys: |
          ${{ runner.os }}-build-image-

    - name: Build Docker images
      run: make docker-build

    - name: Push Docker images
      if: github.ref == 'refs/heads/main'
      run: make docker-push

  release:
    name: Release Binaries
    runs-on: ubuntu-18.04
    if: startsWith(github.event.ref, 'refs/tags/v')
    needs:
    - tests
    - build

    steps:
    - name: Checkout
      uses: actions/checkout@v3
      with:
        # fetching all tags is required for the Makefile to compute the right version
        fetch-depth: 0

    - name: Set up Go
      uses: actions/setup-go@v2
      with:
        go-version: 1.18

    - name: Share cache with other actions
      uses: actions/cache@v2
      with:
        path: |
          ~/go/pkg/mod
          /tmp/.buildx-cache
        key: ${{ runner.os }}-build-${{ github.sha }}
        restore-keys: |
          ${{ runner.os }}-build-

    - name: Import GPG key
      id: import_gpg
      uses: crazy-max/ghaction-import-gpg@v4
      with:
        gpg_private_key: ${{ secrets.GPG_KEY }}
        passphrase: ${{ secrets.GPG_PASSWORD }}

    - name: Build binaries
      run: make build

    - name: Release binaries
      env:
        GITHUB_TOKEN: ${{ secrets.REPOSITORIES_ACCESS_TOKEN }}
        GPG_PASSWORD: ${{ secrets.GPG_PASSWORD }}
        GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }}
      run: make release

  release-image:
    name: Release Docker Image
    runs-on: ubuntu-18.04
    if: startsWith(github.event.ref, 'refs/tags/v')
    needs:
    - tests
    - build-image

    steps:
    - name: Checkout
      uses: actions/checkout@v3
      with:
        # fetching all tags is required for the Makefile to compute the right version
        fetch-depth: 0

    - name: Set up Go
      uses: actions/setup-go@v2
      with:
        go-version: 1.18

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

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v1

    - name: Login to Docker Hub
      uses: docker/login-action@v1
      with:
        username: ${{ secrets.DOCKER_HUB_USERNAME }}
        password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

    - name: Share cache with other actions
      uses: actions/cache@v2
      with:
        path: |
          ~/go/pkg/mod
          /tmp/.buildx-cache
        key: ${{ runner.os }}-build-image-${{ github.sha }}
        restore-keys: |
          ${{ runner.os }}-build-image-

    - name: Build Docker images
      run: make docker-build

    - name: Release Docker images
      run: make docker-push
