Merge pull request #91 from brandonnorsworthy/updates #35
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: cicd-dev | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
cicd-dev: | |
runs-on: ubuntu-latest | |
environment: dev | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set up Node.js | |
uses: actions/setup-node@v4 | |
- name: Install dependencies | |
run: npm install | |
- name: Create environment file | |
run: | | |
echo "NODE_ENV=dev" > .env | |
echo "VITE_API_URL=${{ secrets.VITE_API_URL }}" >> .env | |
- name: Build the project | |
run: npm run build | |
# - name: Run tests | |
# env: | |
# NODE_ENV: dev | |
# API_URL: ${{ secrets.VITE_API_URL }} | |
# run: npm test | |
- name: Create tarball | |
run: | | |
tar -czvf dist.tar.gz -C dist . | |
- name: Setup SSH | |
run: | | |
mkdir -p ~/.ssh/ | |
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa | |
chmod 600 ~/.ssh/id_rsa | |
ssh-keyscan -H ${{ secrets.EC2_IP }} >> ~/.ssh/known_hosts | |
- name: Deploy to EC2 | |
run: | | |
scp -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ./dist.tar.gz ${{ secrets.EC2_USER }}@${{ secrets.EC2_IP }}:/home/ec2-user/ | |
ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ${{ secrets.EC2_USER }}@${{ secrets.EC2_IP }} << 'EOF' | |
set -e | |
# Stop the running PM2 app | |
pm2 stop app | |
# Navigate to the home directory and prepare the deployment | |
cd /home/ec2-user | |
sudo rm -rf /var/www/vite-app | |
sudo mkdir -p /var/www/vite-app | |
# Move and extract the new build | |
sudo mv dist.tar.gz /var/www/vite-app/ | |
cd /var/www/vite-app | |
sudo tar -xzvf dist.tar.gz | |
# Set the correct permissions | |
sudo chown -R nginx:nginx /var/www/vite-app | |
# Restart Nginx to serve the new build | |
sudo systemctl restart nginx | |
# Start the PM2 app with the updated content | |
pm2 start --name app "npx serve -s -l 3000" | |
EOF |