---
name: "Build & Release pipeline"

on:
  pull_request:
  workflow_dispatch:
  push:
    tags:
      - '*'
    branches:
      - main
      - next
      - beta

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up QEMU
        id: qemu
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:latest
          platforms: arm64,arm

      - name: Setup node
        uses: actions/setup-node@v4
        with:
          cache: 'npm'
          cache-dependency-path: frontend/package-lock.json

      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version-file: 'service/go.mod'
          cache: true
          cache-dependency-path: 'service/go.mod'

      - name: Print go version
        run: go version

      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_KEY }}

      - name: Login to ghcr
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.CONTAINER_TOKEN }}

      - name: get date
        run: |
          echo "DATE=$(date +'%Y-%m-%d')" >> "$GITHUB_ENV"

      - name: make webui
        run: make -w webui-dist

      - name: unit tests
        run: make -w service-unittests

      - name: build service
        run: make -w service

      - name: integration tests
        run: cd integration-tests && make -w

      - name: Archive integration tests
        uses: actions/upload-artifact@v4.3.1
        if: always()
        with:
          name: "OliveTin-integration-tests-${{ env.DATE }}-${{ github.sha }}"
          path: |
            integration-tests
            !integration-tests/node_modules

      - name: Install goreleaser
        uses: goreleaser/goreleaser-action@v6
        with:
          install-only: true

      - name: release
        if: github.ref_type != 'tag'
        uses: cycjimmy/semantic-release-action@v4
        with:
          extra_plugins: |
            @semantic-release/commit-analyzer
            @semantic-release/exec
            @semantic-release/git
        env:
          GITHUB_TOKEN: ${{ secrets.CONTAINER_TOKEN }}
          GH_TOKEN: ${{ secrets.CONTAINER_TOKEN }}

      - name: Archive binaries
        uses: actions/upload-artifact@v4.3.1
        with:
          name: "OliveTin-snapshot-${{ env.DATE }}-${{ github.sha }}"
          path: dist/OliveTin*.*

