# Tagging based on: https://docs.docker.com/build/ci/github-actions/manage-tags-labels/
# Multi platform based on: https://docs.docker.com/build/ci/github-actions/multi-platform/
name: build
on:
  push:
    branches:
      - "**"
    tags:
      - "v*"
  pull_request:

jobs:
  inverter_gui_pipeline:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: "Setup"
        uses: actions/setup-go@v3
        with:
          go-version: '1.22'

      - name: Lint
        uses: golangci/golangci-lint-action@v3
        with:
          version: latest

      - name: Test
        run: go test -v -race -coverprofile=coverage.txt -covermode=atomic  ./...

      - name: Upload code coverage
        uses: codecov/codecov-action@v3

      - name: Generate docker image labels and tags
        id: docker_meta
        uses: docker/metadata-action@v4
        with:
          images: ghcr.io/diebietse/invertergui
          tags: |
            type=ref,event=branch
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}

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

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

      - name: Login to GitHub Container Registry
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push to GitHub Container Registry
        uses: docker/build-push-action@v4
        with:
          platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.docker_meta.outputs.tags }}
          labels: ${{ steps.docker_meta.outputs.labels }}
