Skip to content

Commit 5edc88a

Browse files
committed
Add: Implemented better GitHub CI actions workflow and preview URL handling.
1 parent 695306b commit 5edc88a

File tree

3 files changed

+171
-2
lines changed

3 files changed

+171
-2
lines changed

.github/workflows/feature-api.yaml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
name: Feature API Deploy
2+
permissions:
3+
contents: read
4+
pull-requests: write
5+
on:
6+
push:
7+
branches:
8+
- 'feature/**'
9+
- 'feat/**'
10+
paths:
11+
- apps/api/**
12+
- packages/**
13+
- "!packages/email/**"
14+
- "!packages/ui/**"
15+
jobs:
16+
deploy:
17+
name: 🚀 Deploy Feature API to Fly
18+
runs-on: ubuntu-latest
19+
steps:
20+
- uses: actions/checkout@v4
21+
- uses: oven-sh/setup-bun@v1
22+
with:
23+
bun-version: latest
24+
- name: Install dependencies
25+
run: bun install
26+
- name: 🔦 Run linter
27+
run: bun run lint
28+
working-directory: ./apps/api
29+
- name: 🪐 Check TypeScript
30+
run: bun run typecheck
31+
working-directory: ./apps/api
32+
33+
# Create a branch-specific Fly app name
34+
- name: Set Fly app name
35+
id: fly-app
36+
run: |
37+
BRANCH_NAME=$(echo "${{ github.ref_name }}" | sed 's/[^a-zA-Z0-9-]/-/g' | cut -c1-30)
38+
APP_NAME="avelero-api-${BRANCH_NAME}"
39+
echo "app_name=${APP_NAME}" >> $GITHUB_OUTPUT
40+
echo "Deploying to: ${APP_NAME}"
41+
42+
- uses: superfly/flyctl-actions/setup-flyctl@master
43+
44+
# Deploy to branch-specific Fly app
45+
- name: Deploy to Fly
46+
run: |
47+
flyctl deploy --remote-only \
48+
--dockerfile apps/api/Dockerfile \
49+
--config apps/api/fly-preview.yml \
50+
--app ${{ steps.fly-app.outputs.app_name }} \
51+
--auto-confirm || flyctl apps create ${{ steps.fly-app.outputs.app_name }} && \
52+
flyctl deploy --remote-only \
53+
--dockerfile apps/api/Dockerfile \
54+
--config apps/api/fly-preview.yml \
55+
--app ${{ steps.fly-app.outputs.app_name }}
56+
env:
57+
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN_STAGING }}
58+
59+
- name: Get deployment URL
60+
id: deploy-url
61+
run: |
62+
URL="https://${{ steps.fly-app.outputs.app_name }}.fly.dev"
63+
echo "url=${URL}" >> $GITHUB_OUTPUT
64+
65+
- name: Comment on PR
66+
if: github.event_name == 'pull_request'
67+
uses: actions/github-script@v6
68+
with:
69+
script: |
70+
github.rest.issues.createComment({
71+
issue_number: context.issue.number,
72+
owner: context.repo.owner,
73+
repo: context.repo.repo,
74+
body: '🚀 API Preview deployed to: ${{ steps.deploy-url.outputs.url }}'
75+
})

.github/workflows/feature-app.yaml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
name: Feature Deployment - App
2+
env:
3+
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
4+
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID_APP }}
5+
TURBO_TOKEN: ${{ secrets.VERCEL_TOKEN }}
6+
TURBO_TEAM: ${{ secrets.VERCEL_ORG_ID }}
7+
on:
8+
push:
9+
branches:
10+
- 'feature/**'
11+
- 'feat/**'
12+
paths:
13+
- apps/app/**
14+
- packages/**
15+
pull_request:
16+
branches:
17+
- staging
18+
- main
19+
paths:
20+
- apps/app/**
21+
- packages/**
22+
permissions:
23+
contents: read
24+
pull-requests: write
25+
jobs:
26+
deploy-feature:
27+
runs-on: ubuntu-latest
28+
steps:
29+
- uses: actions/checkout@v4
30+
- uses: oven-sh/setup-bun@v1
31+
with:
32+
bun-version: latest
33+
- name: Install dependencies
34+
run: bun install
35+
- name: 🔦 Run linter
36+
run: bun run lint
37+
working-directory: ./apps/app
38+
- name: 🪐 Check TypeScript
39+
run: NODE_OPTIONS="--max-old-space-size=8192" bun run typecheck
40+
working-directory: ./apps/app
41+
42+
- name: 📤 Pull Vercel Environment Information
43+
run: bunx vercel pull --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }}
44+
45+
- name: 🏗 Build Project Artifacts
46+
run: bunx vercel build --token=${{ secrets.VERCEL_TOKEN }}
47+
48+
- name: 📤 Pull Environment Variables to .env
49+
run: bunx vercel env pull .env --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }}
50+
51+
- name: 🔄 Deploy Background Jobs
52+
env:
53+
TRIGGER_ACCESS_TOKEN: ${{ secrets.TRIGGER_ACCESS_TOKEN }}
54+
run: |
55+
TRIGGER_PROJECT_ID=${{ secrets.TRIGGER_PROJECT_ID }} bunx trigger.dev@4.0.2 deploy
56+
working-directory: packages/jobs
57+
58+
- name: 🚀 Deploy to Vercel
59+
id: deploy
60+
run: |
61+
DEPLOYMENT_URL=$(bunx vercel deploy --prebuilt --archive=tgz --token=${{ secrets.VERCEL_TOKEN }})
62+
echo "url=${DEPLOYMENT_URL}" >> $GITHUB_OUTPUT
63+
echo "Deployed to: ${DEPLOYMENT_URL}"
64+
65+
- name: Comment on PR
66+
if: github.event_name == 'pull_request'
67+
uses: actions/github-script@v6
68+
with:
69+
script: |
70+
github.rest.issues.createComment({
71+
issue_number: context.issue.number,
72+
owner: context.repo.owner,
73+
repo: context.repo.repo,
74+
body: '🚀 App Preview deployed to: ${{ steps.deploy.outputs.url }}'
75+
})

apps/api/src/index.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,26 @@ app.use(secureHeaders());
1212
app.use(
1313
"*",
1414
cors({
15-
origin: process.env.ALLOWED_API_ORIGINS?.split(",") ?? [],
15+
origin: (origin, c) => {
16+
if (!origin) return origin; // Allow requests with no origin
17+
18+
const allowedOrigins = process.env.ALLOWED_API_ORIGINS?.split(",") ?? [];
19+
20+
// Check exact matches first (most secure and fastest)
21+
if (allowedOrigins.includes(origin)) return origin;
22+
23+
// Check wildcard patterns (only if explicitly configured)
24+
const isAllowed = allowedOrigins.some(pattern => {
25+
if (pattern.includes('*')) {
26+
// Escape dots and replace * with .*
27+
const regex = new RegExp(`^${pattern.replace(/\./g, '\\.').replace(/\*/g, '.*')}$`);
28+
return regex.test(origin);
29+
}
30+
return false;
31+
});
32+
33+
return isAllowed ? origin : undefined;
34+
},
1635
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"],
1736
allowHeaders: [
1837
"Authorization",
@@ -44,4 +63,4 @@ app.get("/health", (c) => c.json({ status: "ok" }, 200));
4463
export default {
4564
port: process.env.PORT ? Number.parseInt(process.env.PORT) : 4000,
4665
fetch: app.fetch,
47-
};
66+
};

0 commit comments

Comments
 (0)