-
Notifications
You must be signed in to change notification settings - Fork 0
83 lines (69 loc) · 2.76 KB
/
main.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
name: 22s CI/CD pipeline
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
env:
DB_URL: ${{ secrets.DB_URL }}
DB_USERNAME: ${{ secrets.DB_USERNAME }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
JWT_SECRET_KEY: ${{ secrets.JWT_SECRET_KEY }}
REDIS_URL: ${{ secrets.REDIS_URL }}
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
# Gradle 캐시 설정
- name: Cache Gradle packages
uses: actions/cache@v3
with:
path: ~/.gradle/caches
key: gradle-${{ runner.os }}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
gradle-${{ runner.os }}-
- name: Build with Gradle Wrapper
run: ./gradlew build
# Docker 빌드 및 Docker Hub에 푸시
- name: Docker build & push to Docker Hub
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -t ${{ secrets.DOCKER_USERNAME }}/docker-test:latest .
docker push ${{ secrets.DOCKER_USERNAME }}/docker-test:latest
# EC2에 배포
- name: Docker image pull & deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_PRIVATE_KEY }}
script: |
# 최신 이미지 Pull
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/docker-test:latest
# 기존 컨테이너 중지 및 삭제
if [ "$(sudo docker ps -q -f name=docker-test)" ]; then
sudo docker stop docker-test
sudo docker rm docker-test
fi
# 환경 변수 파일 생성
echo "DB_URL=${{ secrets.DB_URL }}" > ~/.env
echo "DB_USERNAME=${{ secrets.DB_USERNAME }}" >> ~/.env
echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> ~/.env
echo "JWT_SECRET_KEY=${{ secrets.JWT_SECRET_KEY }}" >> ~/.env
echo "REDIS_URL=${{ secrets.REDIS_URL }}" >> ~/.env
# 새 컨테이너 실행 및 환경 변수 전달
sudo docker run -d --log-driver=syslog --name docker-test -p 8080:8080 \
--env-file ~/.env \
${{ secrets.DOCKER_USERNAME }}/docker-test:latest
# 실행 중인 컨테이너 확인
sudo docker ps -a | grep "docker-test"
# 사용하지 않는 이미지 및 컨테이너 정리
sudo docker image prune -a -f