-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
182 lines (153 loc) · 6.35 KB
/
deploy.yml
File metadata and controls
182 lines (153 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
name: Build and Deploy
on:
push:
branches: [master, dev]
# Allow concurrent deployments for different environments
concurrency:
group: 'pages-${{ github.ref }}'
cancel-in-progress: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run type check
run: npm run type-check
- name: Run linting
run: npm run lint
- name: Run tests
run: npm run test
- name: Build application
run: |
echo "Building for branch: ${{ github.ref_name }}"
echo "NODE_ENV: $NODE_ENV"
echo "SURGE_PREVIEW: $SURGE_PREVIEW"
npm run build
env:
NEXT_PUBLIC_GA_ID: ${{ secrets.NEXT_PUBLIC_GA_ID }}
NEXT_PUBLIC_REQUIRE_CONSENT: true
NEXT_PUBLIC_ANONYMIZE_IP: true
# Set SURGE_PREVIEW for non-master branches to disable basePath
SURGE_PREVIEW: ${{ github.ref != 'refs/heads/master' && 'true' || '' }}
- name: Upload Pages Artifact (Production)
if: github.ref == 'refs/heads/master'
uses: actions/upload-pages-artifact@v3
with:
path: ./out
- name: Upload Preview Artifact
if: github.ref != 'refs/heads/master'
uses: actions/upload-artifact@v4
with:
name: preview-build-${{ github.run_number }}-${{ github.run_attempt }}
path: ./out
retention-days: 30
# Production deployment to main GitHub Pages
deploy-production:
needs: build
runs-on: ubuntu-latest
# Only deploy to production from master branch
if: github.ref == 'refs/heads/master'
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
# Preview deployment for development branches
deploy-preview:
needs: build
runs-on: ubuntu-latest
# Deploy preview for dev branches (not master, only on direct push)
if: github.ref != 'refs/heads/master' && github.event_name == 'push'
# Add deployment environment to show URL in GitHub UI
environment:
name: preview-${{ github.ref_name }}
steps:
- name: Calculate sanitized branch name
id: branch
run: |
SANITIZED_BRANCH=$(echo "${{ github.ref_name }}" | sed 's/[^a-zA-Z0-9-]/-/g' | tr '[:upper:]' '[:lower:]')
echo "sanitized=$SANITIZED_BRANCH" >> $GITHUB_OUTPUT
echo "url=https://gprg-${SANITIZED_BRANCH}.surge.sh" >> $GITHUB_OUTPUT
- name: Debug deployment conditions
run: |
echo "GitHub ref: ${{ github.ref }}"
echo "GitHub ref name: ${{ github.ref_name }}"
echo "Event name: ${{ github.event_name }}"
echo "Is master?: ${{ github.ref == 'refs/heads/master' }}"
echo "Is push?: ${{ github.event_name == 'push' }}"
echo "Should deploy?: ${{ github.ref != 'refs/heads/master' && github.event_name == 'push' }}"
- name: Download Preview Artifact
uses: actions/download-artifact@v4
with:
name: preview-build-${{ github.run_number }}-${{ github.run_attempt }}
path: ./preview-out
- name: Deploy to Surge.sh (Preview)
id: deploy
run: |
npm install -g surge
echo "Deploying preview for branch: ${{ github.ref_name }}"
echo "Sanitized branch name: ${{ steps.branch.outputs.sanitized }}"
echo "Preview URL: ${{ steps.branch.outputs.url }}"
surge ./preview-out ${{ steps.branch.outputs.url }} --token ${{ secrets.SURGE_TOKEN }}
echo "deployment_url=${{ steps.branch.outputs.url }}" >> $GITHUB_OUTPUT
env:
SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }}
- name: Comment PR with Preview URL
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const previewUrl = '${{ steps.deploy.outputs.deployment_url }}';
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `🚀 **Preview Deployment Ready!**\n\n📱 Preview URL: ${previewUrl}\n\n*This preview will be available for 30 days.*`
});
- name: Create deployment status
uses: actions/github-script@v7
with:
script: |
const previewUrl = '${{ steps.deploy.outputs.deployment_url }}';
// Create a commit status with the deployment URL
github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: context.sha,
state: 'success',
target_url: previewUrl,
description: `Preview deployed to ${previewUrl}`,
context: 'deployment/preview'
});
- name: Update Status Check
run: |
echo "🚀 Preview deployed successfully!"
echo "📱 Preview URL: ${{ steps.deploy.outputs.deployment_url }}"
echo ""
echo "You can find this URL in:"
echo "1. GitHub Actions > Environments tab"
echo "2. Commit status checks"
echo "3. This workflow run summary"
# Add to job summary for easy access
echo "## 🚀 Preview Deployment Complete!" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Branch:** \`${{ github.ref_name }}\`" >> $GITHUB_STEP_SUMMARY
echo "**Preview URL:** [${{ steps.deploy.outputs.deployment_url }}](${{ steps.deploy.outputs.deployment_url }})" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Where to find this URL:" >> $GITHUB_STEP_SUMMARY
echo "- **Environments tab:** Go to your repository → Environments → preview-${{ github.ref_name }}" >> $GITHUB_STEP_SUMMARY
echo "- **Commit status:** Check the commit status checks for 'deployment/preview'" >> $GITHUB_STEP_SUMMARY
echo "- **This summary:** Bookmark this workflow run for easy access" >> $GITHUB_STEP_SUMMARY