-
Notifications
You must be signed in to change notification settings - Fork 0
91 lines (75 loc) · 2.98 KB
/
cicd-dev.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
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: Build the project
run: npm run build
- name: Run tests
env:
NODE_ENV: ${{ github.env }}
EXPRESS_PORT: ${{ secrets.EXPRESS_PORT }}
ALLOWED_ORIGINS: ${{ secrets.ALLOWED_ORIGINS }}
POSTGRES_USER: ${{ secrets.POSTGRES_USER }}
POSTGRES_HOST: ${{ secrets.POSTGRES_HOST }}
POSTGRES_DATABASE: ${{ secrets.POSTGRES_DATABASE }}
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
POSTGRES_PORT: ${{ secrets.POSTGRES_PORT }}
POSTGRES_SSL: ${{ secrets.POSTGRES_SSL }}
JWT_SECRET: ${{ secrets.JWT_SECRET }}
BCRYPT_PEPPER: ${{ secrets.BCRYPT_PEPPER }}
BCRYPT_SALT_ROUNDS: ${{ secrets.BCRYPT_SALT_ROUNDS }}
run: npm test
- name: Copy files to dist
run: |
cp package.json dist/
cp package-lock.json dist/
cp *.pem dist/
- name: Create environment file
run: |
echo "NODE_ENV=${{ github.env }}" > dist/.env
echo "EXPRESS_PORT=${{ secrets.EXPRESS_PORT }}" >> dist/.env
echo "ALLOWED_ORIGINS=${{ secrets.ALLOWED_ORIGINS }}" >> dist/.env
echo "POSTGRES_USER=${{ secrets.POSTGRES_USER }}" >> dist/.env
echo "POSTGRES_HOST=${{ secrets.POSTGRES_HOST }}" >> dist/.env
echo "POSTGRES_DATABASE=${{ secrets.POSTGRES_DATABASE }}" >> dist/.env
echo "POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}" >> dist/.env
echo "POSTGRES_PORT=${{ secrets.POSTGRES_PORT }}" >> dist/.env
echo "POSTGRES_SSL=${{ secrets.POSTGRES_SSL }}" >> dist/.env
echo "JWT_SECRET=${{ secrets.JWT_SECRET }}" >> dist/.env
echo "BCRYPT_PEPPER=${{ secrets.BCRYPT_PEPPER }}" >> dist/.env
echo "BCRYPT_SALT_ROUNDS=${{ secrets.BCRYPT_SALT_ROUNDS }}" >> dist/.env
- 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'
cd ~
pm2 stop app
rm -rf app
mkdir app
mv dist.tar.gz ./app
cd app
tar -xzvf dist.tar.gz
npm install
pm2 start npm --name app -- run start
EOF