name: Release docker image based on (1) commit sha (2) release tag version (3) :latest

on:
  release:
    types: [published]

env:
  IMAGE_TAG_LATEST: 'aulaapp/aula-frontend:latest'
  IMAGE_TAG_VERSION: 'aulaapp/aula-frontend:${{ github.event.release.tag_name }}'

jobs:
  build:
    runs-on: [self-hosted, cicd]
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

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

      - name: Add IMAGE_TAG_SHORT_SHA env property with commit short sha
        run: echo "IMAGE_TAG_SHORT_SHA=aulaapp/aula-frontend:$(git rev-parse --short HEAD)" >> $GITHUB_ENV

      - name: Build and tag Docker images
        run: docker build -t $IMAGE_TAG_SHORT_SHA -t $IMAGE_TAG_LATEST -t $IMAGE_TAG_VERSION --build-arg DOCKER_TAG=$IMAGE_TAG_VERSION .

      - name: Push Docker image tags
        run: |
          docker push $IMAGE_TAG_SHORT_SHA && \
          docker push $IMAGE_TAG_VERSION && \
          docker push $IMAGE_TAG_LATEST
