-
Notifications
You must be signed in to change notification settings - Fork 0
92 lines (75 loc) · 2.67 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
name: Java CI with Gradle
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build-docker-image:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
# 깃허브 레포지토리의 코드를 체크아웃
- uses: actions/checkout@v4
# JDK 17 설치
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
# application-prod.yml 파일 생성 및 내용 추가
- name: Make application-prod.yml
run: |
cd ./src/main/resources
touch application-prod.yml
echo "${{ secrets.PROPERTIES_PROD }}" > application-prod.yml
shell: bash
# application-oauth.yml 파일 생성 및 내용 추가
- name: Make application-auth.yml
run: |
cd ./src/main/resources
touch application-auth.yml
echo "${{ secrets.PROPERTIES_OAUTH }}" > application-auth.yml
shell: bash
# Gradle 설정
- name: Setup Gradle
uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0
# gradlew 파일에 실행 권한 부여
- name: Grant execute permission for gradlew
run: chmod +x gradlew
# Spring Boot 어플리케이션을 Gradle로 빌드
- name: Build with Gradle Wrapper
run: ./gradlew build
# Docker 이미지 빌드
- name: docker image build
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/eco .
# DockerHub 로그인 (이미지 푸시를 위해)
- name: docker login
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PW }}
# DockerHub로 이미지 푸시
- name: Docker Hub push
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/eco
# 위 과정에서 푸시한 이미지를 EC2에서 받아서 실행
run-docker-image-on-ec2:
needs: build-docker-image
runs-on: self-hosted
steps:
# DockerHub에서 이미지 pull
- name: docker pull
run : sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/eco
# 기존에 실행 중인 컨테이너 중지
- name: docker stop container
run: |
if [ $(sudo docker ps -a -q -f name=eco) ]; then
sudo docker stop eco
fi
# 오래된 Docker 이미지 삭제
- name: delete old docker image
run: sudo docker system prune -f
# 새 컨테이너 실행
- name: docker run new container
run: sudo docker run --rm -it -d -p 8092:8092 --name eco ${{ secrets.DOCKERHUB_USERNAME }}/eco