Skip to content

Commit a874312

Browse files
authored
Merge pull request #655 from dzcode-io/chore/deploy--api-stage-to-zcluster
chore: P2 deploy `./api` stage to zcluster
2 parents d98f224 + 88e6368 commit a874312

File tree

9 files changed

+164
-25
lines changed

9 files changed

+164
-25
lines changed

.github/workflows/cd.deploy.stg.yml

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ jobs:
1313
node-version: "20"
1414
os: "ubuntu-latest"
1515

16-
deploy-api:
16+
docker-build-push-api:
1717
needs: build
1818
runs-on: ubuntu-latest
1919
env:
2020
CI: true
2121
STAGE: staging
22-
SSH_ADDRESS_STG: ${{ secrets.SSH_ADDRESS_STG }}
2322
DEPLOY_VERSION: ${{ github.ref_type == 'tag' && github.ref_name || format('stg-0.0.0-{0}-{1}-{2}', github.ref_name, github.run_number, github.run_attempt) }}
2423

2524
steps:
@@ -35,20 +34,33 @@ jobs:
3534
uses: actions/download-artifact@v4
3635
with:
3736
name: build output (ubuntu-latest, 20)
38-
- name: "SSH"
39-
uses: shimataro/ssh-key-action@v2
40-
with:
41-
key: ${{ secrets.SSH_KEY }}
42-
known_hosts: ${{ secrets.SSH_KNOWN_HOSTS }}
37+
4338
- name: "Generate Bundle info"
4439
run: npm run generate:bundle-info $DEPLOY_VERSION stage
40+
4541
- name: "Sentry Release"
4642
# todo-zm: remove sentry entirely
4743
run: cd ./api && npm run generate:sentry-release $DEPLOY_VERSION stage ${{ secrets.SENTRY_AUTH_TOKEN }}
48-
- name: "Deploy"
49-
run: cd ./api && npm run deploy:stg
5044

51-
deploy-web-server:
45+
- name: "Write ./api deps into Dockerfile..."
46+
run: |
47+
cd ./api
48+
npm run prepare-dockerfile
49+
50+
- name: Build docker image
51+
run: |
52+
docker buildx build -f api.Dockerfile . -t ghcr.io/dzcode-io/api-dot-stage-dot-dzcode-dot-io-server:latest
53+
env:
54+
DOCKER_BUILDKIT: 1
55+
56+
- name: Push docker image
57+
run: |
58+
echo $CR_PAT | docker login ghcr.io -u dzcode-io --password-stdin
59+
docker push ghcr.io/dzcode-io/api-dot-stage-dot-dzcode-dot-io-server:latest
60+
env:
61+
CR_PAT: ${{ secrets.CR_PAT }}
62+
63+
docker-build-push-web-server:
5264
needs: build
5365
runs-on: ubuntu-latest
5466
env:
@@ -80,7 +92,7 @@ jobs:
8092
- name: "Bundle ./web for deployment"
8193
run: |
8294
cd ./web
83-
npm run bundle
95+
npm run bundle:alone
8496
npm run pre-deploy
8597
8698
- name: "Write ./web-server deps into Dockerfile..."
@@ -101,6 +113,16 @@ jobs:
101113
env:
102114
CR_PAT: ${{ secrets.CR_PAT }}
103115

116+
deploy-to-zcluster:
117+
needs: [docker-build-push-api, docker-build-push-web-server]
118+
runs-on: ubuntu-latest
119+
env:
120+
CI: true
121+
122+
steps:
123+
- name: "Git"
124+
uses: actions/checkout@v4
125+
104126
- name: install zcluster
105127
run: |
106128
curl -fsSL https://infra.zak-man.com/install.sh | sh
@@ -110,3 +132,5 @@ jobs:
110132
run: zcluster deploy -p stage-dzcode ./docker-compose.stage.yml
111133
env:
112134
ADMIN_AUTH_TOKEN: ${{ secrets.ADMIN_AUTH_TOKEN }}
135+
OPENAI_KEY: ${{ secrets.OPENAI_KEY }}
136+
GITHUB_TOKEN: ${{ secrets.API_GITHUB_TOKEN }}

api.Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
FROM --platform=linux/amd64 node:22
2+
3+
WORKDIR /usr/src/repo
4+
5+
COPY ./package.json ./package.json
6+
7+
# AUTO_GEN
8+
COPY ./api/dist ./api/dist
9+
COPY ./api/package.json ./api/package.json
10+
COPY ./api/db ./api/db
11+
COPY ./packages/tooling/package.json ./packages/tooling/package.json
12+
COPY ./data/dist ./data/dist
13+
COPY ./data/package.json ./data/package.json
14+
COPY ./data/models ./data/models
15+
COPY ./packages/models/dist ./packages/models/dist
16+
COPY ./packages/models/package.json ./packages/models/package.json
17+
COPY ./packages/utils/dist ./packages/utils/dist
18+
COPY ./packages/utils/package.json ./packages/utils/package.json
19+
# AUTO_GEN_END
20+
21+
RUN npm install --omit=dev --workspace=@dzcode.io/api
22+
23+
ENV PORT=80
24+
WORKDIR /usr/src/repo/api
25+
CMD [ "npm", "run", "start" ]

api/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@
7878
"lint:prettier": "prettier --config ../packages/tooling/.prettierrc --ignore-path ../packages/tooling/.prettierignore --log-level warn",
7979
"lint:ts-prune": "tsx ../packages/tooling/setup-ts-prune.ts && ts-prune --error",
8080
"lint:tsc": "tspc --noEmit",
81-
"start": "wait-port postgres:5432 && wait-port meilisearch:7700 && delay 2 && node dist/app/index.js",
81+
"start": "node dist/app/index.js",
8282
"start:dev": "tsx ../packages/tooling/nodemon.ts \"@dzcode.io/api\" && npm-run-all --parallel start:nodemon db:server",
83+
"prepare-dockerfile": "tsx ../packages/tooling/write-dockerfile.ts @dzcode.io/api",
8384
"start:nodemon": "wait-port localhost:5432 && delay 2 && nodemon dist/app/index.js",
8485
"test": "npm run build && npm run test:alone",
8586
"test:alone": "jest --config ../packages/tooling/jest.config.ts --rootDir .",

docker-compose.stage.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
name: dzcode-stage
2+
13
services:
24
web:
35
image: ghcr.io/dzcode-io/stage-dot-dzcode-dot-io-server:latest
@@ -10,6 +12,56 @@ services:
1012
networks:
1113
- main-infra-network
1214

15+
api:
16+
image: ghcr.io/dzcode-io/api-dot-stage-dot-dzcode-dot-io-server:latest
17+
pull_policy: always
18+
restart: unless-stopped
19+
depends_on:
20+
postgres:
21+
condition: service_started
22+
meilisearch:
23+
condition: service_started
24+
environment:
25+
- VIRTUAL_HOST=api.stage.dzcode.io
26+
- LETSENCRYPT_HOST=api.stage.dzcode.io
27+
- NODE_ENV=staging
28+
- OPENAI_KEY={{OPENAI_KEY}}
29+
- GITHUB_TOKEN={{GITHUB_TOKEN}}
30+
volumes:
31+
- fetch_cache:/usr/src/repo/api/fetch_cache
32+
- sqlite_db:/usr/src/repo/api/sqlite_db
33+
networks:
34+
- main-infra-network
35+
- internal-network
36+
37+
postgres:
38+
image: postgres
39+
volumes:
40+
- postgres_db:/var/lib/postgresql/data
41+
environment:
42+
POSTGRES_HOST_AUTH_METHOD: trust
43+
POSTGRES_DB: db
44+
networks:
45+
- internal-network
46+
47+
meilisearch:
48+
image: getmeili/meilisearch:v1.11.3 # database schema is different between versions
49+
volumes:
50+
- meilisearch_db:/meili_data
51+
environment:
52+
MEILI_NO_ANALYTICS: true
53+
MEILI_MASTER_KEY: "default" # we only access it through `./api`
54+
networks:
55+
- internal-network
56+
1357
networks:
1458
main-infra-network:
1559
external: true
60+
internal-network:
61+
internal: true
62+
63+
volumes:
64+
postgres_db:
65+
meilisearch_db:
66+
fetch_cache:
67+
sqlite_db:

packages/tooling/write-dockerfile.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@ import { execSync } from "child_process";
22
import { existsSync, readFileSync, writeFileSync } from "fs";
33
import { join } from "path";
44

5+
const scope = process.argv[2];
6+
if (!scope) throw new Error("Please provide a scope");
7+
58
const workspaceRoot = join(__dirname, "../..");
6-
const dockerFilePath = join(workspaceRoot, "web-server.Dockerfile");
9+
const dockerFilePath = join(workspaceRoot, `${scope.replace("@dzcode.io/", "")}.Dockerfile`);
710

811
console.log(`writing ${dockerFilePath} ...`);
912

1013
const stdout = execSync(
11-
`lerna list --include-dependencies --json --all --loglevel silent --scope @dzcode.io/web-server`,
14+
`lerna list --include-dependencies --json --all --loglevel silent --scope ${scope}`,
1215
);
1316
const dependencies = JSON.parse(stdout.toString()) as Array<{ name: string; location: string }>;
14-
const subPaths = ["dist", "bundle", "package.json"];
17+
const subPaths = ["dist", "bundle", "package.json", "db", "models"];
1518
const directoriesToCopy = dependencies
1619
.map<string>(({ location }) => location)
1720
.reduce<Array<string>>(

packages/utils/src/config/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const fsConfig = (env: Environment, extra?: Record<string, unknown>) => {
1313
port: apiPort,
1414
url: [
1515
`http://${hostname}:${apiPort}`,
16-
"https://api-stage.dzcode.io",
16+
"https://api.stage.dzcode.io",
1717
"https://api.dzcode.io",
1818
][e],
1919
},

scripts/deploy.rs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ struct Args {
2222
/// Environment to deploy to
2323
#[arg(short, long, value_enum)]
2424
env: Env,
25+
26+
/// Clean build
27+
#[arg(short, long, default_value_t = false)]
28+
clean: bool,
2529
}
2630

2731
fn main() {
@@ -32,17 +36,26 @@ fn main() {
3236
println!("Ensuring docker is running ...");
3337
cli_run::cli_run("docker", vec!["ps"]);
3438

35-
println!("Building ./web-server ...");
36-
cli_run::cli_run("npm", vec!["run", "clean"]);
37-
cli_run::cli_run("npm", vec!["run", "bundle", "--workspace=@dzcode.io/web"]);
38-
cli_run::cli_run("npm", vec!["run", "pre-deploy", "--workspace=@dzcode.io/web"]);
39-
cli_run::cli_run("npm", vec!["run", "build", "--workspace=@dzcode.io/web-server"]);
39+
if args.clean {
40+
println!("clean...");
41+
cli_run::cli_run("npm", vec!["run", "clean"]);
42+
}
43+
44+
println!("Build...");
45+
cli_run::cli_run("npm", vec!["run", "build"]);
4046

41-
println!("Writing ./web-server deps into Dockerfile...");
47+
println!("Preparing ./web-server ...");
48+
cli_run::cli_run("npm", vec!["run", "bundle:alone", "--workspace=@dzcode.io/web"]);
49+
cli_run::cli_run("npm", vec!["run", "pre-deploy", "--workspace=@dzcode.io/web"]);
4250
cli_run::cli_run("npm", vec!["run", "prepare-dockerfile", "--workspace=@dzcode.io/web-server"]);
4351

44-
println!("Building docker image ...");
52+
println!("Preparing ./api ...");
53+
cli_run::cli_run("npm", vec!["run", "prepare-dockerfile", "--workspace=@dzcode.io/api"]);
54+
55+
4556
let env_str = format!("{:?}", env).to_lowercase();
57+
58+
println!("Building ./web-server docker image ...");
4659
cli_run::cli_run(
4760
"docker",
4861
vec![
@@ -56,6 +69,21 @@ fn main() {
5669
],
5770
);
5871

72+
println!("Building ./api docker image ...");
73+
cli_run::cli_run(
74+
"docker",
75+
vec![
76+
"buildx",
77+
"build",
78+
"-f",
79+
"api.Dockerfile",
80+
".",
81+
"-t",
82+
&format!("ghcr.io/dzcode-io/api-dot-{}-dot-dzcode-dot-io-server:latest", env_str),
83+
],
84+
);
85+
86+
5987
println!("Logging in to GitHub Container Registry ...");
6088
let gh_token = std::env::var("DOCKER_REGISTRY_PASSWORD")
6189
.expect("DOCKER_REGISTRY_PASSWORD environment variable not set");
@@ -76,6 +104,10 @@ fn main() {
76104
"docker",
77105
vec!["push", &format!("ghcr.io/dzcode-io/{}-dot-dzcode-dot-io-server:latest", env_str)],
78106
);
107+
cli_run::cli_run(
108+
"docker",
109+
vec!["push", &format!("ghcr.io/dzcode-io/api-dot-{}-dot-dzcode-dot-io-server:latest", env_str)],
110+
);
79111

80112
println!("Deploying to zcluster ...");
81113
cli_run::cli_run(

web-server.Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ COPY ./web-server/dist ./web-server/dist
99
COPY ./web-server/package.json ./web-server/package.json
1010
COPY ./api/dist ./api/dist
1111
COPY ./api/package.json ./api/package.json
12+
COPY ./api/db ./api/db
1213
COPY ./packages/models/dist ./packages/models/dist
1314
COPY ./packages/models/package.json ./packages/models/package.json
1415
COPY ./packages/utils/dist ./packages/utils/dist
@@ -19,9 +20,10 @@ COPY ./web/package.json ./web/package.json
1920
COPY ./packages/tooling/package.json ./packages/tooling/package.json
2021
COPY ./data/dist ./data/dist
2122
COPY ./data/package.json ./data/package.json
23+
COPY ./data/models ./data/models
2224
# AUTO_GEN_END
2325

24-
RUN npm install --install-strategy=nested --omit=dev --workspace=@dzcode.io/web-server
26+
RUN npm install --omit=dev --workspace=@dzcode.io/web-server
2527

2628
ENV PORT=80
2729
WORKDIR /usr/src/repo/web-server

web-server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"lint:prettier": "prettier --config ../packages/tooling/.prettierrc --ignore-path ../packages/tooling/.prettierignore --log-level warn",
4949
"lint:ts-prune": "tsx ../packages/tooling/setup-ts-prune.ts && ts-prune --error",
5050
"lint:tsc": "tsc --noEmit",
51-
"prepare-dockerfile": "tsx ../packages/tooling/write-dockerfile.ts",
51+
"prepare-dockerfile": "tsx ../packages/tooling/write-dockerfile.ts @dzcode.io/web-server",
5252
"prepare-web": "lerna run bundle --scope=@dzcode.io/web --include-dependencies --stream && lerna run pre-deploy --scope=@dzcode.io/web --include-dependencies --stream",
5353
"start": "node dist/app/index.js",
5454
"start:dev": "npm run prepare-web && tsx ../packages/tooling/nodemon.ts \"@dzcode.io/web-server\" && npm run start:nodemon",

0 commit comments

Comments
 (0)