deploy-prod.yml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. name: "[Prod] Deploy"
  2. on:
  3. workflow_dispatch:
  4. inputs:
  5. tag:
  6. description: "Version tag to deploy (e.g. v1.2.39). Leave blank to deploy the newest published release. 'latest' is refused."
  7. required: false
  8. default: ""
  9. jobs:
  10. deploy:
  11. runs-on: [self-hosted, Linux, X64, hetzner]
  12. steps:
  13. - name: Resolve version tag
  14. id: tag
  15. run: |
  16. TAG="${{ inputs.tag }}"
  17. if [ -z "$TAG" ]; then
  18. # Newest published release (drafts are excluded, so publishing the
  19. # release in GitHub is the gate that makes it deployable).
  20. TAG=$(curl -fsSL -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
  21. "https://api.github.com/repos/${{ github.repository }}/releases/latest" \
  22. | grep -m1 '"tag_name"' | cut -d'"' -f4)
  23. echo "Resolved newest published release: $TAG"
  24. fi
  25. if [ -z "$TAG" ] || [ "$TAG" = "latest" ]; then
  26. echo "::error::No deployable version tag ('latest' is refused; rollback needs a named target)."
  27. exit 1
  28. fi
  29. echo "value=$TAG" >> "$GITHUB_OUTPUT"
  30. - name: Log in to GitHub Container Registry
  31. run: echo "${{ secrets.GHCR_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
  32. - name: Create deployment files
  33. env:
  34. VIRTUAL_HOST: ${{ secrets.CLOUD_VIRTUAL_HOST }}
  35. DATABASE_URL: ${{ secrets.CLOUD_DATABASE_URL }}
  36. BETTER_AUTH_SECRET: ${{ secrets.CLOUD_BETTER_AUTH_SECRET }}
  37. APP_URL: ${{ secrets.CLOUD_APP_URL }}
  38. # Plans are sold on torqvoice.com. This secret is the one torqvoice.com
  39. # holds as TORQVOICE_SERVICE_SECRET; the app signs checkout handoffs
  40. # with it and authenticates billing calls with it. The Stripe keys
  41. # themselves live in this app's database and are edited from the
  42. # torqvoice.com admin, so they are no longer passed here.
  43. TORQVOICE_SERVICE_SECRET: ${{ secrets.TORQVOICE_SERVICE_SECRET }}
  44. # Signed by torqvoice.com for APP_URL (scripts/mint-cloud-token.mjs there).
  45. # Without it the app ignores TORQVOICE_MODE=cloud and runs self-hosted.
  46. TORQVOICE_CLOUD_TOKEN: ${{ secrets.CLOUD_TORQVOICE_CLOUD_TOKEN }}
  47. DATA_PATH: ${{ secrets.DATA_PATH }}
  48. POSTHOG_HOST: ${{ secrets.NEXT_PUBLIC_POSTHOG_HOST }}
  49. POSTHOG_KEY: ${{ secrets.NEXT_PUBLIC_POSTHOG_KEY }}
  50. BACKUP_HEARTBEAT_TOKEN: ${{ secrets.BACKUP_HEARTBEAT_TOKEN }}
  51. INTEGRATIONS_ENCRYPTION_KEY: ${{ secrets.CLOUD_INTEGRATIONS_ENCRYPTION_KEY }}
  52. GOOGLE_INTEGRATION_CLIENT_ID: ${{ secrets.CLOUD_GOOGLE_INTEGRATION_CLIENT_ID }}
  53. GOOGLE_INTEGRATION_CLIENT_SECRET: ${{ secrets.CLOUD_GOOGLE_INTEGRATION_CLIENT_SECRET }}
  54. GOOGLE_AUTH_CLIENT_ID: ${{ secrets.CLOUD_GOOGLE_AUTH_CLIENT_ID }}
  55. GOOGLE_AUTH_CLIENT_SECRET: ${{ secrets.CLOUD_GOOGLE_AUTH_CLIENT_SECRET }}
  56. MICROSOFT_INTEGRATION_CLIENT_ID: ${{ secrets.CLOUD_MICROSOFT_INTEGRATION_CLIENT_ID }}
  57. MICROSOFT_INTEGRATION_CLIENT_SECRET: ${{ secrets.CLOUD_MICROSOFT_INTEGRATION_CLIENT_SECRET }}
  58. ZOOM_INTEGRATION_CLIENT_ID: ${{ secrets.CLOUD_ZOOM_INTEGRATION_CLIENT_ID }}
  59. ZOOM_INTEGRATION_CLIENT_SECRET: ${{ secrets.CLOUD_ZOOM_INTEGRATION_CLIENT_SECRET }}
  60. QUICKBOOKS_INTEGRATION_CLIENT_ID: ${{ secrets.CLOUD_QUICKBOOKS_INTEGRATION_CLIENT_ID }}
  61. QUICKBOOKS_INTEGRATION_CLIENT_SECRET: ${{ secrets.CLOUD_QUICKBOOKS_INTEGRATION_CLIENT_SECRET }}
  62. run: |
  63. mkdir -p $HOME/torqvoice-deploy/prod
  64. cd $HOME/torqvoice-deploy/prod
  65. env | grep -E '^(VIRTUAL_HOST|DATA_PATH|DATABASE_URL|BETTER_AUTH_SECRET|APP_URL|TORQVOICE_|POSTHOG_|BACKUP_|INTEGRATIONS_|GOOGLE_INTEGRATION_|GOOGLE_AUTH_|MICROSOFT_INTEGRATION_|ZOOM_INTEGRATION_|QUICKBOOKS_INTEGRATION_)' > .env
  66. cat > docker-compose.yml << 'COMPOSE'
  67. networks:
  68. proxy:
  69. external: true
  70. name: proxy
  71. services:
  72. torqvoice-app:
  73. container_name: torqvoice-app
  74. image: ghcr.io/torqvoice/torqvoice:${APP_TAG:-latest}
  75. restart: unless-stopped
  76. volumes:
  77. - ${DATA_PATH}/torqvoice_data:/app/data
  78. expose:
  79. - "3000"
  80. environment:
  81. VIRTUAL_HOST: ${VIRTUAL_HOST}
  82. VIRTUAL_PORT: "3000"
  83. LETSENCRYPT_HOST: ${VIRTUAL_HOST}
  84. DATABASE_URL: ${DATABASE_URL}
  85. BETTER_AUTH_SECRET: ${BETTER_AUTH_SECRET}
  86. NEXT_PUBLIC_APP_URL: ${APP_URL}
  87. TORQVOICE_MODE: cloud
  88. # Production sits behind Cloudflare and the origin only accepts its
  89. # edges, so the visitor address is the one Cloudflare says it is.
  90. # Without this every visitor shares the edge's rate-limit bucket.
  91. TRUST_CF_CONNECTING_IP: "true"
  92. TORQVOICE_SERVICE_SECRET: ${TORQVOICE_SERVICE_SECRET}
  93. TORQVOICE_CLOUD_TOKEN: ${TORQVOICE_CLOUD_TOKEN}
  94. TORQVOICE_COM_URL: https://torqvoice.com
  95. POSTHOG_HOST: ${POSTHOG_HOST}
  96. POSTHOG_KEY: ${POSTHOG_KEY}
  97. BACKUP_HEARTBEAT_TOKEN: ${BACKUP_HEARTBEAT_TOKEN}
  98. INTEGRATIONS_ENCRYPTION_KEY: ${INTEGRATIONS_ENCRYPTION_KEY}
  99. GOOGLE_INTEGRATION_CLIENT_ID: ${GOOGLE_INTEGRATION_CLIENT_ID}
  100. GOOGLE_INTEGRATION_CLIENT_SECRET: ${GOOGLE_INTEGRATION_CLIENT_SECRET}
  101. GOOGLE_AUTH_CLIENT_ID: ${GOOGLE_AUTH_CLIENT_ID}
  102. GOOGLE_AUTH_CLIENT_SECRET: ${GOOGLE_AUTH_CLIENT_SECRET}
  103. MICROSOFT_INTEGRATION_CLIENT_ID: ${MICROSOFT_INTEGRATION_CLIENT_ID}
  104. MICROSOFT_INTEGRATION_CLIENT_SECRET: ${MICROSOFT_INTEGRATION_CLIENT_SECRET}
  105. ZOOM_INTEGRATION_CLIENT_ID: ${ZOOM_INTEGRATION_CLIENT_ID}
  106. ZOOM_INTEGRATION_CLIENT_SECRET: ${ZOOM_INTEGRATION_CLIENT_SECRET}
  107. QUICKBOOKS_INTEGRATION_CLIENT_ID: ${QUICKBOOKS_INTEGRATION_CLIENT_ID}
  108. QUICKBOOKS_INTEGRATION_CLIENT_SECRET: ${QUICKBOOKS_INTEGRATION_CLIENT_SECRET}
  109. networks:
  110. - proxy
  111. COMPOSE
  112. # Without the cloud token the new container would come up as a
  113. # self-hosted install: one workshop for everybody, branding back on.
  114. # Stop here, before anything is replaced.
  115. if [ -z "$TORQVOICE_CLOUD_TOKEN" ]; then
  116. echo "::error title=Cloud token missing::CLOUD_TORQVOICE_CLOUD_TOKEN is empty. Mint one for $APP_URL with scripts/mint-cloud-token.mjs in the torqvoice.com repo."
  117. exit 1
  118. fi
  119. # The link to torqvoice.com is what the subscription pages hang on. A
  120. # wrong secret or an origin the site does not list only shows up as a
  121. # missing page, so the deploy asks the site itself and says so.
  122. if [ -n "$TORQVOICE_SERVICE_SECRET" ]; then
  123. status=$(curl -sS -o /dev/null -w '%{http_code}' -X POST https://torqvoice.com/api/app/subscription/ping \
  124. -H "Authorization: Bearer $TORQVOICE_SERVICE_SECRET" -H 'Content-Type: application/json' \
  125. -d "{\"appUrl\":\"$APP_URL\"}" || echo 000)
  126. if [ "$status" != "200" ]; then
  127. echo "::warning title=torqvoice.com link not confirmed::ping answered $status. Check TORQVOICE_SERVICE_SECRET on both sides and that $APP_URL is TORQVOICE_APP_URL or TORQVOICE_STAGING_APP_URL on torqvoice.com. The subscription page stays hidden until it is."
  128. else
  129. echo "torqvoice.com link confirmed for $APP_URL"
  130. fi
  131. else
  132. echo "::warning title=torqvoice.com link not configured::TORQVOICE_SERVICE_SECRET is empty, so plans cannot be bought from this deployment."
  133. fi
  134. - name: Ensure data directory permissions
  135. run: |
  136. DATA_DIR="${{ secrets.DATA_PATH }}/torqvoice_data"
  137. mkdir -p "$DATA_DIR"
  138. docker run --rm -v "$DATA_DIR":/data alpine chown -R 1001:1001 /data
  139. - name: Check free disk space
  140. run: |
  141. MIN_GB=5
  142. DOCKER_ROOT=$(docker info --format '{{.DockerRootDir}}' 2>/dev/null || echo /var/lib/docker)
  143. AVAIL_KB=$(df -Pk "$DOCKER_ROOT" | awk 'NR==2 {print $4}')
  144. AVAIL_GB=$(awk "BEGIN {printf \"%.1f\", $AVAIL_KB/1024/1024}")
  145. echo "Free space on $DOCKER_ROOT: ${AVAIL_GB} GB (minimum ${MIN_GB} GB)"
  146. if [ "$AVAIL_KB" -lt $((MIN_GB * 1024 * 1024)) ]; then
  147. echo "::error::Not enough disk space to pull image: ${AVAIL_GB} GB free, need ${MIN_GB} GB. Aborting deploy."
  148. exit 1
  149. fi
  150. - name: Back up production database
  151. env:
  152. DATABASE_URL: ${{ secrets.CLOUD_DATABASE_URL }}
  153. run: |
  154. set -euo pipefail
  155. DB_USER=$(echo "$DATABASE_URL" | sed -E 's|^[^:]+://([^:]+):.*|\1|')
  156. DB_NAME=$(echo "$DATABASE_URL" | sed -E 's|.*/([^/?]+)(\?.*)?$|\1|')
  157. BACKUP_DIR="${{ secrets.DATA_PATH }}/db-backups/$DB_NAME"
  158. # The volume is root-owned and the runner is not root. Binding the
  159. # path into a container makes Docker create it, and the chown hands
  160. # it to the runner so the dump redirect below can write there.
  161. docker run --rm -v "$BACKUP_DIR":/b alpine chown "$(id -u):$(id -g)" /b
  162. # The Postgres data directory lives on this filesystem: filling it
  163. # takes the database down, not just the backup.
  164. MIN_GB=2
  165. AVAIL_KB=$(df -Pk "$BACKUP_DIR" | awk 'NR==2 {print $4}')
  166. if [ "$AVAIL_KB" -lt $((MIN_GB * 1024 * 1024)) ]; then
  167. echo "::error::Only $(awk "BEGIN {printf \"%.1f\", $AVAIL_KB/1024/1024}") GB free where the database lives. Refusing to dump."
  168. exit 1
  169. fi
  170. DUMP="$BACKUP_DIR/${DB_NAME}-$(date +%Y%m%d-%H%M%S)-pre-${{ steps.tag.outputs.value }}.dump"
  171. echo "Dumping $DB_NAME to $DUMP"
  172. docker exec torqvoice-db pg_dump -U "$DB_USER" -d "$DB_NAME" -Fc > "$DUMP"
  173. # A dump nobody can read is worse than no dump. No backup, no deploy.
  174. docker exec -i torqvoice-db pg_restore --list < "$DUMP" > /dev/null
  175. echo "Verified $(du -h "$DUMP" | cut -f1) backup"
  176. # Keep the 10 most recent pre-deploy dumps; hourly dumps in the same
  177. # folder belong to the backup cron and age out there.
  178. ls -t "$BACKUP_DIR"/*-pre-*.dump 2>/dev/null | tail -n +11 | xargs -r rm --
  179. - name: Deploy
  180. run: |
  181. cd $HOME/torqvoice-deploy/prod
  182. APP_TAG=${{ steps.tag.outputs.value }} docker compose up -d --pull always
  183. - name: Health check
  184. run: |
  185. echo "Waiting for prod container to be ready..."
  186. for i in $(seq 1 12); do
  187. STATUS=$(docker inspect --format='{{.State.Status}}' torqvoice-app 2>/dev/null || echo "not found")
  188. if [ "$STATUS" = "running" ]; then
  189. echo "Deployed ${{ steps.tag.outputs.value }}."
  190. exit 0
  191. fi
  192. echo "Status: $STATUS, retrying in 5s..."
  193. sleep 5
  194. done
  195. echo "::error::Container failed to start"
  196. docker logs torqvoice-app --tail 50
  197. exit 1
  198. - name: Prune old images and build cache
  199. run: docker image prune -f && docker builder prune -f