Repository navigation
170 lines (149 loc) · 6.08 KB
/
Copy pathcd.yml
File metadata and controls
170 lines (149 loc) · 6.08 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
name: Production CD Pipeline
on:
push:
branches:
- release/production
workflow_dispatch:
inputs:
bump_type:
description: 'SemVer bump type (used if manual run)'
required: true
default: 'patch'
type: choice
options:
- patch
- minor
- major
permissions:
id-token: write
contents: write
actions: read
pull-requests: read
jobs:
deploy:
name: Build & Production Deploy
runs-on: ubuntu-latest
environment: production
steps:
- name: Checkout Source Code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Authenticate to AWS
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_CI_ROLE_ARN }}
aws-region: ap-southeast-1
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Set up Node.js Environment
uses: actions/setup-node@v4
with:
node-version: '24'
cache: 'npm'
- name: Get SSM Parameters for Deployment
id: ssm-param
uses: Armadillidiid/ssm-get-parameters-action@v1
with:
by-path: true
secret: /durianpy/cms/prod/
with-decryption: true
mask-values: true
recursive: true
- name: Determine and Apply Version Bump
id: version-bump
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BUMP_TYPE_INPUT: ${{ github.event.inputs.bump_type }}
run: ./scripts/bump-version.sh
- name: Install Dependencies
run: npm ci --legacy-peer-deps
- name: Execute Integration Tests
env:
PAYLOAD_SECRET: ${{ steps.ssm-param.outputs.PAYLOAD_SECRET }}
DATABASE_URL: ${{ steps.ssm-param.outputs.DATABASE_URL }}
run: npm run test:int
- name: Fetch Lambda Layer URL
id: layer-url
run: |
LAYER_URL=$(aws lambda get-layer-version-by-arn --arn arn:aws:lambda:ap-southeast-1:044395824272:layer:AWS-Parameters-and-Secrets-Lambda-Extension:61 --query 'Content.Location' --output text)
echo "url=$LAYER_URL" >> $GITHUB_OUTPUT
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build Image
uses: docker/build-push-action@v6
with:
context: .
file: docker/lambda/Dockerfile
load: true
tags: |
${{ steps.login-ecr.outputs.registry }}/durianpy-cms-prod:${{ steps.version-bump.outputs.new_tag }}
secrets: |
payload_secret=${{ steps.ssm-param.outputs.PAYLOAD_SECRET }}
database_url=${{ steps.ssm-param.outputs.DATABASE_URL }}
cloudfront_domain=${{ steps.ssm-param.outputs.CLOUDFRONT_DISTRIBUTION_DOMAIN }}
s3_bucket=${{ steps.ssm-param.outputs.S3_BUCKET }}
next_public_server_url=${{ steps.ssm-param.outputs.NEXT_PUBLIC_SERVER_URL }}
build-args: |
LAYER_URL=${{ steps.layer-url.outputs.url }}
AWS_DEFAULT_REGION=ap-southeast-1
ENVIRONMENT=production
- name: Sync Static Assets to S3
run: |
IMAGE_URI="${{ steps.login-ecr.outputs.registry }}/durianpy-cms-prod:${{ steps.version-bump.outputs.new_tag }}"
CONTAINER_ID=$(docker create "$IMAGE_URI")
mkdir -p .next
docker cp "$CONTAINER_ID":/app/.next/static .next/static
docker cp "$CONTAINER_ID":/app/public public
docker rm "$CONTAINER_ID"
aws s3 sync .next/static s3://${{ steps.ssm-param.outputs.S3_BUCKET }}/artifacts/_next/static --delete
aws s3 sync public s3://${{ steps.ssm-param.outputs.S3_BUCKET }}/public --delete
- name: Push Image
if: success()
run: |
docker push ${{ steps.login-ecr.outputs.registry }}/durianpy-cms-prod:${{ steps.version-bump.outputs.new_tag }}
- name: Summarize Build
if: always()
run: |
echo "### 🚢 Deployment Summary" >> $GITHUB_STEP_SUMMARY
echo "Image: \`${{ steps.login-ecr.outputs.registry }}/durianpy-cms-prod:${{ steps.version-bump.outputs.new_tag }}\`" >> $GITHUB_STEP_SUMMARY
- name: Deploy to AWS Lambda
if: success()
run: |
aws lambda update-function-code \
--function-name durianpy-cms-prod \
--image-uri ${{ steps.login-ecr.outputs.registry }}/durianpy-cms-prod:${{ steps.version-bump.outputs.new_tag }}
- name: Commit and Push Version Bump and CHANGELOG
if: success()
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NEW_TAG: ${{ steps.version-bump.outputs.new_tag }}
run: |
# 1. Generate release notes from GitHub API
echo "Generating release notes for $NEW_TAG..."
RELEASE_NOTES=$(gh api --method POST /repos/"$GITHUB_REPOSITORY"/releases/generate-notes -f tag_name="$NEW_TAG" -f target_commitish="release/production" --jq .body)
# 2. Prepend notes to CHANGELOG.md
DATE_STR=$(date +%Y-%m-%d)
if [ -f CHANGELOG.md ]; then
echo -e "# Release $NEW_TAG ($DATE_STR)\n\n$RELEASE_NOTES\n\n$(cat CHANGELOG.md)" > CHANGELOG.md
else
echo -e "# Release $NEW_TAG ($DATE_STR)\n\n$RELEASE_NOTES" > CHANGELOG.md
fi
# 3. Commit and push changes
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add package.json CHANGELOG.md
git commit --no-verify -m "chore(release): bump version to $NEW_TAG and update CHANGELOG [skip ci]"
git push origin release/production
- name: Create Git Tag and Release
if: success()
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NEW_TAG: ${{ steps.version-bump.outputs.new_tag }}
run: |
echo "🚀 Creating GitHub Release for $NEW_TAG..."
gh release create "$NEW_TAG" \
--title "Release $NEW_TAG" \
--generate-notes \
--target "release/production"