-
Notifications
You must be signed in to change notification settings - Fork 0
109 lines (92 loc) · 3.29 KB
/
cicd.yml
File metadata and controls
109 lines (92 loc) · 3.29 KB
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
name: cicd
on:
push:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install JDK 21
uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'temurin'
- name: Build with Gradle And Test
run: |
mkdir -p ./src/main/resources
echo ${{ secrets.APPLICATION_YML }} | base64 --decode > ./src/main/resources/application.yml
if [ -s ./src/main/resources/application.yml ]; then
echo "application.yml Create Success"
else
echo "application.yml Create Fail"
exit 1
fi
chmod +x ./gradlew
./gradlew clean build -x test
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_ACCESS_TOKEN }}
- name: Build Docker
run: docker build --platform linux/amd64 -t ${{ secrets.DOCKERHUB_USERNAME }}/saegil .
- name: Push Docker
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/saegil:latest
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Get Github Actions VM IP
id: ip
uses: haythem/public-ip@v1.3
- name: Print VM IP
run: |
echo ${{ steps.ip.outputs.ipv4 }}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_IAM_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.AWS_IAM_ACCESS_SECRET_KEY }}
aws-region: 'ap-northeast-2'
- name: Add GitHub Actions IP
run: |
aws ec2 authorize-security-group-ingress \
--group-id ${{ secrets.AWS_SECURITY_GROUP_ID }} \
--protocol tcp \
--port 22 \
--cidr ${{ steps.ip.outputs.ipv4 }}/32
# 일단 기존에 동작하던 도커 컨테이너를 중단하고 삭제해야될듯
- name: Stop Current Server
uses: appleboy/ssh-action@master
with:
username: ubuntu
host: ${{ secrets.EC2_ELASTIC_IP }}
key: ${{ secrets.EC2_SSH_KEY }}
script: |
sudo docker stop ${{ secrets.EC2_WAS }}
sudo docker rm ${{ secrets.EC2_WAS }}
sudo docker image prune -a -f
- name: Docker Compose
uses: appleboy/ssh-action@master
with:
username: ubuntu
host: ${{ secrets.EC2_ELASTIC_IP }}
key: ${{ secrets.EC2_SSH_KEY }}
script: |
sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/saegil:latest
sudo docker-compose -f docker-compose.yml up -d
# 얘는 새롭게 뜬 도커 컨테이너 헬스체크
- name: Check Deploy Server URL
uses: jtalk/url-health-check-action@v3
with:
url: https://${{ secrets.EC2_HTTPS_DOMAIN }}/env
max-attempts: 5
retry-delay: 10s
- name: Remove GitHub Actions IP
run: |
aws ec2 revoke-security-group-ingress \
--group-id ${{ secrets.AWS_SECURITY_GROUP_ID }} \
--protocol tcp \
--port 22 \
--cidr ${{ steps.ip.outputs.ipv4 }}/32