name: Deploy service message-delivery-method-web-push
run-name: ${{ github.actor }} deployed to build branch
on:
  push:
    branches:
      - main
    paths:
      - 'services/message-delivery-method-web-push/**'
      - 'packages/couchdb-changes-stream/**'
      - '.gitea/workflows/deploy-service-message-delivery-method-web-push.yaml'
      - 'docker-compose.yml'
      - 'package.json'
      - 'pnpm-lock.yaml'

jobs:
  service message-delivery-method-web-push:
    runs-on: ubuntu-latest

    env:
      COUCHDB_USER: ${{ secrets.COUCHDB_USER }}
      COUCHDB_PASSWORD: ${{ secrets.COUCHDB_PASSWORD }}
      VAPID_SUBJECT: ${{ vars.VAPID_SUBJECT }}
      GCM_API_KEY: ${{ vars.GCM_API_KEY }}

    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: install dependencies
        run: |
          corepack enable pnpm && corepack use pnpm@9.15.0
          pnpm install --filter ./services/message-delivery-method-web-push

      - name: build service
        run: |
          pnpm run --filter ./services/message-delivery-method-web-push build
          pnpm --filter ./services/message-delivery-method-web-push deploy --prod build/service-message-delivery-method-web-push

      - name: build docker image
        run: MESSAGE_DELIVERY_METHOD_WEB_PUSH_TAG=commit-${{env.GITHUB_SHA}} docker compose -p hereconnect build message-delivery-method-web-push

      - name: start container
        run: MESSAGE_DELIVERY_METHOD_WEB_PUSH_TAG=commit-${{env.GITHUB_SHA}} docker compose -p hereconnect up -d --pull never --wait message-delivery-method-web-push

      - name: rename to latest
        run: docker tag hereconnect/service-message-delivery-method-web-push:commit-${{env.GITHUB_SHA}} hereconnect/service-message-delivery-method-web-push:latest

      - name: rollback on failure
        if: failure()
        env:
          COUCHDB_TAG: latest
        run: docker compose -p hereconnect up -d --pull never --wait message-delivery-method-web-push

      - name: cleanup docker image
        if: always()
        run: docker rmi hereconnect/service-message-delivery-method-web-push:commit-${{env.GITHUB_SHA}}
