Skip to content

Commit

Permalink
Merge pull request #1226 from andrew-bierman/feat/preview-version-of-api
Browse files Browse the repository at this point in the history
Automatically deploy backend api based on branch update.
  • Loading branch information
andrew-bierman authored Sep 13, 2024
2 parents b2ceae2 + da63314 commit 28ced46
Show file tree
Hide file tree
Showing 5 changed files with 349 additions and 294 deletions.
141 changes: 41 additions & 100 deletions .github/actions/setup-wrangler-toml/action.yml
Original file line number Diff line number Diff line change
@@ -1,77 +1,44 @@
name: 'Generate Wrangler TOML'
description: 'Generates the wrangler.toml file for Cloudflare Workers'
inputs:
GOOGLE_CLIENT_ID:
description: 'Google Client ID'
APP_URL:
description: 'The Application URL'
required: true
GOOGLE_CLIENT_SECRET:
description: 'Google Client Secret'
required: true
STMP_EMAIL:
description: 'SMTP Email'
AWS_SIGN_ALGORITHM:
description: 'The AWS sign algorithm'
required: true
STMP_PASSWORD:
description: 'SMTP Password'
BUCKET_ENDPOINT:
description: 'The bucket endpoint'
required: true
JWT_SECRET:
description: 'JWT Secret'
BUCKET_NAME:
description: 'The bucket name'
required: true
SEND_GRID_API_KEY:
description: 'Send Grid API Key'
BUCKET_REGION:
description: 'The bucket region'
required: true
MAPBOX_ACCESS_TOKEN:
description: 'Mapbox Access Token'
BUCKET_SERVICE:
description: 'The bucket service'
required: true
OPENWEATHER_KEY:
description: 'OpenWeather Key'
BUCKET_SESSION_TOKEN:
description: 'The bucket session token'
required: true
VECTORIZE_API_KEY:
description: 'Vectorize API Key'
environment:
description: 'The current environment'
required: true
CLOUDFLARE_ACCOUNT_ID:
description: 'Cloudflare Account ID'
required: true
OPENAI_API_KEY:
description: 'OpenAI API Key'
required: true
PREVIEW_DB_ID:
description: 'Preview DB ID'
required: true
PRODUCTION_DB_ID:
description: 'Production DB ID'
required: true
PREVIEW_GOOGLE_CLIENT_ID:
description: 'Preview Google Client ID'
required: true
PREVIEW_GOOGLE_CLIENT_SECRET:
description: 'Preview Google Client Secret'
required: true
PREVIEW_STMP_EMAIL:
description: 'Preview SMTP Email'
required: true
PREVIEW_STMP_PASSWORD:
description: 'Preview SMTP Password'
required: true
PREVIEW_JWT_SECRET:
description: 'Preview JWT Secret'
required: true
PREVIEW_SEND_GRID_API_KEY:
description: 'Preview Send Grid API Key'
required: true
PREVIEW_MAPBOX_ACCESS_TOKEN:
description: 'Preview Mapbox Access Token'
required: true
PREVIEW_OPENWEATHER_KEY:
description: 'Preview OpenWeather Key'
DB_ID:
description: 'The database ID'
required: true
PREVIEW_VECTORIZE_API_KEY:
description: 'Preview Vectorize API Key'
GOOGLE_CLIENT_ID:
description: 'Google Client ID'
required: true
PREVIEW_CLOUDFLARE_ACCOUNT_ID:
description: 'Preview Cloudflare Account ID'
STMP_EMAIL:
description: 'SMTP Email'
required: true
PREVIEW_OPENAI_API_KEY:
description: 'Preview OpenAI API Key'
VECTOR_INDEX_NAME:
description: 'The vector index'
required: true
runs:
using: 'composite'
Expand All @@ -83,53 +50,27 @@ runs:
echo 'main = "src/index.ts"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'compatibility_date = "2024-03-14"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'node_compat = true' >> ${{ github.workspace }}/server/wrangler.toml
echo '[env.preview]' >> ${{ github.workspace }}/server/wrangler.toml
echo '[[env.preview.d1_databases]]' >> ${{ github.workspace }}/server/wrangler.toml
echo '[env.${{ inputs.environment }}]' >> ${{ github.workspace }}/server/wrangler.toml
echo '[[env.${{ inputs.environment }}.d1_databases]]' >> ${{ github.workspace }}/server/wrangler.toml
echo 'binding = "DB"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'database_name = "preview"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'database_id = "${{ inputs.PREVIEW_DB_ID }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo '[env.preview.ai]' >> ${{ github.workspace }}/server/wrangler.toml
echo 'database_name = "${{ inputs.environment }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'database_id = "${{ inputs.DB_ID }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo '[env.${{ inputs.environment }}.ai]' >> ${{ github.workspace }}/server/wrangler.toml
echo 'binding = "AI"' >> ${{ github.workspace }}/server/wrangler.toml
echo '[[env.preview.vectorize]]' >> ${{ github.workspace }}/server/wrangler.toml
echo '[[env.${{ inputs.environment }}.vectorize]]' >> ${{ github.workspace }}/server/wrangler.toml
echo 'binding = "VECTOR_INDEX"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'index_name = "vector-index-preview"' >> ${{ github.workspace }}/server/wrangler.toml
echo '[env.preview.vars]' >> ${{ github.workspace }}/server/wrangler.toml
echo 'NODE_ENV = "preview"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'GOOGLE_CLIENT_ID = "${{ inputs.GOOGLE_CLIENT_ID }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'GOOGLE_CLIENT_SECRET = "${{ inputs.GOOGLE_CLIENT_SECRET }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'STMP_EMAIL = "${{ inputs.STMP_EMAIL }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'STMP_PASSWORD = "${{ inputs.STMP_PASSWORD }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'JWT_SECRET = "${{ inputs.JWT_SECRET }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'SEND_GRID_API_KEY = "${{ inputs.SEND_GRID_API_KEY }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'MAPBOX_ACCESS_TOKEN = "${{ inputs.MAPBOX_ACCESS_TOKEN }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'OSM_URI = "https://overpass-api.de/api/interpreter"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'WEATHER_URL = "https://api.openweathermap.org/data/2.5/forecast"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'OPENWEATHER_KEY = "${{ inputs.OPENWEATHER_KEY }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'VECTORIZE_API_KEY = "${{ inputs.VECTORIZE_API_KEY }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'index_name = "${{ inputs.VECTOR_INDEX_NAME }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo '[env.${{ inputs.environment }}.vars]' >> ${{ github.workspace }}/server/wrangler.toml
echo 'APP_URL = "${{ inputs.APP_URL }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'AWS_SIGN_ALGORITHM = "${{ inputs.AWS_SIGN_ALGORITHM }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'BUCKET_ENDPOINT = "${{ inputs.BUCKET_ENDPOINT }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'BUCKET_NAME = "${{ inputs.BUCKET_NAME }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'BUCKET_REGION = "${{ inputs.BUCKET_REGION }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'BUCKET_SERVICE = "${{ inputs.BUCKET_SERVICE }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'BUCKET_SESSION_TOKEN = "${{ inputs.BUCKET_SESSION_TOKEN }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'CLOUDFLARE_ACCOUNT_ID = "${{ inputs.CLOUDFLARE_ACCOUNT_ID }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'OPENAI_API_KEY = "${{ inputs.OPENAI_API_KEY }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo '[env.production]' >> ${{ github.workspace }}/server/wrangler.toml
echo '[[env.production.d1_databases]]' >> ${{ github.workspace }}/server/wrangler.toml
echo 'binding = "DB"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'database_name = "production"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'database_id = "${{ inputs.PRODUCTION_DB_ID }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo '[env.production.ai]' >> ${{ github.workspace }}/server/wrangler.toml
echo 'binding = "AI"' >> ${{ github.workspace }}/server/wrangler.toml
echo '[[env.production.vectorize]]' >> ${{ github.workspace }}/server/wrangler.toml
echo 'binding = "VECTOR_INDEX"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'index_name = "vector-index"' >> ${{ github.workspace }}/server/wrangler.toml
echo '[env.production.vars]' >> ${{ github.workspace }}/server/wrangler.toml
echo 'NODE_ENV = "production"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'GOOGLE_CLIENT_ID = "${{ inputs.GOOGLE_CLIENT_ID }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'GOOGLE_CLIENT_SECRET = "${{ inputs.GOOGLE_CLIENT_SECRET }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'STMP_EMAIL = "${{ inputs.STMP_EMAIL }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'STMP_PASSWORD = "${{ inputs.STMP_PASSWORD }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'JWT_SECRET = "${{ inputs.JWT_SECRET }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'SEND_GRID_API_KEY = "${{ inputs.SEND_GRID_API_KEY }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'MAPBOX_ACCESS_TOKEN = "${{ inputs.MAPBOX_ACCESS_TOKEN }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'NODE_ENV = "${{ inputs.environment }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'OSM_URI = "https://overpass-api.de/api/interpreter"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'STMP_EMAIL = "${{ inputs.STMP_EMAIL }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'WEATHER_URL = "https://api.openweathermap.org/data/2.5/forecast"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'OPENWEATHER_KEY = "${{ inputs.OPENWEATHER_KEY }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'VECTORIZE_API_KEY = "${{ inputs.VECTORIZE_API_KEY }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'CLOUDFLARE_ACCOUNT_ID = "${{ inputs.CLOUDFLARE_ACCOUNT_ID }}"' >> ${{ github.workspace }}/server/wrangler.toml
echo 'OPENAI_API_KEY = "${{ inputs.OPENAI_API_KEY }}"' >> ${{ github.workspace }}/server/wrangler.toml
191 changes: 108 additions & 83 deletions .github/workflows/backend-preview.yml
Original file line number Diff line number Diff line change
@@ -1,91 +1,116 @@
# name: 'Backend Preview Deployment'
name: 'Backend Preview Deployment'

# on:
# workflow_dispatch:
# pull_request:
# branches:
# - feat/preview-version-of-api
# types: [closed]
# push:
# branches:
# - feat/preview-version-of-api
# paths:
# - 'packages/validations/**'
# - 'packages/shared-types/**'
# - 'server/**'
# concurrency:
# group: ${{ github.workflow }}-${{ github.ref }}
# cancel-in-progress: true
on:
workflow_dispatch:
push:
branches:
- development
paths:
- '.github/workflows/backend-preview.yml'
- 'packages/validations/**'
- 'packages/shared-types/**'
- 'server/**'
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

# jobs:
# deploy:
# name: Deploy to Cloudflare Workers
# runs-on: ubuntu-latest
# timeout-minutes: 10
jobs:
deploy:
name: Deploy to Cloudflare Workers
runs-on: ubuntu-latest
timeout-minutes: 10

# steps:
# - name: Checkout code
# uses: actions/checkout@v3
# with:
# fetch-depth: 1
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 1

# - name: Setup JS Runtime environment
# uses: ./.github/actions/setup-js-runtime
- name: Setup JS Runtime environment
uses: ./.github/actions/setup-js-runtime

# - name: Install dependencies
# uses: ./.github/actions/install-deps
- name: Generate wrangler.toml
uses: ./.github/actions/setup-wrangler-toml
with:
environment: preview
APP_URL: ${{ secrets.VITE_PUBLIC_APP_URL }}
AWS_SIGN_ALGORITHM: ${{ secrets.PREVIEW_AWS_SIGN_ALGORITHM }}
BUCKET_ENDPOINT: ${{ secrets.PREVIEW_BUCKET_ENDPOINT }}
BUCKET_NAME: ${{ secrets.PREVIEW_BUCKET_NAME }}
BUCKET_REGION: ${{ secrets.PREVIEW_BUCKET_REGION }}
BUCKET_SERVICE: ${{ secrets.PREVIEW_BUCKET_SERVICE }}
BUCKET_SESSION_TOKEN: ${{ secrets.PREVIEW_BUCKET_SESSION_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
DB_ID: ${{ secrets.PREVIEW_DB_ID }}
GOOGLE_CLIENT_ID: ${{ secrets.PREVIEW_GOOGLE_CLIENT_ID }}
STMP_EMAIL: ${{ secrets.PREVIEW_STMP_EMAIL }}
VECTOR_INDEX_NAME: ${{ secrets.PREVIEW_VECTOR_INDEX || 'vector-index-preview' }}

# - name: Generate wrangler.toml
# uses: ./.github/actions/setup-wrangler-toml
# with:
# GOOGLE_CLIENT_ID: ${{ secrets.GOOGLE_CLIENT_ID }}
# GOOGLE_CLIENT_SECRET: ${{ secrets.GOOGLE_CLIENT_SECRET }}
# STMP_EMAIL: ${{ secrets.STMP_EMAIL }}
# STMP_PASSWORD: ${{ secrets.STMP_PASSWORD }}
# JWT_SECRET: ${{ secrets.JWT_SECRET }}
# SEND_GRID_API_KEY: ${{ secrets.SEND_GRID_API_KEY }}
# MAPBOX_ACCESS_TOKEN: ${{ secrets.MAPBOX_ACCESS_TOKEN }}
# OPENWEATHER_KEY: ${{ secrets.OPENWEATHER_KEY }}
# VECTORIZE_API_KEY: ${{ secrets.VECTORIZE_API_KEY }}
# CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
# OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
# PREVIEW_DB_ID: ${{ secrets.PREVIEW_DB_ID }}
# PRODUCTION_DB_ID: ${{ secrets.PRODUCTION_DB_ID }}
# PREVIEW_GOOGLE_CLIENT_ID: ${{ secrets.PREVIEW_GOOGLE_CLIENT_ID }}
# PREVIEW_GOOGLE_CLIENT_SECRET: ${{ secrets.PREVIEW_GOOGLE_CLIENT_SECRET }}
# PREVIEW_STMP_EMAIL: ${{ secrets.PREVIEW_STMP_EMAIL }}
# PREVIEW_STMP_PASSWORD: ${{ secrets.PREVIEW_STMP_PASSWORD }}
# PREVIEW_JWT_SECRET: ${{ secrets.PREVIEW_JWT_SECRET }}
# PREVIEW_SEND_GRID_API_KEY: ${{ secrets.PREVIEW_SEND_GRID_API_KEY }}
# PREVIEW_MAPBOX_ACCESS_TOKEN: ${{ secrets.PREVIEW_MAPBOX_ACCESS_TOKEN }}
# PREVIEW_OPENWEATHER_KEY: ${{ secrets.PREVIEW_OPENWEATHER_KEY }}
# PREVIEW_VECTORIZE_API_KEY: ${{ secrets.PREVIEW_VECTORIZE_API_KEY }}
# PREVIEW_CLOUDFLARE_ACCOUNT_ID: ${{ secrets.PREVIEW_CLOUDFLARE_ACCOUNT_ID }}
# PREVIEW_OPENAI_API_KEY: ${{ secrets.PREVIEW_OPENAI_API_KEY }}
- name: Migrate database
uses: cloudflare/[email protected]
with:
wranglerVersion: '3.75.0'
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID}}
workingDirectory: server
preCommands: |
echo '---Clear previous wrangler logs---'
rm /home/runner/.config/.wrangler/logs/* || true
command: d1 migrations apply preview --remote --env preview
packageManager: yarn
env:
NO_D1_WARNING: true
CI: true
WRANGLER_LOG: debug
WRANGLER_LOG_SANITIZE: false

# - name: Migrate database
# # run: cd packages/api && bun run migrate
# run: cd server && yarn run migrate:preview
# env:
# NO_D1_WARNING: true
# CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
- name: Print wrangler extra logs on failure
if: failure()
run: |
cat "$(ls -t /home/runner/.config/.wrangler/logs/* | head -n 1)" || true;
- name: Deploy
uses: cloudflare/[email protected]
with:
wranglerVersion: '3.75.0'
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID}}
workingDirectory: server
# INFO: We need to upload secret manually because the default secrets input of this action `cloudflare/[email protected]` use the `wrangler secret put` command to upload secrets but this command will do not work non-interactive context.
preCommands: >
echo '---Clear previous wrangler logs file---' &&
rm /home/runner/.config/.wrangler/logs/* || true &&
echo '---Generate and upload secrets---' &&
echo '>> Generate secrets file' &&
echo '{}' | jq '
.WORKERS_AI_API_KEY="${{ secrets.PREVIEW_WORKERS_AI_API_KEY }}" |
.VECTORIZE_API_KEY="${{ secrets.PREVIEW_VECTORIZE_API_KEY }}" |
.BUCKET_ACCESS_KEY_ID="${{ secrets.PREVIEW_BUCKET_ACCESS_KEY_ID }}" |
.GOOGLE_CLIENT_SECRET="${{ secrets.PREVIEW_GOOGLE_CLIENT_SECRET }}" |
.JWT_SECRET="${{ secrets.PREVIEW_JWT_SECRET }}" |
.MAPBOX_ACCESS_TOKEN="${{ secrets.PREVIEW_MAPBOX_ACCESS_TOKEN }}" |
.OPENAI_API_KEY="${{ secrets.PREVIEW_OPENAI_API_KEY }}" |
.OPENWEATHER_KEY="${{ secrets.PREVIEW_OPENWEATHER_KEY }}" |
.SEND_GRID_API_KEY="${{ secrets.PREVIEW_SEND_GRID_API_KEY }}" |
.STMP_PASSWORD="${{ secrets.PREVIEW_STMP_PASSWORD }}" |
.VECTORIZE_API_KEY="${{ secrets.PREVIEW_VECTORIZE_API_KEY }}" |
.X_AMZ_SECURITY_TOKEN="${{ secrets.PREVIEW_X_AMZ_SECURITY_TOKEN }}" |
.JWT_VERIFICATION_KEY="${{ secrets.PREVIEW_JWT_SECRET }}"
' > secrets.json &&
echo '<< Secrets file generated' &&
echo '>> Upload secrets' &&
yarn wrangler secret bulk --env preview secrets.json &&
echo '<< Secrets uploaded'
command: deploy src/index.ts --env preview
packageManager: yarn
env:
CI: true
NO_D1_WARNING: true
WRANGLER_LOG: debug
WRANGLER_LOG_SANITIZE: false

- name: Print wrangler extra logs on failure
if: failure()
run: |
cat "$(ls -t /home/runner/.config/.wrangler/logs/* | head -n 1)" || true;
# - name: Deploy
# uses: cloudflare/[email protected]
# with:
# wranglerVersion: '3.15.0'
# apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
# # workingDirectory: packages/api
# workingDirectory: server
# command: yarn run deploy:preview
# # packageManager: bun
# packageManager: yarn
# secrets: |
# APP_URL
# JWT_VERIFICATION_KEY
# CI
# env:
# APP_URL: ${{ secrets.VITE_PUBLIC_APP_URL }}
# JWT_VERIFICATION_KEY: ${{ secrets.JWT_SECRET }}
# CI: true
# NO_D1_WARNING: true
Loading

0 comments on commit 28ced46

Please sign in to comment.