forked from doctor-smith/hanoi-towers
-
Notifications
You must be signed in to change notification settings - Fork 0
160 lines (137 loc) · 5.19 KB
/
main.yml
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
name: CI Pipeline Hanoi Towers
on:
push:
branches:
- master
- staging
- develop
jobs:
set-environment:
runs-on: ubuntu-latest
outputs:
env: ${{ steps.set-env.outputs.env }}
steps:
- name: Determine environment
id: set-env
run: |
if [[ "${{ github.ref }}" == "refs/heads/master" ]]; then
echo "env=prod" >> $GITHUB_OUTPUT
elif [[ "${{ github.ref }}" == "refs/heads/staging" ]]; then
echo "env=staging" >> $GITHUB_OUTPUT
elif [[ "${{ github.ref }}" == "refs/heads/develop" ]]; then
echo "env=develop" >> $GITHUB_OUTPUT
fi
build-backend:
runs-on: ubuntu-latest
needs: set-environment
environment: ${{ needs.set-environment.outputs.env }}
outputs:
backend_changed: ${{ steps.backend-check.outputs.backend_changed }}
env:
JWT_SECRET: ${{ secrets.JWT_SECRET }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
ENVIRONMENT: ${{ needs.set-environment.outputs.env }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check for backend changes
id: backend-check
run: |
if git diff --name-only ${{ github.event.before }} ${{ github.event.after }} | grep -q '^hanoi-backend/'; then
echo "backend_changed=true" >> $GITHUB_ENV
echo "backend_changed=true" >> $GITHUB_OUTPUT
else
echo "backend_changed=false" >> $GITHUB_ENV
echo "backend_changed=false" >> $GITHUB_OUTPUT
fi
- name: Set up .env file
if: env.backend_changed == 'true'
run: |
sed -i "s|^JWT_SECRET=.*|JWT_SECRET=${JWT_SECRET}|" hanoi-backend/.env
sed -i "s|^DATABASE_PASSWORD=.*|DATABASE_PASSWORD=${DB_PASSWORD}|" hanoi-backend/.env
- name: Set up JDK 17
if: env.backend_changed == 'true'
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
- name: Grant execute permission for Gradle Wrapper
if: env.backend_changed == 'true'
run: chmod +x ./gradlew
- name: Build backend
if: env.backend_changed == 'true'
run: |
./gradlew :hanoi-backend:build
- name: Cache backend build artifacts
if: env.backend_changed == 'true'
uses: actions/cache@v4
with:
path: hanoi-backend/build/libs
key: backend-${{ env.ENVIRONMENT }}-${{ runner.os }}-${{ hashFiles('hanoi-backend/**') }}
restore-keys: |
backend-${{ env.ENVIRONMENT }}-${{ runner.os }}-
- name: Upload backend artifact
if: env.backend_changed == 'true'
uses: actions/upload-artifact@v4
with:
name: backend-artifact-${{ env.ENVIRONMENT }}
path: hanoi-backend/build/libs
build-frontend:
runs-on: ubuntu-latest
needs: set-environment
environment: ${{ needs.set-environment.outputs.env }}
outputs:
frontend_changed: ${{ steps.frontend-check.outputs.frontend_changed }}
env:
HANOI_FRONTEND_URL: ${{ vars.HANOI_FRONTEND_URL }}
HANOI_BACKEND_URL: ${{ vars.HANOI_BACKEND_URL }}
ENVIRONMENT: ${{ needs.set-environment.outputs.env }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check for frontend changes
id: frontend-check
run: |
if git diff --name-only ${{ github.event.before }} ${{ github.event.after }} | grep -q '^hanoi-frontend/'; then
echo "frontend_changed=true" >> $GITHUB_ENV
echo "frontend_changed=true" >> $GITHUB_OUTPUT
else
echo "frontend_changed=false" >> $GITHUB_ENV
echo "frontend_changed=false" >> $GITHUB_OUTPUT
fi
- name: Set up .env file
if: env.frontend_changed == 'true'
run: |
sed -i "s|^HANOI_FRONTEND_URL=.*|HANOI_FRONTEND_URL=${HANOI_FRONTEND_URL}|" hanoi-frontend/.env
sed -i "s|^HANOI_BACKEND_URL=.*|HANOI_BACKEND_URL=${HANOI_BACKEND_URL}|" hanoi-frontend/.env
- name: Set up JDK 17
if: env.frontend_changed == 'true'
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
- name: Grant execute permission for Gradle Wrapper
if: env.frontend_changed == 'true'
run: chmod +x ./gradlew
- name: Build frontend
if: env.frontend_changed == 'true'
run: |
./gradlew :hanoi-frontend:build
- name: Cache frontend build artifacts
if: env.frontend_changed == 'true'
uses: actions/cache@v4
with:
path: hanoi-frontend/build/dist/js/productionExecutable
key: frontend-${{ env.ENVIRONMENT }}-${{ runner.os }}-${{ hashFiles('hanoi-frontend/**') }}
restore-keys: |
frontend-${{ env.ENVIRONMENT }}-${{ runner.os }}-
- name: Upload frontend artifact
if: env.frontend_changed == 'true'
uses: actions/upload-artifact@v4
with:
name: frontend-artifact-${{ env.ENVIRONMENT }}
path: hanoi-frontend/build/dist/js/productionExecutable