-
Notifications
You must be signed in to change notification settings - Fork 0
98 lines (80 loc) · 2.98 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
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
name: Deploy Spring Boot to EC2 with Docker Compose
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up JDK 21
uses: actions/setup-java@v1
with:
java-version: '21'
- name: Set up Docker credentials
run: |
echo "DOCKER_USERNAME=${{ secrets.DOCKER_USERNAME }}" >> $GITHUB_ENV
echo "DOCKER_PASSWORD=${{ secrets.DOCKER_PASSWORD }}" >> $GITHUB_ENV
- name: Build JAR file
run: ./gradlew bootJar
- name: Build and push Docker image with JIB
run: ./gradlew jib -Pprod --image=${{ secrets.DOCKER_USERNAME }}/the_monitor:latest
deploy:
runs-on: ubuntu-latest
needs: build
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Copy nginx.conf to EC2
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ec2-user
key: ${{ secrets.EC2_PRIVATE_KEY }}
source: "./nginx.conf"
target: "/home/ec2-user/"
- name: Create .env file on EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ec2-user
key: ${{ secrets.EC2_PRIVATE_KEY }}
script: |
echo "${{ secrets.ENV }}" > /home/ec2-user/.env
- name: Copy docker-compose.yml to EC2
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ec2-user
key: ${{ secrets.EC2_PRIVATE_KEY }}
source: "./docker-compose.yml"
target: "/home/ec2-user/"
- name: Connect to EC2 and deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ec2-user
key: ${{ secrets.EC2_PRIVATE_KEY }}
script: |
# Install jq if not already installed
if ! command -v jq &> /dev/null
then
sudo yum install -y jq
fi
# Set up Docker Compose on EC2 if not installed
if ! command -v docker-compose &> /dev/null
then
sudo curl -L "https://github.com/docker/compose/releases/download/$(curl -s https://api.github.com/repos/docker/compose/releases/latest | jq -r '.tag_name')/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
fi
cd /home/ec2-user
# Remove all running containers and networks (forcefully)
docker-compose -f docker-compose.yml down --remove-orphans || true
docker rm -f $(docker ps -aq) || true
docker network prune -f || true
# Pull the latest Docker images
docker-compose -f docker-compose.yml pull
# Start containers with Docker Compose
docker-compose -f docker-compose.yml up -d