Merge pull request #37 from rotirk20/development #26
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: CI/CD Pipeline | |
on: | |
push: | |
branches: | |
- main # Trigger deployment only on push to the main branch | |
pull_request: | |
branches: | |
- main # Run checks (build only) on PRs | |
jobs: | |
# --- Build Frontend --- | |
build-frontend: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Set up Node.js for frontend | |
uses: actions/setup-node@v3 | |
with: | |
node-version: "20" | |
- name: Install frontend dependencies | |
working-directory: ./frontend | |
run: npm install | |
- name: Build frontend | |
working-directory: ./frontend | |
run: npm run build | |
- name: Upload frontend artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: angular-build | |
path: ./frontend/dist/smetovi/ # Path to Angular build files | |
# --- Build Backend --- | |
build-backend: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Set up Node.js for backend | |
uses: actions/setup-node@v3 | |
with: | |
node-version: "20" | |
- name: Install backend dependencies | |
working-directory: ./backend | |
run: npm install | |
- name: Upload backend artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: backend-artifact | |
path: ./backend/ # Upload backend folder for deployment | |
# --- Deploy Frontend --- | |
deploy-frontend: | |
runs-on: ubuntu-latest | |
needs: [build-frontend, build-backend] # Ensure both build jobs complete before deployment | |
if: github.ref == 'refs/heads/main' # Only deploy if on the main branch | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Download frontend artifact | |
uses: actions/download-artifact@v4 | |
with: | |
name: angular-build | |
path: ./frontend/dist/smetovi/ # Ensure this is your Angular build folder | |
- name: Deploy frontend to cPanel via FTP | |
uses: SamKirkland/[email protected] | |
with: | |
server: ${{ secrets.FTP_SERVER }} | |
username: ${{ secrets.FTP_USERNAME }} | |
password: ${{ secrets.FTP_PASSWORD }} | |
local-dir: ./frontend/dist/smetovi/ | |
server-dir: /public_html/ | |
port: ${{ secrets.FTP_PORT }} | |
log-level: verbose | |
force: true # This will re-upload all files, even unchanged ones | |
# --- Deploy Backend to VM --- | |
deploy-backend: | |
runs-on: ubuntu-latest | |
needs: build-backend | |
if: github.ref == 'refs/heads/main' # Only deploy if on the main branch | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Download backend artifact | |
uses: actions/download-artifact@v4 | |
with: | |
name: backend-artifact | |
path: ./backend/ | |
# Adding SSH Key for the VM | |
- name: Setup SSH | |
uses: webfactory/[email protected] | |
with: | |
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | |
# Copy Backend Files to VM using scp | |
- name: Copy Backend to VM | |
run: | | |
rsync -avz -e "ssh -o StrictHostKeyChecking=no" ./backend/ --exclude 'node_modules' ${{ secrets.VM_USER }}@${{ secrets.VM_IP }}:~/smetovi/backend | |
# Restart Node.js Backend on VM | |
- name: Restart Backend on VM | |
run: | | |
ssh -o StrictHostKeyChecking=no ${{ secrets.VM_USER }}@${{ secrets.VM_IP }} << 'EOF' | |
cd ~/smetovi/backend | |
npm install | |
pm2 restart Smetovi | |
EOF |