name: release
run-name: release ${{ github.actor }}
on:
    push:
        tags:
            - "version-[0-9]+.[0-9]+.[0-9]+"
    workflow_dispatch:
env:
    REGISTRY: docker.frappet.com
    IMAGE_NAME: ehr/bma-ehr-recruit-exam-service
    DEPLOY_HOST: frappet.com
    DEPLOY_PORT: 10102
    COMPOSE_PATH: /home/frappet/docker/bma/bma-ehr-recruit-exam

jobs:
    # act workflow_dispatch -W .github/workflows/release.yaml --input IMAGE_VER=test-v6.1 -s DOCKER_USER=sorawit -s DOCKER_PASS=P@ssword -s SSH_PASSWORD=P@ssw0rd
    release:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v3
            # skip Set up QEMU because it fail on act and container
            # Gen Version try to get version from tag or inut
            - name: Set output tags
              id: vars
              run: echo "tag=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT
            - name: Gen Version
              id: gen_ver
              run: |
                if [[ $GITHUB_REF == 'refs/tags/'* ]]; then
                  IMAGE_VER=${{ steps.vars.outputs.tag }}
                else
                  IMAGE_VER=${{ github.event.inputs.IMAGE_VER }}
                fi
                if [[ $IMAGE_VER == '' ]]; then
                  IMAGE_VER='test-vBeta'
                fi
                echo '::set-output name=image_ver::'$IMAGE_VER
            - name: Check Version
              run: |
                  echo $GITHUB_REF
                  echo ${{ steps.gen_ver.outputs.image_ver }}

            - name: Set up Docker Buildx
              uses: docker/setup-buildx-action@v2
            - name: Login in to registry
              uses: docker/login-action@v2
              with:
                  registry: ${{env.REGISTRY}}
                  username: ${{secrets.DOCKER_USER}}
                  password: ${{secrets.DOCKER_PASS}}
            - name: Build and push docker image
              uses: docker/build-push-action@v3
              with:
                  context: .
                  platforms: linux/amd64
                  push: true
                  tags: ${{env.REGISTRY}}/${{env.IMAGE_NAME}}:${{ steps.gen_ver.outputs.image_ver }},${{env.REGISTRY}}/${{env.IMAGE_NAME}}:latest

            - name: Reload docker compose
              uses: appleboy/ssh-action@v0.1.8
              with:
                  host: ${{env.DEPLOY_HOST}}
                  username: frappet
                  password: ${{ secrets.SSH_PASSWORD }}
                  port: 10102
                  script: |
                      cd "${{env.COMPOSE_PATH}}"
                      docker compose pull
                      docker compose up -d
                      echo "${{ steps.gen_ver.outputs.image_ver }}"> success
            - name: Notify Discord Success
              if: success()
              run: |
                curl -H "Content-Type: application/json" \
                -X POST \
                -d '{
                  "embeds": [{
                    "title": "✅ Deployment Success!",
                    "description": "**Details:**\n- Image: `${{env.IMAGE_NAME}}`\n- Version: `${{ steps.gen_ver.outputs.image_ver }}`\n- Deployed by: `${{github.actor}}`",
                    "color": 3066993,
                    "footer": {
                      "text": "Release Notification",
                      "icon_url": "https://example.com/success-icon.png"
                    },
                    "timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'"
                  }]
                }' \
                ${{ secrets.DISCORD_WEBHOOK }}

            - name: Notify Discord Failure
              if: failure()
              run: |
                curl -H "Content-Type: application/json" \
                -X POST \
                -d '{
                  "embeds": [{
                    "title": "❌ Deployment Failed!",
                    "description": "**Details:**\n- Image: `${{env.IMAGE_NAME}}`\n- Version: `${{ steps.gen_ver.outputs.image_ver }}`\n- Attempted by: `${{github.actor}}`",
                    "color": 15158332,
                    "footer": {
                      "text": "Release Notification",
                      "icon_url": "https://example.com/failure-icon.png"
                    },
                    "timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'"
                  }]
                }' \
                ${{ secrets.DISCORD_WEBHOOK }}
