Merge pull request #37 from eksqtr/development #36
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: Deploying to Production (Google Cloud Platform VM) | |
on: | |
push: | |
branches: | |
- master | |
pull_request: | |
branches: | |
- development | |
types: | |
- closed | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
if: github.event_name == 'push' || (github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'master') | |
steps: | |
- name: Checkout source code | |
uses: actions/checkout@v3 | |
- name: Setting up NodeJS Environment | |
uses: actions/setup-node@v3 | |
with: | |
node-version: '18.17.0' | |
- name: Install project production dependencies | |
run: npm ci production | |
- name: Build the project for production environment | |
run: npm run build | |
- name: Configuring Directory Permissions of the server | |
env: | |
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
VM_USERNAME: ${{ secrets.VM_USERNAME }} | |
VM_IP: ${{ secrets.VM_IP }} | |
run: | | |
echo "$SSH_PRIVATE_KEY" > key.pem | |
chmod 600 key.pem | |
ssh -i key.pem -o StrictHostKeyChecking=no $VM_USERNAME@$VM_IP "sudo mkdir -p /var/www/website-portfolio/.next" | |
# ssh -i key.pem -o StrictHostKeyChecking=no $VM_USERNAME@$VM_IP "sudo mkdir -p /var/www/website-portfolio/public" | |
ssh -i key.pem -o StrictHostKeyChecking=no $VM_USERNAME@$VM_IP "sudo chown -R $VM_USERNAME:$VM_USERNAME /var/www/website-portfolio" | |
- name: Sync files to server and Configure nginx | |
env: | |
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
VM_USERNAME: ${{ secrets.VM_USERNAME }} | |
VM_IP: ${{ secrets.VM_IP }} | |
run: | | |
echo "$SSH_PRIVATE_KEY" > key.pem | |
chmod 600 key.pem | |
#echo "deleting /.next/ directory" | |
# Remove the .next directory before syncing files | |
#ssh -i key.pem -o StrictHostKeyChecking=no $VM_USERNAME@$VM_IP "sudo rm -rf /var/www/website-portfolio" | |
#echo "deletion success." | |
# Creating website-portfolio directory | |
rsync -avz -e "ssh -i key.pem -o StrictHostKeyChecking=no" --delete .next node_modules package.json package-lock.json $VM_USERNAME@$VM_IP:/var/www/website-portfolio/ | |
# Deploy the Nginx configuration file to a temporary location | |
rsync -avz -e "ssh -i key.pem -o StrictHostKeyChecking=no" deploy/nginx.conf $VM_USERNAME@$VM_IP:/tmp/nginx.conf | |
# Update Nginx configuration and reload Nginx | |
ssh -i key.pem -o StrictHostKeyChecking=no $VM_USERNAME@$VM_IP << 'EOF' | |
# Move the new configuration file into place and set up symbolic link | |
sudo mv /tmp/nginx.conf /etc/nginx/sites-available/website-portfolio | |
sudo ln -sf /etc/nginx/sites-available/website-portfolio /etc/nginx/sites-enabled/ | |
# Test the Nginx configuration and reload Nginx conf | |
sudo nginx -t | |
sudo nginx -s reload | |
EOF | |
- name: Configure and Start application | |
env: | |
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
VM_USERNAME: ${{ secrets.VM_USERNAME }} | |
VM_IP: ${{ secrets.VM_IP }} | |
run: | | |
echo "$SSH_PRIVATE_KEY" > key.pem | |
chmod 600 key.pem | |
ssh -i key.pem -o StrictHostKeyChecking=no $VM_USERNAME@$VM_IP << EOF | |
cd /var/www/website-portfolio | |
# install libraries (Disabled) | |
npm install | |
# Build the project (Disabled) | |
# npm run build | |
# Ensure PM2 is installed | |
if ! command -v pm2 &> /dev/null | |
then | |
echo "PM2 not found, installing..." | |
sudo npm install -g pm2 | |
else | |
echo "PM2 is already installed, continue;" | |
fi | |
# Delete existing PM2 process if it exists | |
if pm2 list | grep -q "website-portfolio" | |
then | |
echo "Deleting existing PM2 process..." | |
pm2 delete website-portfolio | |
else | |
echo "PM2 process not found, continue;" | |
fi | |
# Start the Next.js application with PM2 | |
pm2 start npm --name "website-portfolio" -- start || true | |
# Start the Next.js application with PM2 | |
# pm2 start /var/www/website-portfolio/standalone/server.js --name "website-portfolio" | |
# else | |
# echo "Error: Script not found: /var/www/website-portfolio/standalone/server.js" | |
# exit 1 | |
# fiif [ -f /var/www/website-portfolio/standalone/server.js ]; then | |
# Save the PM2 process list | |
pm2 save | |
EOF | |
shell: bash |