name: Build
on:
  push:
    branches: [main]

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  release:
    name: Release
    runs-on: ubuntu-latest
    outputs:
      new_tag: ${{ steps.tag_version.outputs.new_tag }}
    steps:
      - uses: actions/checkout@v4
      - name: Bump version and push tag
        id: tag_version
        uses: mathieudutour/github-tag-action@v6.2
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          default_bump: minor
      - name: Create a GitHub release
        uses: ncipollo/release-action@v1
        with:
          tag: ${{ steps.tag_version.outputs.new_tag }}
          name: ${{ steps.tag_version.outputs.new_tag }}
          body: ${{ steps.tag_version.outputs.changelog }}
          generateReleaseNotes: true

  publish:
    name: GitHub Container Registry
    runs-on: ubuntu-latest
    needs: release
    permissions:
      contents: read
      packages: write
    steps:
      - uses: actions/checkout@v4
      - name: Login to GitHub Container Registry
        run: echo ${{ secrets.GITHUB_TOKEN }} | docker login ${{ env.REGISTRY }} -u ${{ github.actor }} --password-stdin
      - name: Build image for GitHub Package Registry
        run: |
          docker build . --file Dockerfile \
          --build-arg "version=${{ needs.release.outputs.new_tag }}" \
          --tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.release.outputs.new_tag }} \
          --tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
      - name: Push image to GitHub Package Registry
        run: |
          docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
          docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.release.outputs.new_tag }}

  # deploy:
  #   name: Update Portainer Deployment
  #   runs-on: ubuntu-latest
  #   needs: publish
  #   steps:
  #     - uses: fjogeleit/http-request-action@v1
  #       with:
  #         url: ${{ secrets.PORTAINER_WEBHOOK_URL }}
  #         method: POST
  #         timeout: 60000
  #         preventFailureOnNoResponse: "true"