deploy-prod.yml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. # Optional. Next encrypts what a server action closes over with a key
  53. # made fresh by every build, so a tab left open across a deploy can
  54. # no longer be read by the new container. A fixed key keeps those tabs
  55. # working. 32 random bytes, base64: `openssl rand -base64 32`. Left
  56. # unset, the build's own key is used, as before.
  57. NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: ${{ secrets.CLOUD_NEXT_SERVER_ACTIONS_ENCRYPTION_KEY }}
  58. GOOGLE_INTEGRATION_CLIENT_ID: ${{ secrets.CLOUD_GOOGLE_INTEGRATION_CLIENT_ID }}
  59. GOOGLE_INTEGRATION_CLIENT_SECRET: ${{ secrets.CLOUD_GOOGLE_INTEGRATION_CLIENT_SECRET }}
  60. GOOGLE_AUTH_CLIENT_ID: ${{ secrets.CLOUD_GOOGLE_AUTH_CLIENT_ID }}
  61. GOOGLE_AUTH_CLIENT_SECRET: ${{ secrets.CLOUD_GOOGLE_AUTH_CLIENT_SECRET }}
  62. MICROSOFT_INTEGRATION_CLIENT_ID: ${{ secrets.CLOUD_MICROSOFT_INTEGRATION_CLIENT_ID }}
  63. MICROSOFT_INTEGRATION_CLIENT_SECRET: ${{ secrets.CLOUD_MICROSOFT_INTEGRATION_CLIENT_SECRET }}
  64. ZOOM_INTEGRATION_CLIENT_ID: ${{ secrets.CLOUD_ZOOM_INTEGRATION_CLIENT_ID }}
  65. ZOOM_INTEGRATION_CLIENT_SECRET: ${{ secrets.CLOUD_ZOOM_INTEGRATION_CLIENT_SECRET }}
  66. QUICKBOOKS_INTEGRATION_CLIENT_ID: ${{ secrets.CLOUD_QUICKBOOKS_INTEGRATION_CLIENT_ID }}
  67. QUICKBOOKS_INTEGRATION_CLIENT_SECRET: ${{ secrets.CLOUD_QUICKBOOKS_INTEGRATION_CLIENT_SECRET }}
  68. run: |
  69. mkdir -p $HOME/torqvoice-deploy/prod
  70. cd $HOME/torqvoice-deploy/prod
  71. 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_|NEXT_SERVER_ACTIONS_)' > .env
  72. cat > docker-compose.yml << 'COMPOSE'
  73. networks:
  74. proxy:
  75. external: true
  76. name: proxy
  77. services:
  78. torqvoice-app:
  79. container_name: torqvoice-app
  80. image: ghcr.io/torqvoice/torqvoice:${APP_TAG:-latest}
  81. restart: unless-stopped
  82. volumes:
  83. - ${DATA_PATH}/torqvoice_data:/app/data
  84. expose:
  85. - "3000"
  86. environment:
  87. VIRTUAL_HOST: ${VIRTUAL_HOST}
  88. VIRTUAL_PORT: "3000"
  89. LETSENCRYPT_HOST: ${VIRTUAL_HOST}
  90. DATABASE_URL: ${DATABASE_URL}
  91. BETTER_AUTH_SECRET: ${BETTER_AUTH_SECRET}
  92. NEXT_PUBLIC_APP_URL: ${APP_URL}
  93. TORQVOICE_MODE: cloud
  94. # Production sits behind Cloudflare and the origin only accepts its
  95. # edges, so the visitor address is the one Cloudflare says it is.
  96. # Without this every visitor shares the edge's rate-limit bucket.
  97. TRUST_CF_CONNECTING_IP: "true"
  98. TORQVOICE_SERVICE_SECRET: ${TORQVOICE_SERVICE_SECRET}
  99. TORQVOICE_CLOUD_TOKEN: ${TORQVOICE_CLOUD_TOKEN}
  100. TORQVOICE_COM_URL: https://torqvoice.com
  101. POSTHOG_HOST: ${POSTHOG_HOST}
  102. POSTHOG_KEY: ${POSTHOG_KEY}
  103. BACKUP_HEARTBEAT_TOKEN: ${BACKUP_HEARTBEAT_TOKEN}
  104. INTEGRATIONS_ENCRYPTION_KEY: ${INTEGRATIONS_ENCRYPTION_KEY}
  105. NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: ${NEXT_SERVER_ACTIONS_ENCRYPTION_KEY}
  106. GOOGLE_INTEGRATION_CLIENT_ID: ${GOOGLE_INTEGRATION_CLIENT_ID}
  107. GOOGLE_INTEGRATION_CLIENT_SECRET: ${GOOGLE_INTEGRATION_CLIENT_SECRET}
  108. GOOGLE_AUTH_CLIENT_ID: ${GOOGLE_AUTH_CLIENT_ID}
  109. GOOGLE_AUTH_CLIENT_SECRET: ${GOOGLE_AUTH_CLIENT_SECRET}
  110. MICROSOFT_INTEGRATION_CLIENT_ID: ${MICROSOFT_INTEGRATION_CLIENT_ID}
  111. MICROSOFT_INTEGRATION_CLIENT_SECRET: ${MICROSOFT_INTEGRATION_CLIENT_SECRET}
  112. ZOOM_INTEGRATION_CLIENT_ID: ${ZOOM_INTEGRATION_CLIENT_ID}
  113. ZOOM_INTEGRATION_CLIENT_SECRET: ${ZOOM_INTEGRATION_CLIENT_SECRET}
  114. QUICKBOOKS_INTEGRATION_CLIENT_ID: ${QUICKBOOKS_INTEGRATION_CLIENT_ID}
  115. QUICKBOOKS_INTEGRATION_CLIENT_SECRET: ${QUICKBOOKS_INTEGRATION_CLIENT_SECRET}
  116. networks:
  117. - proxy
  118. COMPOSE
  119. # Without the cloud token the new container would come up as a
  120. # self-hosted install: one workshop for everybody, branding back on.
  121. # Stop here, before anything is replaced.
  122. if [ -z "$TORQVOICE_CLOUD_TOKEN" ]; then
  123. 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."
  124. exit 1
  125. fi
  126. # The link to torqvoice.com is what the subscription pages hang on. A
  127. # wrong secret or an origin the site does not list only shows up as a
  128. # missing page, so the deploy asks the site itself and says so.
  129. if [ -n "$TORQVOICE_SERVICE_SECRET" ]; then
  130. status=$(curl -sS -o /dev/null -w '%{http_code}' -X POST https://torqvoice.com/api/app/subscription/ping \
  131. -H "Authorization: Bearer $TORQVOICE_SERVICE_SECRET" -H 'Content-Type: application/json' \
  132. -d "{\"appUrl\":\"$APP_URL\"}" || echo 000)
  133. if [ "$status" != "200" ]; then
  134. 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."
  135. else
  136. echo "torqvoice.com link confirmed for $APP_URL"
  137. fi
  138. else
  139. echo "::warning title=torqvoice.com link not configured::TORQVOICE_SERVICE_SECRET is empty, so plans cannot be bought from this deployment."
  140. fi
  141. - name: Ensure data directory permissions
  142. run: |
  143. DATA_DIR="${{ secrets.DATA_PATH }}/torqvoice_data"
  144. mkdir -p "$DATA_DIR"
  145. docker run --rm -v "$DATA_DIR":/data alpine chown -R 1001:1001 /data
  146. - name: Check free disk space
  147. run: |
  148. MIN_GB=5
  149. DOCKER_ROOT=$(docker info --format '{{.DockerRootDir}}' 2>/dev/null || echo /var/lib/docker)
  150. AVAIL_KB=$(df -Pk "$DOCKER_ROOT" | awk 'NR==2 {print $4}')
  151. AVAIL_GB=$(awk "BEGIN {printf \"%.1f\", $AVAIL_KB/1024/1024}")
  152. echo "Free space on $DOCKER_ROOT: ${AVAIL_GB} GB (minimum ${MIN_GB} GB)"
  153. if [ "$AVAIL_KB" -lt $((MIN_GB * 1024 * 1024)) ]; then
  154. echo "::error::Not enough disk space to pull image: ${AVAIL_GB} GB free, need ${MIN_GB} GB. Aborting deploy."
  155. exit 1
  156. fi
  157. - name: Back up production database
  158. env:
  159. DATABASE_URL: ${{ secrets.CLOUD_DATABASE_URL }}
  160. run: |
  161. set -euo pipefail
  162. DB_USER=$(echo "$DATABASE_URL" | sed -E 's|^[^:]+://([^:]+):.*|\1|')
  163. DB_NAME=$(echo "$DATABASE_URL" | sed -E 's|.*/([^/?]+)(\?.*)?$|\1|')
  164. BACKUP_DIR="${{ secrets.DATA_PATH }}/db-backups/$DB_NAME"
  165. # The volume is root-owned and the runner is not root. Binding the
  166. # path into a container makes Docker create it, and the chown hands
  167. # it to the runner so the dump redirect below can write there.
  168. docker run --rm -v "$BACKUP_DIR":/b alpine chown "$(id -u):$(id -g)" /b
  169. # The Postgres data directory lives on this filesystem: filling it
  170. # takes the database down, not just the backup.
  171. MIN_GB=2
  172. AVAIL_KB=$(df -Pk "$BACKUP_DIR" | awk 'NR==2 {print $4}')
  173. if [ "$AVAIL_KB" -lt $((MIN_GB * 1024 * 1024)) ]; then
  174. echo "::error::Only $(awk "BEGIN {printf \"%.1f\", $AVAIL_KB/1024/1024}") GB free where the database lives. Refusing to dump."
  175. exit 1
  176. fi
  177. DUMP="$BACKUP_DIR/${DB_NAME}-$(date +%Y%m%d-%H%M%S)-pre-${{ steps.tag.outputs.value }}.dump"
  178. echo "Dumping $DB_NAME to $DUMP"
  179. docker exec torqvoice-db pg_dump -U "$DB_USER" -d "$DB_NAME" -Fc > "$DUMP"
  180. # A dump nobody can read is worse than no dump. No backup, no deploy.
  181. docker exec -i torqvoice-db pg_restore --list < "$DUMP" > /dev/null
  182. echo "Verified $(du -h "$DUMP" | cut -f1) backup"
  183. # Keep the 10 most recent pre-deploy dumps; hourly dumps in the same
  184. # folder belong to the backup cron and age out there.
  185. ls -t "$BACKUP_DIR"/*-pre-*.dump 2>/dev/null | tail -n +11 | xargs -r rm --
  186. - name: Deploy
  187. run: |
  188. cd $HOME/torqvoice-deploy/prod
  189. APP_TAG=${{ steps.tag.outputs.value }} docker compose up -d --pull always
  190. - name: Health check
  191. run: |
  192. echo "Waiting for prod container to be ready..."
  193. for i in $(seq 1 12); do
  194. STATUS=$(docker inspect --format='{{.State.Status}}' torqvoice-app 2>/dev/null || echo "not found")
  195. if [ "$STATUS" = "running" ]; then
  196. echo "Deployed ${{ steps.tag.outputs.value }}."
  197. exit 0
  198. fi
  199. echo "Status: $STATUS, retrying in 5s..."
  200. sleep 5
  201. done
  202. echo "::error::Container failed to start"
  203. docker logs torqvoice-app --tail 50
  204. exit 1
  205. - name: Prune old images and build cache
  206. run: docker image prune -f && docker builder prune -f