-
Notifications
You must be signed in to change notification settings - Fork 0
150 lines (125 loc) · 4.48 KB
/
deploy-frontend.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
# GitHub action to deploy MartinBullmanApp frontend.
name: deploy-frontend
on:
# trigger workflow on pushes to the main branch or pull requests targeting the main branch.
push:
branches:
- main
pull_request:
branches:
- main
env:
# set environment variables for node.js version and github context values.
NODE_VERSION: 20
FILENAME: "${{ github.sha }}-${{ github.run_id }}"
GITHUB_SHA: ${{ github.sha }}
GITHUB_REF: ${{ github.ref }}
jobs:
lint:
runs-on: ubuntu-latest
steps:
# step 1: checkout the code from the repository.
- name: Checkout Code
uses: actions/checkout@v4
# step 2: set up Node.js environment.
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
# step 3: install project dependencies.
- name: Install Dependencies
working-directory: ./frontend
run: npm install
# Step 4: Run ESLint to lint the code.
- name: Run Lint
working-directory: ./frontend
run: npm run lint
test:
needs: lint
runs-on: ubuntu-latest
steps:
# step 1: checkout the code from the repository.
- name: Checkout Code
uses: actions/checkout@v4
# step 2: set up Node.js environment.
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
# step 3: install project dependencies.
- name: Install Dependencies
working-directory: ./frontend
run: npm install
# Step 4: Run ESLint to lint the code.
- name: Run Tests
working-directory: ./frontend
run: npm run test
build:
needs: test
runs-on: ubuntu-latest
steps:
# step 1: checkout the code from the repository.
- name: Checkout Code
uses: actions/checkout@v4
# step 2: install project dependencies.
- name: Install Dependencies
working-directory: ./frontend
run: npm install
- name: Create .env File
working-directory: ./frontend
run: |
echo "BASE_URL=${{ secrets.BASE_URL }}" >> .env
echo "API_BASE_URL=${{ secrets.API_BASE_URL }}" >> .env
echo "GOOGLE_RECAPTCHA_ID=${{ secrets.GOOGLE_RECAPTCHA_ID }}" >> .env
echo "GOOGLE_RECAPTCHA_SITE_KEY=${{ secrets.GOOGLE_RECAPTCHA_SITE_KEY }}" >> .env
# step 3: build the frontend application.
- name: Build Application
working-directory: ./frontend
run: npm run build
# step 4: archive the build output into a tarball.
- name: Archive Artifacts
working-directory: ./frontend/
run: tar -czf ../${{ env.FILENAME }}.tar.gz .output
# step 5: upload the archived artifacts for later use in deployment.
- name: Store Artifacts
uses: actions/upload-artifact@v4
with:
name: frontend-artifacts
path: ${{ env.FILENAME }}.tar.gz
deploy:
# deployment job depends on the build job.
needs: build
runs-on: ubuntu-latest
steps:
# step 1: download the build artifacts from the build job.
- name: Download Artifacts
uses: actions/download-artifact@v4
with:
name: frontend-artifacts
# step 3: copy the frontend artifacts to the remote server.
- name: Deploy Artifacts
uses: appleboy/scp-action@master
with:
host: ${{ secrets.LIGHTSAIL_HOST }}
username: ubuntu
key: ${{ secrets.LIGHTSAIL_SSH_KEY }}
target: /home/ubuntu
source: ${{ env.FILENAME }}.tar.gz
# step 4: extract the artifacts and restart the application.
- name: Extract and Restart Frontend
uses: appleboy/ssh-action@v0.1.8
with:
host: ${{ secrets.LIGHTSAIL_HOST }}
username: ${{ secrets.LIGHTSAIL_USER }}
key: ${{ secrets.LIGHTSAIL_SSH_KEY }}
script: |
mkdir -p frontend_releases/${{ env.FILENAME }}
tar -xzf ${{ env.FILENAME }}.tar.gz -C frontend_releases/${{ env.FILENAME }}
cp -r frontend_releases/${{ env.FILENAME }}/.output MartinBullmanApp/frontend
rm -r ${{ env.GITHUB_SHA }}.tar.gz
# ensure node.js environment is loaded.
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm use 20.13.1
# restart frontend application.
.nvm/versions/node/v20.13.1/bin/pm2 restart all