diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 00000000..2b0b2016 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -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 + +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" + + cd website + pnpm install --frozen-lockfile + pnpm run build + + 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"