name: Deploy to GCP
on:
  repository_dispatch:
    types: gcp-deployment

jobs:
  build:
    runs-on: ubuntu-20.04
    env:
      API_URI: /graphql/
      APP_MOUNT_URI: /dashboard/
      STATIC_URL: /dashboard/static/
      SENTRY_ORG: saleor
      SENTRY_PROJECT: dashboard
      SENTRY_URL_PREFIX: "~/dashboard/static"
      ENVIRONMENT: ${{ github.event.client_payload.deployment_name }}
      DEPLOYMENT_TARGET: ${{ github.event.client_payload.deployment_target }}
      VERSION_FAMILY: ${{ github.event.client_payload.version_family }}
      SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
      SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
      APPS_MARKETPLACE_API_URI: "https://apps.saleor.io/api/v2/saleor-apps"
      IS_CLOUD_INSTANCE: true
    steps:
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.event.client_payload.ref }}
      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version-file: ".nvmrc"
      - name: Package
        timeout-minutes: 15
        run: |
          npm ci
      - name: Build
        run: |
          npm run build
      - uses: google-github-actions/auth@v0
        with:
          credentials_json: ${{ secrets.GCP_DASHBOARD_PROD_PRIVATE_KEY }}
      - name: Set up Cloud SDK
        uses: google-github-actions/setup-gcloud@v0
      - name: Deploy
        run: |
          DOTLESS_VERSION_FAMILY=$(echo $VERSION_FAMILY | tr -d .)

          gcloud storage cp -r build/dashboard/*  gs://saleor-cloud-eu-dashboard/${DEPLOYMENT_TARGET}-v${DOTLESS_VERSION_FAMILY}/static/
          gcloud storage cp build/dashboard/index.html gs://saleor-cloud-eu-dashboard/${DEPLOYMENT_TARGET}-v${DOTLESS_VERSION_FAMILY}/index.html

          gcloud compute url-maps invalidate-cdn-cache saleor-cloud-eu --path "/dashboard" --async
          gcloud compute url-maps invalidate-cdn-cache saleor-cloud-eu --path "/dashboard/*" --async
      - name: Notify Slack
        if: ${{ always() }}
        env:
          JOB_DEPLOYMENT_KIND: production
          JOB_STATUS: ${{ job.status }}
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_CLOUD_DEPLOYMENTS_WEBHOOK_URL }}
          JOB_TITLE: "Dashboard deployment to ${{ env.ENVIRONMENT }}"
        run: |
          python3 ./.github/workflows/notify/notify-slack.py
