name: Release Docker image

on:
  push:
    tags:
      - "*"
    branches:
      - "*"

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

jobs:
  release:
    name: release
    runs-on: ubuntu-latest
    if: ${{ success() && (contains(github.ref, 'refs/tags') || github.ref == 'refs/heads/master') }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Get Tag Version
        id: version
        shell: bash
        run: |
          if [[ "${GITHUB_REF}" == "refs/heads/master" ]]; then
            echo "::set-output name=TAG_VERSION::latest"
          else
            echo "::set-output name=TAG_VERSION::${GITHUB_REF##*/}"
          fi
      - name: Extract branch name
        id: extract_branch
        shell: bash
        run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
      - name: Log in to the container registry
        uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Build and push image using branch name
        uses: docker/build-push-action@v3
        with:
          context: .
          file: docker/Dockerfile
          push: true
          tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.version.outputs.TAG_VERSION }}