name: Build and Release
run-name: ${{ gitea.actor }} is building on ${{ gitea.repository }} 🚀
on:
  workflow_dispatch:

env:
  DOCKER_HUB_NAMESPACE: christiangoeschel
  DOCKER_HUB_REPO: portfolio

jobs:
  setup-version-build-publish:
    name: Setup, Build, and Publish
    runs-on: ubuntu-latest
    outputs:
      version_tag: ${{ steps.versioning.outputs.VERSION_TAG }}

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup act_runner git identity
        run: |
          git config --global user.email "act_runner@foo.bar"
          git config --global user.name "act_runner"

      - name: Install git-cliff
        run: |
          chmod +x ./install-cliff.sh
          ./install-cliff.sh
        working-directory: ./scripts

      - id: versioning
        name: Bump version
        run: |
          echo "VERSION_TAG=$(/usr/local/bin/git-cliff --bumped-version 2>/dev/null | tr -d 'v')" >> "${GITHUB_OUTPUT}"
          echo "New version: ${VERSION}"
        env:
          VERSION: ${{ steps.versioning.outputs.VERSION_TAG }}

      - name: Generate CHANGELOG
        run: |
          /usr/local/bin/git-cliff --bump -o CHANGELOG.md
          git add CHANGELOG.md

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

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

      - name: Login to Docker Hub
        run: |
          echo "${{ secrets.DOCKER_HUB_TOKEN }}" | docker login -u ${{ vars.DOCKER_HUB_USERNAME }} --password-stdin

      - name: Build and Push
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile
          push: true
          tags: ${{ env.DOCKER_HUB_NAMESPACE }}/${{ env.DOCKER_HUB_REPO }}:${{ steps.versioning.outputs.VERSION_TAG }}
          platforms: linux/amd64,linux/arm64,linux/arm64/v8

    # - name: Build image
    #   run: |
    #     docker build -t ${DOCKER_HUB_NAMESPACE}/${DOCKER_HUB_REPO}:${VERSION} .
    #     docker push ${DOCKER_HUB_NAMESPACE}/${DOCKER_HUB_REPO}:${VERSION}
    #   env:
    #     VERSION: ${{ steps.versioning.outputs.VERSION_TAG }}

      - name: Publish CHANGELOG and push new tag to 'main'
        run: |
          git commit -m "ci: Release ${VERSION} and update CHANGELOG"
          git tag "v${VERSION}"
          git push origin main
          git push --tags
        env:
          VERSION: ${{ steps.versioning.outputs.VERSION_TAG }}
