-
Notifications
You must be signed in to change notification settings - Fork 20
69 lines (62 loc) · 2.57 KB
/
deploy.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
name: Deploy to EC2
on:
push:
branches:
- main
jobs:
check-changes:
runs-on: ubuntu-latest
environment: Production
outputs:
changes: ${{ steps.check_changes.outputs.changes }}
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 2
- name: Check for changes in server
id: check_changes
run: |
git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep '^apps/server/' && echo "changes=true" >> $GITHUB_OUTPUT || echo "changes=false" >> $GITHUB_OUTPUT
deploy:
runs-on: ubuntu-latest
environment: Production
needs: check-changes
if: needs.check-changes.outputs.changes == 'true'
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Build and push Docker image
run: |
docker compose build
docker tag brunoroh/bolhadev-chat:latest ${{ vars.DOCKER_REPO}}/bolhadev-chat:latest
docker tag brunoroh/bolhadev-nginx:latest ${{ vars.DOCKER_REPO}}/bolhadev-nginx:latest
docker login -u ${{ vars.DOCKER_USERNAME }} -p ${{secrets.DOCKER_TOKEN}}
docker push ${{ vars.DOCKER_REPO}}/bolhadev-chat:latest
docker push ${{ vars.DOCKER_REPO}}/bolhadev-nginx:latest
- name: SSH into EC2 instance and deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
script: |
sudo docker pull ${{ vars.DOCKER_REPO }}/bolhadev-chat:latest
sudo docker pull ${{ vars.DOCKER_REPO }}/bolhadev-nginx:latest
sudo docker stop bolhadev-nginx || true
sudo docker stop bolhadev-chat || true
sudo docker rm bolhadev-nginx || true
sudo docker rm bolhadev-chat || true
sudo docker run -d -p "127.0.0.1:4000:4000" -p "127.0.0.1:4001:4001" --name bolhadev-chat --restart unless-stopped --network internal ${{ vars.DOCKER_REPO }}/bolhadev-chat:latest
sudo docker run -d -p 80:80 -p 443:443 \
--name bolhadev-nginx \
-v /var/www/certbot:/var/www/certbot \
-v /etc/letsencrypt/ssl/fullchain.pem:/etc/nginx/ssl/fullchain.pem \
-v /etc/letsencrypt/ssl/privkey.pem:/etc/nginx/ssl/privkey.pem \
--network internal \
${{ vars.DOCKER_REPO }}/bolhadev-nginx:latest
docker image prune -f