# https://github.com/marketplace/actions/deploy-to-cloudflare-workers-with-wrangler
# https://github.com/cloudflare/wrangler-action
name: Deploy to Cloudflare Workers with Wrangler

on:
  push:
    branches:
      - master

permissions:
  contents: read

concurrency:
  group: cloudflare-workers-master
  cancel-in-progress: true

jobs:
  deploy:
    runs-on: ubuntu-latest
    name: Deploy
    steps:
    - name: Checkout
      uses: actions/checkout@v6

    - name: Use Node.js 24
      uses: actions/setup-node@v6
      with:
        node-version-file: .nvmrc
        cache: npm
        cache-dependency-path: |
          doesitarm-default/package-lock.json
          workers/analytics/package-lock.json
  
    - name: Write Wrangler configs
      shell: bash
      run: |
          set -euo pipefail
          umask 077

          printf '%s' '${{ secrets.WRANGLER_ENV }}' | base64 --decode > doesitarm-default/.env
          printf '%s' '${{ secrets.WRANGLER_TOML }}' | base64 --decode > doesitarm-default/wrangler.toml
          printf '%s' '${{ secrets.ANALYTICS_WRANGER_TOML }}' | base64 --decode > workers/analytics/wrangler.toml

    - name: Install default worker dependencies
      working-directory: doesitarm-default
      run: npm ci

    - name: Install analytics worker dependencies
      working-directory: workers/analytics
      run: npm ci

    - name: Deploy Default Worker
      uses: cloudflare/wrangler-action@1.3.0
      with:
          apiToken: ${{ secrets.CF_API_TOKEN }}
          workingDirectory: 'doesitarm-default'

    - name: Deploy Analytics Worker
      uses: cloudflare/wrangler-action@1.3.0
      with:
          apiToken: ${{ secrets.CF_API_TOKEN }}
          workingDirectory: 'workers/analytics'
