name: Release (Production)

on:
  workflow_dispatch: {}
  push:
    tags:
      - "v[0-9]+.[0-9]+.[0-9]+"
      - "**/v[0-9]+.[0-9]+.[0-9]+"

permissions:
  contents: write

jobs:
  lint:
    name: Lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-go@v5
        with:
          go-version: stable
      - name: golangci-lint
        uses: golangci/golangci-lint-action@v6
        with:
          version: v1.60

  test:
    name: Test
    strategy:
      matrix:
        go-version:
          - 1.23.x
        platform:
          - ubuntu-latest
          - macos-latest
          - windows-latest
    runs-on: ${{ matrix.platform }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: 1.23.x
      - name: Run tests
        run: |
          go test ./... -coverprofile coverage.out

  build:
    name: Build
    runs-on: ubuntu-latest
    needs:
      - test
      - lint
    env:
      CGO_ENABLED: 0
      TAG: ${{ github.ref_name }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: 1.23.x
      - name: Login to Docker Hub
        uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Build
        uses: goreleaser/goreleaser-action@9ed2f89a662bf1735a48bc8557fd212fa902bebf # v6
        with:
          version: v0.155.0
          args: --debug
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Enable experimental docker features
        run: |
          mkdir -p ~/.docker/ && \
          echo '{"experimental": "enabled"}' > ~/.docker/config.json
      - name: Create manifest for version
        run: |
          export DH_TAG=$(git tag --points-at HEAD | sed 's/^v*//')
          docker manifest create \
            beatkind/watchtower:$DH_TAG \
            beatkind/watchtower:amd64-$DH_TAG \
            beatkind/watchtower:i386-$DH_TAG \
            beatkind/watchtower:armhf-$DH_TAG \
            beatkind/watchtower:arm64v8-$DH_TAG
      - name: Create manifest for latest
        run: |
          docker manifest create \
            beatkind/watchtower:latest \
            beatkind/watchtower:amd64-latest \
            beatkind/watchtower:i386-latest \
            beatkind/watchtower:armhf-latest \
            beatkind/watchtower:arm64v8-latest
      - name: Push manifests to Dockerhub
        env:
          DOCKER_USER: ${{ secrets.DOCKERHUB_USERNAME }}
          DOCKER_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
        run: |
          docker login -u $DOCKER_USER -p $DOCKER_TOKEN && \
            docker manifest push beatkind/watchtower:$(echo $TAG | sed 's/^v*//') && \
            docker manifest push beatkind/watchtower:latest
