-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
83 additions
and
12 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,90 @@ | ||
name: Jest Tests | ||
name: Deploy NextJs-Woo App | ||
|
||
on: | ||
pull_request: | ||
workflow_dispatch: | ||
push: | ||
branches: [main] | ||
branches: | ||
- main # Change this to your default branch if it's different | ||
|
||
jobs: | ||
unit-test: | ||
name: Run Tests | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Node.js | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: '20.12.2' # Specify the Node.js version you want to use | ||
|
||
- name: Install dependencies | ||
run: | | ||
yarn install --prod | ||
- name: Build Next.js app | ||
run: | | ||
yarn build | ||
- name: Prepare assets for standalone build | ||
run: | | ||
# Copy public assets and static files to the standalone directory | ||
cp -r public .next/standalone/ | ||
cp -r .next/static .next/standalone/.next/ | ||
- name: Zip the build folder | ||
run: | | ||
zip -r nextjs-app.zip .next/standalone | ||
- name: Upload zip file | ||
uses: actions/upload-artifact@v2 | ||
with: | ||
name: nextjs-app | ||
path: nextjs-app.zip | ||
|
||
deploy: | ||
runs-on: ubuntu-latest | ||
needs: build # Ensure this job runs after the build job | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: '20.11.0' | ||
- run: yarn | ||
- run: yarn test | ||
- name: Download zip file | ||
uses: actions/download-artifact@v2 | ||
with: | ||
name: nextjs-app | ||
|
||
- name: Install sshpass | ||
run: | | ||
sudo apt-get update | ||
sudo apt-get install -y sshpass | ||
- name: Deploy to server | ||
env: | ||
SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }} | ||
SERVER_IP: ${{ secrets.SERVER_IP }} | ||
USERNAME: ${{ secrets.USERNAME }} | ||
run: | | ||
# Create the SSH directory | ||
mkdir -p ~/.ssh | ||
# Add the server's host key to known hosts | ||
ssh-keyscan -H $SERVER_IP >> ~/.ssh/known_hosts | ||
# Copy the zip file to the server using SSH with password | ||
echo "$SSH_PASSWORD" | sshpass scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null nextjs-app.zip $USERNAME@$SERVER_IP:/path/on/server | ||
# Connect to the server and deploy | ||
echo "$SSH_PASSWORD" | sshpass ssh $USERNAME@$SERVER_IP << 'EOF' | ||
# Unzip the application | ||
cd /path/on/server | ||
unzip -o nextjs-app.zip | ||
# Install PM2 if not installed | ||
if ! command -v pm2 &> /dev/null | ||
then | ||
npm install -g pm2 | ||
fi | ||
# Start the application with PM2, setting NODE_PORT | ||
NODE_PORT=3000 pm2 start .next/standalone/server.js --name nextjs-app --watch | ||
pm2 save | ||
pm2 startup | ||
EOF |