name: Build Docker image
permissions:
  contents: write

env:
  SKIP_MAKE_SETUP_CHECK: 'true'

on:
  push:
    # Sequence of patterns matched against refs/tags
    tags:
      - '*' # Push events to matching v*, i.e. v1.0, v20.15.10

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  release:
    name: Create Release
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v3
        with:
          # by default, it uses a depth of 1
          # this fetches all history so that we can read each commit
          fetch-depth: 0
      - name: Generate Changelog
        run: .gitea/release_message.sh > release_message.md
      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          body_path: release_message.md

  deploy:
    needs: release
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Set up dotnet
      uses: actions/setup-dotnet@v4
      with:
        dotnet-version: '9.0.X'
    - name: Check version match
      run: |
        REPOSITORY_NAME=$(echo "$GITHUB_REPOSITORY" | awk -F '/' '{print $2}' | tr '-' '_')
        if [ "$(cat $REPOSITORY_NAME/VERSION)" = "${GITHUB_REF_NAME}" ] ; then
          echo "Version matches successfully!"
        else
          echo "Version must match!"
          exit -1
        fi
    - name: Login to Gitea container registry
      uses: docker/login-action@v3
      with:
        username: gitearobot
        password: ${{ secrets.PACKAGE_GITEA_PAT }}
        registry: git.disi.dev
    - name: Login to HomeLab NuGet feed
      uses: sersoft-gmbh/dotnet-nuget-login-action@v1
      with:
          registry-url: 'https://git.disi.dev/api/packages/HomeLab/nuget/index.json'
          registry-name: HomeLab
          username: gitearobot
          password: ${{ secrets.PACKAGE_GITEA_PAT }}
    - name: Build and publish
      run: |
        REPOSITORY_OWNER=$(echo "$GITHUB_REPOSITORY" | awk -F '/' '{print $1}' | tr '[:upper:]' '[:lower:]')
        REPOSITORY_NAME=$(echo "$GITHUB_REPOSITORY" | awk -F '/' '{print $2}' | tr '-' '_')
        docker build -t "git.disi.dev/$REPOSITORY_OWNER/song-of-the-day:$(cat $REPOSITORY_NAME/VERSION)" ./song_of_the_day
        docker push "git.disi.dev/$REPOSITORY_OWNER/song-of-the-day:$(cat $REPOSITORY_NAME/VERSION)"