name: Release
on:
  release:
    types: [released]
  workflow_dispatch:
    inputs:
      TAG_NAME:
        description: "Tag name that the major tag will point to"
        required: true

env:
  TAG_NAME: ${{ github.event.inputs.TAG_NAME || github.event.release.tag_name }}
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

permissions:
  contents: write

jobs:
  verify_release:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - name: Grep action.yaml content
        id: grep-image-content
        run: |
          image=$(grep -E "jekyll-build-pages.*\'" action.yml)
          echo "image=$image" >> $GITHUB_OUTPUT
      - uses: actions-ecosystem/action-regex-match@v2
        id: regex-match
        with:
          text: ${{ steps.grep-image-content.outputs.image }}
          regex: "jekyll-build-pages:(.*)'"
      - name: Version match
        run: |
          if [ "${{ steps.regex-match.outputs.group1 }}" != "${{ env.TAG_NAME }}" ]; then
            echo "version mismatch. action.yaml with version ${{ steps.regex-match.outputs.group1 }} Tag version ${{ env.TAG_NAME }}";
            exit 1
          fi
      - name: Verify image published
        run: docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.TAG_NAME }}

  update_tag:
    needs: verify_release
    name: Update the major tag to include the ${{ github.event.inputs.TAG_NAME || github.event.release.tag_name }} changes
    environment:
      # Note: this environment is protected
      name: Release
    runs-on: ubuntu-latest
    outputs:
      major_tag: ${{ steps.update-major-tag.outputs.major-tag }}
    steps:
      - name: Update the ${{ env.TAG_NAME }} major tag
        id: update-major-tag
        uses: actions/publish-action@v0.3.0
        with:
          source-tag: ${{ env.TAG_NAME }}
