Update deploy_test.yml #34
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: π Deploy Next.js App | |
on: | |
push: | |
branches: [main] | |
jobs: | |
build-and-deploy: | |
name: π Build and Deploy | |
runs-on: ubuntu-latest | |
env: | |
NODE_VERSION: 20 | |
steps: | |
- name: π Checkout Repository | |
uses: actions/checkout@v4 | |
- name: π Setup Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: ${{ env.NODE_VERSION }} | |
- name: π¦ Install and Build | |
run: | | |
yarn install | |
yarn build | |
- name: Set up SSH | |
run: | | |
mkdir -p ~/.ssh | |
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa | |
chmod 600 ~/.ssh/id_rsa | |
ssh-keyscan -H ${{ secrets.SERVER_HOST }} >> ~/.ssh/known_hosts | |
- name: Deploy via SSH | |
run: | | |
ssh -o StrictHostKeyChecking=no ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} << 'EOF' | |
set -e | |
echo "Checking disk space..." | |
df -h | |
echo "Checking if Yarn is installed..." | |
if ! command -v yarn > /dev/null; then | |
echo "Yarn is not installed. Installing Yarn directly..." | |
rm -rf ~/.yarn ~/.config/yarn | |
curl -o- -L https://yarnpkg.com/install.sh | bash | |
export PATH=$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH | |
else | |
echo "Yarn is already installed." | |
fi | |
echo "Node.js version installed:" | |
node -v | |
echo "Yarn version installed:" | |
yarn -v | |
echo "Deploying the application..." | |
cd /home/${{ secrets.SERVER_USER }}/public_html | |
echo "Pulling latest changes..." | |
git pull origin main || { echo "Git pull failed"; exit 1; } | |
echo "Cleaning up Yarn cache and old lock files..." | |
yarn cache clean | |
yarn config set registry https://registry.npmjs.org/ | |
rm -f package-lock.json | |
echo "Installing dependencies..." | |
yarn install --frozen-lockfile || (yarn cache clean && yarn install --frozen-lockfile) || { echo "Dependency installation failed"; exit 1; } | |
echo "Building the application..." | |
yarn build || { echo "Build process failed"; exit 1; } | |
echo "Starting application with PM2..." | |
npx pm2 describe nextjs-app > /dev/null && npx pm2 reload nextjs-app || npx pm2 start yarn --name "nextjs-app" -- run start | |
echo "Saving PM2 process list..." | |
npx pm2 save | |
echo "Deployment completed successfully!" | |
EOF |