-
Notifications
You must be signed in to change notification settings - Fork 0
205 lines (179 loc) · 6.81 KB
/
Copy pathdeploy_note_app.yml
File metadata and controls
205 lines (179 loc) · 6.81 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
name: CI/CD Pipeline
on:
pull_request:
branches:
- main
push:
branches:
- main
env:
PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
SERVICE_NAME: notes-app-service
REGION: us-central1
GAR_LOCATION: us-central1 # Location for Artifact Registry
jobs:
test:
name: Build and Test Suite
runs-on: ubuntu-24.04
steps:
- name: Checkout repository
uses: actions/checkout@v4.1.7
- name: Set up Node.js
uses: actions/setup-node@v4.0.3
with:
node-version: 20
cache: 'npm'
cache-dependency-path: '**/package-lock.json'
# Backend setup and tests
- name: Install backend dependencies
working-directory: ./part3/notes_fullstack
run: npm ci
- name: Lint backend code
working-directory: ./part3/notes_fullstack
run: npm run lint
- name: Run backend unit tests
working-directory: ./part3/notes_fullstack
run: npm test
env:
TEST_MONGODB_URI: ${{ secrets.TEST_MONGODB_URI }}
SECRET: ${{ secrets.SECRET }}
# Frontend setup and build
- name: Install frontend dependencies
working-directory: ./part2/notes_frontend
run: npm ci
- name: Lint frontend code
working-directory: ./part2/notes_frontend
run: npm run lint
- name: Build frontend for E2E tests
working-directory: ./part3/notes_fullstack
run: npm run build:ui
# E2E tests
- name: Install Playwright browsers
working-directory: ./part3/notes_fullstack
run: npx playwright install --with-deps
- name: Run Playwright E2E tests
working-directory: ./part3/notes_fullstack
run: npm run test:e2e
env:
TEST_MONGODB_URI: ${{ secrets.TEST_MONGODB_URI }}
SECRET: ${{ secrets.SECRET }}
- name: Upload Playwright report
uses: actions/upload-artifact@v4.3.4
if: always()
with:
name: playwright-report
path: part3/notes_fullstack/playwright-report/
retention-days: 10
- name: Upload build artifact for deployment
if: github.event_name == 'push'
uses: actions/upload-artifact@v4.3.4
with:
name: production-build
path: part3/notes_fullstack/dist
deploy:
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
name: Deploy to Cloud Run
needs: test
runs-on: ubuntu-24.04
permissions:
contents: 'read'
id-token: 'write' # Required for Workload Identity Federation
steps:
- name: Checkout repository
uses: actions/checkout@v4.1.7
- name: Download build artifact
uses: actions/download-artifact@v4.1.8
with:
name: production-build
path: part3/notes_fullstack/dist
- name: Authenticate to Google Cloud
id: auth
uses: 'google-github-actions/auth@v2.1.3'
with:
workload_identity_provider: ${{ secrets.GCP_WIF_PROVIDER }}
service_account: ${{ secrets.GCP_SA_EMAIL }}
- name: Configure Docker
run: gcloud auth configure-docker ${{ env.GAR_LOCATION }}-docker.pkg.dev
- name: Build and push Docker image to Artifact Registry
run: |
docker build . -f part3/notes_fullstack/Dockerfile -t ${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.SERVICE_NAME }}/${{ env.SERVICE_NAME }}:${{ github.sha }}
docker push ${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.SERVICE_NAME }}/${{ env.SERVICE_NAME }}:${{ github.sha }}
- name: Deploy image to Cloud Run
run: |
gcloud run deploy ${{ env.SERVICE_NAME }} \
--image ${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.SERVICE_NAME }}/${{ env.SERVICE_NAME }}:${{ github.sha }} \
--region ${{ env.REGION }} \
--allow-unauthenticated \
--set-env-vars "MONGODB_URI=${{ secrets.MONGODB_URI }},SECRET=${{ secrets.SECRET }}"
tag_release:
name: Tag Release
needs: [deploy]
runs-on: ubuntu-24.04
permissions:
contents: 'write' # Required to push new tags
# Skip tagging if the commit message contains #skip for push events on the target branch
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && !contains(github.event.head_commit.message, '#skip') }}
outputs:
tag: ${{ steps.tag_version.outputs.tag }}
changelog: ${{ steps.tag_version.outputs.changelog }}
steps:
- name: Checkout repository
uses: actions/checkout@v4.1.7
with:
fetch-depth: 0 # Required for changelog generation
- name: Bump version and push tag
id: tag_version
uses: anothrNick/github-tag-action@1.73.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TAG_PREFIX: v
DEFAULT_BUMP: patch
- name: Log new tag
run: echo "New tag is ${{ steps.tag_version.outputs.tag }}"
create_release:
name: Create GitHub Release
needs: [tag_release]
runs-on: ubuntu-24.04
permissions:
contents: 'write' # Required to create a release
steps:
- name: Create GitHub Release
uses: actions/create-release@v1.1.4
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ needs.tag_release.outputs.tag }}
release_name: Release ${{ needs.tag_release.outputs.tag }}
body: ${{ needs.tag_release.outputs.changelog }}
draft: false
prerelease: false
notify:
name: Discord Notification
needs: [test, create_release]
runs-on: ubuntu-24.04
if: always() # This is key - it runs even if previous jobs fail
steps:
- name: Determine Status
id: status_check
run: |
if [[ "${{ needs.test.result }}" == "failure" || "${{ needs.create_release.result }}" == "failure" ]]; then
echo "status=failure" >> $GITHUB_OUTPUT
echo "color=15548997" >> $GITHUB_OUTPUT
echo "status_text=Failed" >> $GITHUB_OUTPUT
else
echo "status=success" >> $GITHUB_OUTPUT
echo "color=5763719" >> $GITHUB_OUTPUT
echo "status_text=Success" >> $GITHUB_OUTPUT
fi
- name: Send Discord notification
uses: sarisia/actions-status-discord@v1
with:
webhook: ${{ secrets.DISCORD_WEBHOOK }}
status: ${{ steps.status_check.outputs.status }}
title: "Pipeline for `${{ github.repository }}`"
description: |
Workflow **${{ github.workflow }}** has finished with status: **${{ steps.status_check.outputs.status_text }}**
Commit: `${{ github.sha }}` by `${{ github.actor }}`
View Workflow Run
color: ${{ steps.status_check.outputs.color }}
username: "GitHub Actions"