Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Merge pull request #94 from mauro8778/mauro2 #27

Merge pull request #94 from mauro8778/mauro2

Merge pull request #94 from mauro8778/mauro2 #27

name: CI/CD Pipeline
on:
push:
branches:
- develop
- main
workflow_dispatch:
inputs:
environment:
description: "Choose deployment environment"
required: true
type: choice
options:
- development
- production
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Install Dependencies
run: npm install
- name: Build Application
run: npm run build
build-and-push-docker:
runs-on: ubuntu-latest
needs: build-and-test
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and Push Docker Images
uses: docker/build-push-action@v6
with:
context: ./
push: true
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/pawsome-node:backend
${{ secrets.DOCKERHUB_USERNAME }}/pawsome-node:${{ github.sha }}
deploy-to-render:
runs-on: ubuntu-latest
needs: build-and-test
if: ${{ github.ref_name == 'develop' || github.event.inputs.environment == 'development' }}
steps:
- name: Trigger Render Deployment
uses: fjogeleit/http-request-action@v1
with:
url: ${{ secrets.RENDER_DEPLOY_DEV_HOOK }}
method: GET
deploy-to-aws:
runs-on: ubuntu-latest
needs: build-and-test
if: ${{ github.ref_name == 'main' || github.event.inputs.environment == 'production' }}
steps:
- name: Checkout Backend Code
uses: actions/checkout@v3
- name: Set up SSH key for Public Instance
run: |
mkdir -p ~/.ssh
echo "${{ secrets.AWS_SSH_KEY }}" > ~/.ssh/pawsome-front.pem
chmod 600 ~/.ssh/pawsome-front.pem
- name: Set up SSH key for Private Instance
run: |
echo "${{ secrets.PRIVATE_INSTANCE_KEY }}" > ~/.ssh/private-instance.pem
chmod 600 ~/.ssh/private-instance.pem
- name: Deploy Backend to AWS EC2
env:
URI: ${{ secrets.URI }}
PORT: ${{ secrets.PORT }}
MAIL_HOST: ${{ secrets.MAIL_HOST }}
MAIL_PORT: ${{ secrets.MAIL_PORT }}
MAIL_USER: ${{ secrets.MAIL_USER }}
MAIL_PASS: ${{ secrets.MAIL_PASS }}
JWT_SECRET: ${{ secrets.JWT_SECRET }}
run: |
ssh -o StrictHostKeyChecking=no -i ~/.ssh/pawsome-front.pem ec2-user@${{ secrets.EC2_PUBLIC_IP }} << 'EOF'
ssh -o StrictHostKeyChecking=no -i ~/.ssh/private-instance.pem ec2-user@${{ secrets.EC2_PRIVATE_IP }} << 'INNER_SSH'
cd /home/ec2-user
if [ ! -d "i004-devops" ]; then
git clone -b pawsome https://github.com/igrowker/i004-devops.git || true
fi
cd /Pawsome
docker-compose pull
export MONGODB_URI=${MONGODB_URI}
export PORT=${PORT}
export MAIL_HOST=${MAIL_HOST}
export MAIL_PORT=${MAIL_PORT}
export MAIL_USER=${MAIL_USER}
export MAIL_PASS=${MAIL_PASS}
export JWT_SECRET=${JWT_SECRET}
docker-compose up -d --build
INNER_SSH
EOF