-
Notifications
You must be signed in to change notification settings - Fork 0
358 lines (333 loc) · 13 KB
/
deploy.yml
File metadata and controls
358 lines (333 loc) · 13 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# CI/CD Deployment Workflow for Digitization Academy
#
# WORKFLOW:
# - Push to 'development' branch → Deploys to DEVELOPMENT automatically
# - Push to 'main' branch → Creates GitHub release with semantic version → Deploys to PRODUCTION
#
# VERSION CONTROL:
# Use commit message tags to control version bumps:
# - [major] or [breaking] → Major version bump (1.0.0 → 2.0.0)
# - [minor] or [feature] → Minor version bump (1.0.0 → 1.1.0)
# - Default (no tag) → Patch version bump (1.0.0 → 1.0.1)
#
# SKIP DEPLOYMENT:
# Add [skip deploy] or [no deploy] to your commit message to push without deploying
#
name: Build and Deploy
on:
push:
branches:
- main # Creates release and deploys to production
- development # Development environment
release:
types: [published] # Production deployment trigger
permissions:
id-token: write
contents: read
jobs:
# Create release when pushing to main
create-release:
runs-on: ubuntu-latest
permissions:
contents: write
if: "github.ref == 'refs/heads/main' && !contains(github.event.head_commit.message, '[skip deploy]') && !contains(github.event.head_commit.message, '[no deploy]')"
outputs:
release-tag: ${{ steps.create_release.outputs.tag_name }}
steps:
- uses: actions/checkout@v5
with:
fetch-depth: 0
- name: Determine version bump
id: version_bump
env:
CURRENT_COMMIT_MSG: ${{ github.event.head_commit.message }}
run: |
# Get the latest tag, check if any tags exist
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ -z "$LATEST_TAG" ]; then
echo "No tags found - this is the first release"
LATEST_TAG="0.0.0"
# For first release, show all commits
COMMITS=$(git log --oneline)
echo "All commits (first release):"
else
echo "Latest tag: $LATEST_TAG"
# Show commits since last tag
COMMITS=$(git log ${LATEST_TAG}..HEAD --oneline)
echo "Commits since last tag:"
fi
echo "$COMMITS"
# Get the specific commit message for this push (passed via env to avoid backtick execution)
echo "Current commit message: $(printf '%s' "$CURRENT_COMMIT_MSG")"
# Determine version bump based on the current commit message only
if echo "$CURRENT_COMMIT_MSG" | grep -i "\[major\]\|\[breaking\]"; then
BUMP_TYPE="major"
elif echo "$CURRENT_COMMIT_MSG" | grep -i "\[minor\]\|\[feature\]"; then
BUMP_TYPE="minor"
else
BUMP_TYPE="patch"
fi
echo "Bump type: $BUMP_TYPE"
echo "bump_type=$BUMP_TYPE" >> $GITHUB_OUTPUT
echo "current_version=$LATEST_TAG" >> $GITHUB_OUTPUT
- name: Calculate new version
id: new_version
run: |
CURRENT="${{ steps.version_bump.outputs.current_version }}"
BUMP_TYPE="${{ steps.version_bump.outputs.bump_type }}"
# Check if this is the first release (no existing tags)
if [ "$CURRENT" = "0.0.0" ]; then
echo "First release detected - setting version to 1.0.0"
NEW_VERSION="1.0.0"
else
# Parse current version for subsequent releases
if [[ $CURRENT =~ ^([0-9]+)\.([0-9]+)\.([0-9]+) ]]; then
MAJOR=${BASH_REMATCH[1]}
MINOR=${BASH_REMATCH[2]}
PATCH=${BASH_REMATCH[3]}
else
MAJOR=0
MINOR=0
PATCH=0
fi
# Increment based on bump type
case $BUMP_TYPE in
"major")
MAJOR=$((MAJOR + 1))
MINOR=0
PATCH=0
;;
"minor")
MINOR=$((MINOR + 1))
PATCH=0
;;
"patch")
PATCH=$((PATCH + 1))
;;
esac
NEW_VERSION="$MAJOR.$MINOR.$PATCH"
fi
echo "New version: $NEW_VERSION"
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
- name: Create Release
id: create_release
run: |
# Create release notes based on whether this is first release or not
if [ "${{ steps.version_bump.outputs.current_version }}" = "0.0.0" ]; then
RELEASE_NOTES="## What's Changed
🎉 **First Release!**
Auto-generated initial release from main branch.
**Latest commit:**
\`\`\`
$COMMIT_MESSAGE
\`\`\`"
else
RELEASE_NOTES="## What's Changed
Auto-generated release from main branch.
**Commits included:**
\`\`\`
$COMMIT_MESSAGE
\`\`\`
**Full Changelog**: https://github.com/${{ github.repository }}/compare/${{ steps.version_bump.outputs.current_version }}...${{ steps.new_version.outputs.new_version }}"
fi
gh release create "${{ steps.new_version.outputs.new_version }}" \
--title "Release ${{ steps.new_version.outputs.new_version }}" \
--notes "$RELEASE_NOTES"
echo "tag_name=${{ steps.new_version.outputs.new_version }}" >> $GITHUB_OUTPUT
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
# ... existing code ...
build-and-deploy-production:
needs: create-release
runs-on: ubuntu-latest
environment: production
if: "github.ref == 'refs/heads/main' && !contains(github.event.head_commit.message, '[skip deploy]') && !contains(github.event.head_commit.message, '[no deploy]')"
steps:
- uses: actions/checkout@v5
- name: Get Runner IP
id: ip
uses: haythem/public-ip@v1.3
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::147899039648:role/GitHubActionsDeployRole
aws-region: ${{ secrets.AWS_REGION }}
- name: Whitelist Runner IP in AWS Security Group
run: |
aws ec2 authorize-security-group-ingress \
--region ${{ secrets.AWS_REGION }} \
--group-id ${{ secrets.AWS_SECURITY_GROUP_ID }} \
--protocol tcp \
--port 22 \
--cidr ${{ steps.ip.outputs.ipv4 }}/32
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
extensions: mbstring, xml, bcmath, ctype, json, tokenizer, pdo, pdo_mysql
- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-v3-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-v3-
- name: Create storage directories
run: |
mkdir -p storage/framework/cache/data
mkdir -p storage/framework/sessions
mkdir -p storage/framework/views
mkdir -p bootstrap/cache
- name: Install Composer dependencies (without scripts)
run: composer install --prefer-dist --no-progress --no-dev --optimize-autoloader --no-scripts
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'yarn'
- name: Install Yarn dependencies
run: yarn install --frozen-lockfile --ignore-engines
- name: Build assets for Production
run: npm run production
- name: Create deployment package
run: |
# Clean any prior
rm -rf deployment-package || true
# Create isolated temp dir
TEMP_DIR=$(mktemp -d)
# Sync repo files to temp
rsync -av \
--exclude=node_modules \
--exclude=.git \
--exclude=.github \
--exclude=tests \
--exclude=storage/logs \
--exclude=vendor \
. "$TEMP_DIR/"
# Copy assets into temp/public/
cp -r public/css public/js public/fonts public/images public/svg public/mix-manifest.json "$TEMP_DIR/public/" || true
# Move to package name
mv "$TEMP_DIR" deployment-package
- name: Upload deployment artifact
uses: actions/upload-artifact@v4
with:
name: digitizationacademy-${{ github.sha }}
path: deployment-package/
retention-days: 30
- name: Deploy with Deployer
uses: deployphp/action@v1
with:
private-key: ${{ secrets.DEPLOY_PRIVATE_KEY }}
dep: deploy production
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_SHA: ${{ github.sha }}
GITHUB_REPO: ${{ github.repository }}
OPCACHE_WEBHOOK_TOKEN: ${{ secrets.OPCACHE_WEBHOOK_TOKEN }}
- name: Revoke Runner IP from AWS Security Group
if: always()
run: |
aws ec2 revoke-security-group-ingress \
--region ${{ secrets.AWS_REGION }} \
--group-id ${{ secrets.AWS_SECURITY_GROUP_ID }} \
--protocol tcp \
--port 22 \
--cidr ${{ steps.ip.outputs.ipv4 }}/32
build-and-deploy-development:
runs-on: ubuntu-latest
environment: development
if: "github.ref == 'refs/heads/development' && !contains(github.event.head_commit.message, '[skip deploy]') && !contains(github.event.head_commit.message, '[no deploy]')"
steps:
- uses: actions/checkout@v5
- name: Get Runner IP
id: ip
uses: haythem/public-ip@v1.3
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::147899039648:role/GitHubActionsDeployRole
aws-region: ${{ secrets.AWS_REGION }}
- name: Whitelist Runner IP in AWS Security Group
run: |
aws ec2 authorize-security-group-ingress \
--region ${{ secrets.AWS_REGION }} \
--group-id ${{ secrets.AWS_SECURITY_GROUP_ID }} \
--protocol tcp \
--port 22 \
--cidr ${{ steps.ip.outputs.ipv4 }}/32
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
extensions: mbstring, xml, bcmath, ctype, json, tokenizer, pdo, pdo_mysql
- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-v3-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-v3-
- name: Create storage directories
run: |
mkdir -p storage/framework/cache/data
mkdir -p storage/framework/sessions
mkdir -p storage/framework/views
mkdir -p bootstrap/cache
- name: Install Composer dependencies (without scripts)
run: composer install --prefer-dist --no-progress --no-dev --optimize-autoloader --no-scripts
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'yarn'
- name: Install Yarn dependencies
run: yarn install --frozen-lockfile --ignore-engines
- name: Build assets for Development
run: npm run production
- name: Create deployment package
run: |
# Clean any prior
rm -rf deployment-package || true
# Create isolated temp dir
TEMP_DIR=$(mktemp -d)
# Sync repo files to temp (exclusions prevent bloat/self-ref)
rsync -av \
--exclude=node_modules \
--exclude=.git \
--exclude=.github \
--exclude=tests \
--exclude=storage/logs \
--exclude=vendor \
. "$TEMP_DIR/"
# Copy assets into temp/public/ (these are already built via npm run production)
cp -r public/css public/js public/fonts public/images public/svg public/mix-manifest.json "$TEMP_DIR/public/" || true
# Rename to package name
mv "$TEMP_DIR" deployment-package
- name: Upload deployment artifact
uses: actions/upload-artifact@v4
with:
name: digitizationacademy-${{ github.sha }}
path: deployment-package/
retention-days: 30
- name: Deploy with Deployer
uses: deployphp/action@v1
with:
private-key: ${{ secrets.DEPLOY_PRIVATE_KEY }}
dep: deploy development
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_SHA: ${{ github.sha }}
GITHUB_REPO: ${{ github.repository }}
OPCACHE_WEBHOOK_TOKEN: ${{ secrets.OPCACHE_WEBHOOK_TOKEN }}
- name: Revoke Runner IP from AWS Security Group
if: always()
run: |
aws ec2 revoke-security-group-ingress \
--region ${{ secrets.AWS_REGION }} \
--group-id ${{ secrets.AWS_SECURITY_GROUP_ID }} \
--protocol tcp \
--port 22 \
--cidr ${{ steps.ip.outputs.ipv4 }}/32