Skip to content

Commit c6a4231

Browse files
committed
Added CI/CD
1 parent b038e3b commit c6a4231

5 files changed

Lines changed: 122 additions & 1 deletion

File tree

.github/workflows/cd.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: CD
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- master
8+
workflow_dispatch:
9+
10+
concurrency:
11+
group: sg-stats-server-prod
12+
cancel-in-progress: true
13+
14+
jobs:
15+
deploy:
16+
name: Deploy to Production
17+
runs-on: ubuntu-latest
18+
timeout-minutes: 30
19+
20+
steps:
21+
- name: Deploy over SSH
22+
uses: appleboy/ssh-action@v1.2.2
23+
env:
24+
APP_DIR: ${{ secrets.CD_APP_DIR }}
25+
BRANCH: ${{ github.ref_name }}
26+
with:
27+
host: ${{ secrets.CD_SSH_HOST }}
28+
username: ${{ secrets.CD_SSH_USER }}
29+
key: ${{ secrets.CD_SSH_PRIVATE_KEY }}
30+
port: ${{ secrets.CD_SSH_PORT }}
31+
script_stop: true
32+
command_timeout: 30m
33+
envs: APP_DIR,BRANCH
34+
script: |
35+
export NVM_DIR="$HOME/.nvm"
36+
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
37+
bash "$APP_DIR/deploy/remote-deploy.sh" "$APP_DIR" "$BRANCH"

.github/workflows/ci.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
- master
9+
10+
jobs:
11+
test:
12+
name: Run tests
13+
runs-on: ubuntu-latest
14+
timeout-minutes: 10
15+
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v4
19+
20+
- name: Setup Node
21+
uses: actions/setup-node@v4
22+
with:
23+
node-version-file: .nvmrc
24+
cache: npm
25+
26+
- name: Install dependencies
27+
run: npm ci
28+
29+
- name: Run tests
30+
run: npm test

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v18.14.0
1+
v22.16.0

deploy/remote-deploy.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
if [[ "$#" -lt 2 ]]; then
5+
printf 'Usage: %s <app_dir> <branch>\n' "$0" >&2
6+
exit 64
7+
fi
8+
9+
APP_DIR="$1"
10+
BRANCH="$2"
11+
12+
if [[ ! -d "$APP_DIR/.git" ]]; then
13+
printf 'ERROR: %s is not a git repository\n' "$APP_DIR" >&2
14+
exit 66
15+
fi
16+
17+
if ! command -v pm2 >/dev/null 2>&1; then
18+
printf 'ERROR: pm2 is not installed on server\n' >&2
19+
exit 69
20+
fi
21+
22+
cd "$APP_DIR"
23+
24+
git fetch --prune origin
25+
26+
if git show-ref --verify --quiet "refs/heads/$BRANCH"; then
27+
git checkout "$BRANCH"
28+
else
29+
git checkout -b "$BRANCH" "origin/$BRANCH"
30+
fi
31+
32+
git reset --hard "origin/$BRANCH"
33+
34+
npm ci --omit=dev
35+
pm2 startOrReload ecosystem.config.cjs --update-env
36+
pm2 save
37+
38+
echo "Deploy finished: branch=$BRANCH dir=$APP_DIR"

ecosystem.config.cjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = {
2+
apps: [
3+
{
4+
name: 'sg-stats-server',
5+
script: 'src/server.js',
6+
cwd: __dirname,
7+
instances: 1,
8+
autorestart: true,
9+
watch: false,
10+
max_memory_restart: '300M',
11+
env: {
12+
NODE_ENV: 'production'
13+
}
14+
}
15+
]
16+
};

0 commit comments

Comments
 (0)