Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
name: Deploy Website

on:
pull_request:
branches:
- main
types:
- opened
- synchronize
- reopened

concurrency:
group: deploy-preview-${{ github.event.pull_request.number }}
cancel-in-progress: true
Comment on lines +12 to +14

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concurrency group should include PR number to prevent different PRs from canceling each other's deployments

Suggested change
concurrency:
group: deploy-preview
cancel-in-progress: true
concurrency:
group: deploy-preview-${{ github.event.pull_request.number }}
cancel-in-progress: true


jobs:
deploy:
runs-on: ubuntu-latest

steps:
- name: Deploy to server
uses: appleboy/ssh-action@v1
env:
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: ${{ secrets.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY }}
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY
script: |
set -e

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"

DEPLOY_DIR="$HOME/loglife-preview"
APP_NAME=loglife-preview
PORT=3001
BRANCH=${{ github.head_ref }}

if [ ! -d "$DEPLOY_DIR" ]; then
git clone https://github.com/${{ github.repository }}.git "$DEPLOY_DIR"
fi

cd "$DEPLOY_DIR"
git fetch origin
git checkout "$BRANCH"
git reset --hard "origin/$BRANCH"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

git reset --hard will discard any uncommitted changes or failed deployment artifacts without verification


cd website
pnpm install --frozen-lockfile
pnpm run build
Comment on lines +51 to +52

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add error handling to prevent starting the server if build fails - consider adding set -e at the start of the script

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!


NODE_BIN=$(which node)
NEXT_BIN="$DEPLOY_DIR/website/node_modules/.bin/next"

mkdir -p "$HOME/.config/systemd/user"
printf '%s\n' \
"[Unit]" \
"Description=LogLife Preview" \
"After=network.target" \
"" \
"[Service]" \
"Type=simple" \
"WorkingDirectory=$DEPLOY_DIR/website" \
"Environment=PORT=$PORT" \
"Environment=NODE_ENV=production" \
"Environment=NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=$NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY" \
"ExecStart=$NODE_BIN $NEXT_BIN start --port $PORT" \
"Restart=on-failure" \
"" \
"[Install]" \
"WantedBy=default.target" \
> "$HOME/.config/systemd/user/$APP_NAME.service"

systemctl --user daemon-reload
systemctl --user enable "$APP_NAME"
systemctl --user restart "$APP_NAME"
Loading