-
Notifications
You must be signed in to change notification settings - Fork 0
74 lines (63 loc) · 1.94 KB
/
deploy-ec2.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
name: Update and Deploy Node.js App as Container
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
update_repository:
runs-on: ubuntu-latest
services:
docker:
image: docker:latest
options: --privileged
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
- name: Install npm dependencies
run: npm ci
- name: Build Docker image
run: docker build -t node-app:latest .
- name: Verify Docker image
run: docker images
deploy_to_ec2:
runs-on: ubuntu-latest
needs: update_repository
steps:
- name: Deploy to EC2
uses: easingthemes/ssh-deploy@main
env:
SSH_PRIVATE_KEY: ${{ secrets.EC2_SSH_KEY }}
REMOTE_HOST: ${{ secrets.HOST_DNS }}
REMOTE_USER: ${{ secrets.USERNAME }}
TARGET: ~/my-node-app
SOURCE: ./
- name: Build Docker image on EC2
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST_DNS }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
port: 22
script: |
cd ~/my-node-app
sudo docker build -t node-app:latest .
- name: Run Docker container on EC2
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST_DNS }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
port: 22
script: |
# Stop and remove existing container named node-app if it exists
if [ "$(sudo docker ps -q -f name=node-app)" ]; then
sudo docker stop node-app
sudo docker rm node-app
fi
# Run the newly built Docker image
sudo docker run -d -p 3000:3000 --name node-app node-app:latest