name: Reusable store image name and tags

permissions:
  contents: read

on:
  workflow_call:
    outputs:
      commit_tag:
        value: ${{ jobs.store_image_name_and_tags.outputs.commit_tag }}
      last_commit_tag:
        value: ${{ jobs.store_image_name_and_tags.outputs.last_commit_tag }}
      develop_tag:
        value: ${{ jobs.store_image_name_and_tags.outputs.develop_tag }}

concurrency:
  group: store-image-name-and-tags-${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:
  store_image_name_and_tags:
    runs-on: gha-runner-scale-set-ubuntu-22.04-amd64-small
    name: Compute version tags
    env:
      # REF_NAME: ${{ github.ref_name }}
      EVENT_BEFORE: ${{ github.event.before }}
    outputs:
      commit_tag: ${{ steps.step1.outputs.COMMIT_TAG }}
      last_commit_tag: ${{ steps.step1.outputs.LAST_COMMIT_TAG }}
      develop_tag: ${{ steps.step1.outputs.DEVELOP_TAG }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Compute version tags
        id: step1
        run: |
          # For PR, GITHUB_SHA is NOT the last commit pushed onto the PR branch - https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#pull_request
          if [[ "${{ github.event_name }}" == "pull_request" ]]; then
            echo "COMMIT_TAG=$(git rev-parse --short=7 ${{ github.event.pull_request.head.sha }})" >> $GITHUB_OUTPUT
          else
            echo "COMMIT_TAG=$(git rev-parse --short=7 $GITHUB_SHA)" >> $GITHUB_OUTPUT
          fi
          echo LAST_COMMIT_TAG=$(git rev-parse --short=7 "${{ env.EVENT_BEFORE }}") >> $GITHUB_OUTPUT
          echo DEVELOP_TAG=develop >> $GITHUB_OUTPUT
      - name: Show version tags
        id: step2
        run: |
          echo "COMMIT_TAG: ${{ steps.step1.outputs.COMMIT_TAG }}"
          echo "LAST_COMMIT_TAG: ${{ steps.step1.outputs.LAST_COMMIT_TAG }}"
          echo "DEVELOP_TAG: ${{ steps.step1.outputs.DEVELOP_TAG }}"
          echo "github.ref: ${{ github.ref }}"
