Deploy API and Web #4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy API and Web | |
on: | |
workflow_dispatch: | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
name: Deploy | |
steps: | |
# Step 1: Checkout the code | |
- uses: actions/checkout@v4 | |
# Step 2: Setup pnpm | |
- uses: pnpm/action-setup@v4 | |
# Step 3: Setup Node.js with pnpm cache | |
- uses: actions/setup-node@v4 | |
with: | |
node-version: '20' | |
cache: pnpm | |
# Step 4: Install dependencies | |
- run: pnpm install | |
# Step 5: Replace placeholders in wrangler.toml with GitHub Secrets | |
- name: Replace wrangler.toml values | |
run: | | |
sed -i "s|{{KV_ID}}|${{ secrets.CLOUDFLARE_KV_ID }}|g" apps/api/wrangler.toml | |
if [ -n "${{ secrets.API_CUSTOM_DOMAIN }}" ]; then | |
sed -i "s|{{CUSTOM_DOMAIN}}|${{ secrets.API_CUSTOM_DOMAIN }}|g" apps/api/wrangler.toml | |
sed -i "s|#route|route|g" apps/api/wrangler.toml | |
else | |
echo "Custom domain not set, skipping..." | |
fi | |
# Step 6: Set up Cloudflare Wrangler | |
- name: Set up Wrangler for API | |
id: wrangler-api | |
uses: cloudflare/wrangler-action@v3 | |
with: | |
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
workingDirectory: 'apps/api' | |
preCommands: | | |
pnpm db:migrate:apply | |
pnpm db:seed | |
echo "$NEON_DATABASE_URL" | pnpm wrangler secret put NEON_DATABASE_URL | |
echo "$JWT_SECRET" | pnpm wrangler secret put JWT_SECRET | |
command: deploy --minify | |
env: | |
NEON_DATABASE_URL: ${{ secrets.NEON_DATABASE_URL }} | |
JWT_SECRET: ${{ secrets.JWT_SECRET }} | |
DEFAULT_USER: ${{ secrets.DEFAULT_USER }} | |
DEFAULT_PASSWORD: ${{ secrets.DEFAULT_PASSWORD }} | |
- name: Set NEXT_PUBLIC_WORKER | |
run: | | |
if [ -n "${{ secrets.API_CUSTOM_DOMAIN }}" ]; then | |
echo "Custom domain set, using it..." | |
echo "NEXT_PUBLIC_WORKER=${{ secrets.API_CUSTOM_DOMAIN }}" >> $GITHUB_ENV | |
echo "API URL: $NEXT_PUBLIC_WORKER" | |
else | |
echo "Custom domain not set, using the API Worker URL..." | |
API_WORKER_URL=${{ steps.wrangler-api.outputs.deployment-url }} | |
echo "NEXT_PUBLIC_WORKER=$API_WORKER_URL" >> $GITHUB_ENV | |
echo "API URL: $API_WORKER_URL" | |
fi | |
# Step 7: Deploy Web App to Cloudflare Pages | |
- name: Deploy Web to Cloudflare Pages | |
env: | |
NEXT_PUBLIC_WORKER: ${{ env.NEXT_PUBLIC_WORKER }} | |
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
run: pnpm pages:deploy:web |