name: Tag Docker Version

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  workflow_call:
    secrets:
      GH_PACKAGES:
        required: true

jobs:
  tag_images:
    runs-on: ubuntu-latest
    timeout-minutes: 80
    permissions:
      contents: read
      packages: write
      deployments: write
    steps:
      - name: Login to docker
        env:
          GH_ACTOR: implerhq
          GH_PASSWORD: ${{ secrets.GH_PACKAGES }}
        run: |
          echo $GH_PASSWORD | docker login ghcr.io -u $GH_ACTOR --password-stdin 
      
      - name: Get Latest Docker Version
        run: |  # Multi-line command definition
          #!/bin/bash
          # (Your script content from previous response)
          version=$(curl -sSL https://api.github.com/repos/implerhq/impler.io/releases/latest | jq -r '.tag_name' | grep -oP 'v\K\d+(?:\.\d+){2}')
          echo "DOCKER_VERSION=$version" >> $GITHUB_ENV  # Add to environment variables    
          
      - name: Tag API
        env:
          REGISTERY_OWNER: implerhq
          DOCKER_NAME: impler/api
          DOCKER_VERSION: ${{ env.DOCKER_VERSION }}
        run: |
          docker pull ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:latest
          docker tag ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:latest ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:$DOCKER_VERSION
          docker push ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:$DOCKER_VERSION
          echo "::set-output name=IMAGE::ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:$DOCKER_VERSION"

      - name: Tag Queue Manager
        env:
          REGISTERY_OWNER: implerhq
          DOCKER_NAME: impler/queue-manager
          DOCKER_VERSION: ${{ env.DOCKER_VERSION }}
        run: |
          docker pull ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:latest
          docker tag ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:latest ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:$DOCKER_VERSION
          docker push ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:$DOCKER_VERSION
          echo "::set-output name=IMAGE::ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:$DOCKER_VERSION"

      - name: Tag EMBED
        env:
          REGISTERY_OWNER: implerhq
          DOCKER_NAME: impler/embed
          DOCKER_VERSION: ${{ env.DOCKER_VERSION }}
        run: |
          docker pull ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:latest
          docker tag ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:latest ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:$DOCKER_VERSION
          docker push ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:$DOCKER_VERSION
          echo "::set-output name=IMAGE::ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:$DOCKER_VERSION"

      - name: Tag WIDGET
        env:
          REGISTERY_OWNER: implerhq
          DOCKER_NAME: impler/widget
          DOCKER_VERSION: ${{ env.DOCKER_VERSION }}
        run: |
          docker pull ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:latest
          docker tag ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:latest ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:$DOCKER_VERSION
          docker push ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:$DOCKER_VERSION
          echo "::set-output name=IMAGE::ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:$DOCKER_VERSION"

      - name: Tag Web
        env:
          REGISTERY_OWNER: implerhq
          DOCKER_NAME: impler/web
          DOCKER_VERSION: ${{ env.DOCKER_VERSION }}
        run: |
          docker pull ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:latest
          docker tag ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:latest ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:$DOCKER_VERSION
          docker push ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:$DOCKER_VERSION
          echo "::set-output name=IMAGE::ghcr.io/$REGISTERY_OWNER/$DOCKER_NAME:$DOCKER_VERSION"
