This repository has been archived by the owner on Dec 10, 2024. It is now read-only.
generated from IgrowkerTraining/template-express-ts
-
Notifications
You must be signed in to change notification settings - Fork 9
128 lines (104 loc) · 3.52 KB
/
CI-CD-Pipeline.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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