-
Notifications
You must be signed in to change notification settings - Fork 3
102 lines (81 loc) · 3.3 KB
/
ci-cd-build.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
99
100
101
102
name: Build and Deploy
on:
push:
branches:
- release
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Set environment values badminton-api
run: |
cd ./badminton-api/src/main/resources
touch ./.env
echo "${{ secrets.ENV }}" > ./.env
shell: bash
- name: Set environment values badminton-batch
run: |
cd ./badminton-batch/src/main/resources
touch ./.env
echo "${{ secrets.ENV }}" > ./.env
shell: bash
- name: Set Gradle Wrapper Permissions
run: chmod +x ./gradlew
- name: Build with Gradle
run: ./gradlew assemble
- name: Install Docker Compose
run: |
sudo apt-get update
sudo apt-get install -y docker-compose
- name: Build and push Docker image
run: |
# Log in to Docker Hub
echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
# Build the Docker image with the 'latest' tag
docker-compose build
# Push the Docker image to Docker Hub
docker-compose push
- name: Clean up dangling Docker images locally
run: |
# Clean up any dangling images after the build
sudo docker image prune -f
- name: Test SSH connection
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.PUBLIC_DNS }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: 22
script: |
echo "Testing SSH connection"
uptime
- name: Deploy to EC2 Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.PUBLIC_DNS }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: 22
script: |
echo "Docker Hub에 로그인 하기"
echo "${{ secrets.DOCKERHUB_PASSWORD }}" | sudo docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
echo "모든 실행 중인 컨테이너 중지"
sudo docker stop $(sudo docker ps -aq) # 모든 실행 중인 컨테이너 중지
echo "모든 컨테이너 및 네트워크 삭제"
sudo docker rm $(sudo docker ps -aq) # 모든 컨테이너 삭제
sudo docker network prune -f # 모든 브리지 네트워크 삭제 (주의: 사용 중인 네트워크도 삭제됨)
echo "모든 이미지 삭제"
sudo docker rmi $(sudo docker images -q) # 모든 이미지 삭제
echo "최신 Docker 이미지 Pull"
sudo docker-compose pull # Docker Compose 파일에 정의된 이미지 pull
echo "새로운 컨테이너 실행"
sudo docker-compose up -d # 모든 서비스를 백그라운드에서 실행
echo "서버에서 오래된 Docker 이미지 정리"
sudo docker image prune -f # 불필요한 이미지 정리