Skip to content

Commit c3bb4f4

Browse files
committed
feature(deployment) update deploy script
1 parent 10f0238 commit c3bb4f4

File tree

9 files changed

+202
-50
lines changed

9 files changed

+202
-50
lines changed

.github/workflows/deploy-int.yml

+52-46
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,41 @@ name: Automated Release Deployment
22

33
on:
44
push:
5-
tags:
6-
- rc*
5+
branches:
6+
- main
7+
- 'feature/deploy'
8+
9+
env:
10+
NODE_VERSION: 16.17.0
11+
IP_ADDRESS: "49.12.188.8"
712

813
jobs:
914
test-application:
1015
runs-on: ubuntu-latest
11-
strategy:
12-
matrix:
13-
node-version: [ 16.17.0 ]
1416
steps:
1517
- uses: actions/checkout@v3
16-
- name: Use Node.js ${{ matrix.node-version }}
18+
- name: Test Application
1719
uses: actions/setup-node@v3
1820
with:
19-
node-version: ${{ matrix.node-version }}
21+
node-version: ${{env.NODE_VERSION}}
2022
cache: 'yarn'
21-
- run: yarn
22-
- run: yarn build
23+
- run: |
24+
yarn
25+
yarn build
26+
yarn test
2327
2428
create-deployment-artifacts:
2529
needs: test-application
2630
runs-on: ubuntu-latest
27-
strategy:
28-
matrix:
29-
node-version: [ 16.17.0 ]
3031
steps:
3132
- uses: actions/checkout@v3
32-
- name: Use Node.js ${{ matrix.node-version }}
33+
- name: Build App Artifacts
34+
env:
35+
GITHUB_SHA: ${{ github.sha }}
36+
STRIPE_SECRET_KEY: ${{ secrets.STRIPE_SECRET_TEST_KEY }}
3337
uses: actions/setup-node@v3
3438
with:
35-
node-version: ${{ matrix.node-version }}
39+
node-version: ${{env.NODE_VERSION}}
3640
cache: 'yarn'
3741
- run: |
3842
touch .env
@@ -41,84 +45,83 @@ jobs:
4145
echo APP_DOMAIN=https://fullstackjack.dev >> .env
4246
echo RELEASE_VERSION=${GITHUB_REF} >> .env
4347
echo GITHUB_SHA=${GITHUB_SHA} >> .env
44-
- run: yarn
45-
- run: yarn build
46-
- name: Create deployment artifact
47-
env:
48-
GITHUB_SHA: ${{ github.sha }}
49-
STRIPE_SECRET_KEY: ${{ secrets.STRIPE_SECRET_TEST_KEY }}
50-
run: tar -czf "${GITHUB_SHA}".tar.gz .output
51-
52-
- name: Store artifact for distribution
53-
uses: actions/upload-artifact@v2
48+
yarn
49+
yarn build
50+
cp .env .output/server/.env
51+
cp .env server/database/
52+
tar -czf "${GITHUB_SHA}".tar.gz .output
53+
tar -czf "${GITHUB_SHA}"-database.tar.gz -C ./server database
54+
- name: Store app-artifacts for distribution
55+
uses: actions/upload-artifact@v3
5456
with:
55-
name: app-build
57+
name: app-artifacts
5658
path: ${{ github.sha }}.tar.gz
5759

58-
- name: Store .env
59-
uses: actions/upload-artifact@v2
60+
- name: Store database-artifacts for distribution
61+
uses: actions/upload-artifact@v3
6062
with:
61-
name: env
62-
path: .env
63+
name: database-artifacts
64+
path: ${{ github.sha }}-database.tar.gz
6365

6466
prepare-release-on-servers:
6567
needs: create-deployment-artifacts
6668
name: "Prepare release on INT server"
6769
runs-on: ubuntu-latest
6870
steps:
69-
- uses: actions/download-artifact@v2
71+
- uses: actions/download-artifact@v3
7072
with:
71-
name: app-build
72-
- uses: actions/download-artifact@v2
73+
name: app-artifacts
74+
- uses: actions/download-artifact@v3
7375
with:
74-
name: env
75-
- name: Upload app-build
76+
name: database-artifacts
77+
- name: Upload app-artifacts
7678
uses: appleboy/scp-action@master
7779
with:
78-
host: "49.12.188.8"
80+
host: ${{env.IP_ADDRESS}}
7981
port: "22"
8082
username: "root"
8183
key: ${{ secrets.SSH_KEY }}
8284
source: ${{ github.sha }}.tar.gz
8385
target: /var/www/html/artifacts
8486

85-
- name: Upload env
87+
- name: Upload database-artifacts
8688
uses: appleboy/scp-action@master
8789
with:
88-
host: "49.12.188.8"
90+
host: ${{env.IP_ADDRESS}}
8991
port: "22"
9092
username: "root"
9193
key: ${{ secrets.SSH_KEY }}
92-
source: .env
93-
target: /data/env
94+
source: ${{ github.sha }}-database.tar.gz
95+
target: /var/www/html/artifacts
9496

9597
- name: Extract archive and create directories
9698
uses: appleboy/ssh-action@master
9799
env:
98100
GITHUB_SHA: ${{ github.sha }}
99101
with:
100-
host: "49.12.188.8"
102+
host: ${{env.IP_ADDRESS}}
101103
username: "root"
102104
key: ${{ secrets.SSH_KEY }}
103105
port: "22"
104106
envs: GITHUB_SHA
105107
script: |
106108
mkdir -p "/var/www/html/releases/${GITHUB_SHA}"
107109
tar xzf /var/www/html/artifacts/${GITHUB_SHA}.tar.gz -C "/var/www/html/releases/${GITHUB_SHA}"
110+
tar xzf /var/www/html/artifacts/${GITHUB_SHA}-database.tar.gz -C "/var/www/html"
108111
rm -rf /var/www/html/artifacts/${GITHUB_SHA}.tar.gz
109112
110113
activate-release:
111114
name: "Activate release"
112115
runs-on: ubuntu-latest
113116
needs: prepare-release-on-servers
114117
steps:
115-
- name: Activate release
118+
- name: Activate Release
116119
uses: appleboy/ssh-action@master
117120
env:
118121
RELEASE_PATH: /var/www/html/releases/${{ github.sha }}
119122
ACTIVE_RELEASE_PATH: /var/www/html/live
120123
with:
121-
host: "49.12.188.8"
124+
host: ${{env.IP_ADDRESS}}
122125
username: "root"
123126
key: ${{ secrets.SSH_KEY }}
124127
port: "22"
@@ -127,7 +130,8 @@ jobs:
127130
ln -s -n -f $RELEASE_PATH $ACTIVE_RELEASE_PATH
128131
systemctl restart fullstackjack
129132
chown -R www-data:www-data ${RELEASE_PATH}
130-
npx prisma migrate deploy --schema=/var/www/html/live/.output/server/node_modules/.prisma/client/schema.prisma
133+
chown -R www-data:www-data /var/www/html/database
134+
cd /var/www/html/database && npx prisma migrate deploy
131135
132136
clean-up:
133137
name: "Clean up old versions"
@@ -137,14 +141,16 @@ jobs:
137141
- name: clean up old releases
138142
uses: appleboy/ssh-action@master
139143
with:
140-
host: "49.12.188.8"
144+
host: ${{env.IP_ADDRESS}}
141145
username: "root"
142146
key: ${{ secrets.SSH_KEY }}
143147
port: "22"
144148
script: |
145149
cd /var/www/html/releases && ls -t -1 | tail -n +4 | xargs rm -rf
146150
cd /var/www/html/artifacts && rm -rf *
147-
# delete-artifact
148151
- uses: geekyeggo/delete-artifact@v1
149152
with:
150-
name: create-deployment-artifacts
153+
name: app-artifacts
154+
- uses: geekyeggo/delete-artifact@v1
155+
with:
156+
name: database-artifacts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
title: Deploy Nuxt3 via Github Actions
3+
description: Let's get to action deploying Nuxt3 via Github Actions
4+
slug: deploying-nuxt3-via-github-actions
5+
author: Rick Rohrig
6+
date: 29 Sep 2022
7+
subject: Nuxt Routing
8+
position: 1
9+
---
10+
11+
Hello [World]{.bg-blue-500}!
12+
13+
Automating deployments is pretty much standard these days. Manually typing in deployment commands is too time intensive and failure prone.
14+
15+
One option is GitHub Actions. If your project is open source, like **fullstackjack.dev**, it's also cost free.
16+
17+
## Bird's Eye View
18+
19+
20+
![photo of github jobs flow diagram](https://fullstackjack.dev/img/deployment-jobs-overview.png "https://github.com/jurassicjs/nuxt3-fullstack-tutorial/actions")
21+
22+
### It all begins with a workflow.
23+
A workflow needs a **trigger**, with can be as simple as pushing to a branch or something slightly more advanced like creating a release.
24+
25+
```yaml
26+
on:
27+
push:
28+
branches:
29+
- main
30+
- 'feature/deploy'
31+
```
32+
33+
In this example, the workflow will be triggered every time we push to the **main** or **feature/deploy** branches.
34+
35+
#### Jobs
36+
A workflow contains one or more **jobs**.
37+
38+
```yaml
39+
jobs:
40+
test-application:
41+
create-artifacts:
42+
prepare-release:
43+
activate-release:
44+
clean-up:
45+
```
46+
Each of these jobs will contain one or more **steps**.
47+
48+
#### Steps
49+
Let's take a look at the simplest job, **test-application**
50+
51+
```yaml
52+
jobs:
53+
test-application:
54+
runs-on: ubuntu-latest
55+
strategy:
56+
matrix:
57+
node-version: [ 16.17.0 ]
58+
steps:
59+
- uses: actions/checkout@v3
60+
- name: Use Node.js ${{ matrix.node-version }}
61+
uses: actions/setup-node@v3
62+
with:
63+
node-version: ${{ matrix.node-version }}
64+
cache: 'yarn'
65+
- run: yarn
66+
- run: yarn build
67+
- run: yarn test
68+
```
69+
70+
At the risk of being redundant, I'll walk through what each line means.
71+
72+
```yaml
73+
runs-on: ubuntu-latest
74+
```
75+
here is where you chose what operating system you want your job to run on.
76+
77+
```yaml
78+
strategy:
79+
matrix:
80+
node-version: [ 16.17.0 ]
81+
```
82+
If you add a matrix, the job will run against each entry in the matrix. In this case, we've only listed one node version, so the job will only run against that node version.
83+
84+
```yaml
85+
steps:
86+
- uses: actions/checkout@v3
87+
- name: Use Node.js ${{ matrix.node-version }}
88+
uses: actions/setup-node@v3
89+
with:
90+
node-version: ${{ matrix.node-version }}
91+
cache: 'yarn'
92+
- run: yarn
93+
- run: yarn build
94+
- run: yarn test
95+
```
96+
::alert{icon=👉}
97+
steps is where things get interesting.
98+
::
99+
100+
We use Steps to run our deployment commands. It's like teaching someone every step you'd normally do manually to deploy,
101+
but you know they will never make typos or do things in the wrong order. In other words, you've got the perfect sidekick.
102+
103+
The steps in the **test-application** are pretty straight forward, but as you'll see shortly, things can escalate quickly.
104+
105+
::alert{type=success icon=😎}
106+
One super cool feature with GitHub Actions is you can break off the entire process if one job fails. So, for example, if your tests fail, you won't
107+
accidentally ship broken code.
108+
::

docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: '3.6'
22

33
services:
44
mysql:
5-
image: library/mysql:5.7.36
5+
image: mysql:8.0.30
66
ports:
77
- "127.0.0.1:3306:3306"
88
environment:

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"dev": "nuxt dev",
66
"generate": "nuxt generate",
77
"preview": "nuxt preview",
8-
"test": "echo todo"
8+
"test": "echo todo",
9+
"prisma:generate": "dotenv -e .env -- npx prisma generate",
10+
"prisma:migrate": "dotenv -e .env -- npx prisma migrate deploy --name prod"
911
},
1012
"devDependencies": {
1113
"@nuxt/content": "^2.1.1",
@@ -25,6 +27,7 @@
2527
"@tailwindcss/typography": "^0.5.7",
2628
"@vueuse/core": "^9.2.0",
2729
"bcrypt": "^5.0.1",
30+
"dotenv-cli": "^6.0.0",
2831
"prisma": "^4.3.1",
2932
"stripe": "^10.8.0",
3033
"uuid": "^9.0.0"
19.3 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- CreateTable
2+
CREATE TABLE `TestMigration` (
3+
`id` INTEGER NOT NULL AUTO_INCREMENT,
4+
5+
PRIMARY KEY (`id`)
6+
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- CreateTable
2+
CREATE TABLE `TestAnotherMigration` (
3+
`id` INTEGER NOT NULL AUTO_INCREMENT,
4+
5+
PRIMARY KEY (`id`)
6+
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

server/database/schema.prisma

+8
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,11 @@ model CategoryAssignment {
143143
category Category @relation(fields: [categoryId], references: [id])
144144
categoryId Int
145145
}
146+
147+
model TestMigration {
148+
id Int @id @default(autoincrement())
149+
}
150+
151+
model TestAnotherMigration {
152+
id Int @id @default(autoincrement())
153+
}

yarn.lock

+17-2
Original file line numberDiff line numberDiff line change
@@ -1993,7 +1993,22 @@ dot-prop@^7.2.0:
19931993
dependencies:
19941994
type-fest "^2.11.2"
19951995

1996-
dotenv@^16.0.2:
1996+
dotenv-cli@^6.0.0:
1997+
version "6.0.0"
1998+
resolved "https://registry.yarnpkg.com/dotenv-cli/-/dotenv-cli-6.0.0.tgz#8a30cbc59d0a8aaa166b2fee0a9a55e23a1223ab"
1999+
integrity sha512-qXlCOi3UMDhCWFKe0yq5sg3X+pJAz+RQDiFN38AMSbUrnY3uZshSfDJUAge951OS7J9gwLZGfsBlWRSOYz/TRg==
2000+
dependencies:
2001+
cross-spawn "^7.0.3"
2002+
dotenv "^16.0.0"
2003+
dotenv-expand "^8.0.1"
2004+
minimist "^1.2.5"
2005+
2006+
dotenv-expand@^8.0.1:
2007+
version "8.0.3"
2008+
resolved "https://registry.yarnpkg.com/dotenv-expand/-/dotenv-expand-8.0.3.tgz#29016757455bcc748469c83a19b36aaf2b83dd6e"
2009+
integrity sha512-SErOMvge0ZUyWd5B0NXMQlDkN+8r+HhVUsxgOO7IoPDOdDRD2JjExpN6y3KnFR66jsJMwSn1pqIivhU5rcJiNg==
2010+
2011+
dotenv@^16.0.0, dotenv@^16.0.2:
19972012
version "16.0.2"
19982013
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.0.2.tgz#0b0f8652c016a3858ef795024508cddc4bffc5bf"
19992014
integrity sha512-JvpYKUmzQhYoIFgK2MOnF3bciIZoItIIoryihy0rIA+H4Jy0FmgyKYAHCTN98P5ybGSJcIFbh6QKeJdtZd1qhA==
@@ -3920,7 +3935,7 @@ minimatch@~3.0.4:
39203935
dependencies:
39213936
brace-expansion "^1.1.7"
39223937

3923-
minimist@^1.2.6:
3938+
minimist@^1.2.5, minimist@^1.2.6:
39243939
version "1.2.6"
39253940
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.6.tgz#8637a5b759ea0d6e98702cfb3a9283323c93af44"
39263941
integrity sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==

0 commit comments

Comments
 (0)