Skip to content

Commit eb2dcab

Browse files
Merge pull request #216 from mju-likelion/feature/prod-deploy-#140
Feature/#140 prod 배포 작업
2 parents e0ca5cf + d631526 commit eb2dcab

File tree

3 files changed

+113
-2
lines changed

3 files changed

+113
-2
lines changed

.github/workflows/ci-cd-develop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
mkdir -p ./src/test/resources
3838
cd ./src/test/resources
3939
touch ./application.properties
40-
echo "${{ secrets.DEV_TEST_PROPERTIES }}" > ./application-test.properties
40+
echo "${{ secrets.TEST_PROPERTIES }}" > ./application-test.properties
4141
shell: bash
4242

4343
# 2-1. Gradle Test 를 실행한다

.github/workflows/ci-cd-prod.yml

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# This workflow uses actions that are not certified by GitHub.
2+
# They are provided by a third-party and are governed by
3+
# separate terms of service, privacy policy, and support
4+
# documentation.
5+
# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time
6+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle
7+
8+
name: Java CI/CD with Gradle
9+
10+
on:
11+
push:
12+
branches: [ "main" ]
13+
14+
permissions:
15+
contents: read
16+
17+
jobs:
18+
# Spring Boot 애플리케이션을 빌드하여 도커허브에 푸시하는 과정
19+
build-docker-image:
20+
runs-on: ubuntu-latest
21+
steps:
22+
- uses: actions/checkout@v3
23+
24+
- name: Grant execute permission for gradlew # gradlew 파일에 실행 권한 부여
25+
run: chmod +x gradlew # gradlew 파일에 실행 권한을 부여한다.
26+
27+
# 1. Java 17 세팅
28+
- name: Set up JDK 17
29+
uses: actions/setup-java@v3
30+
with:
31+
java-version: '17'
32+
distribution: 'temurin'
33+
34+
# 2. test 용 application.properties 파일 생성
35+
- name: make application.properties for test
36+
run: |
37+
mkdir -p ./src/test/resources
38+
cd ./src/test/resources
39+
touch ./application.properties
40+
echo "${{ secrets.TEST_PROPERTIES }}" > ./application-test.properties
41+
shell: bash
42+
43+
# 2-1. Gradle Test 를 실행한다
44+
- name: Test with Gradle
45+
run: ./gradlew --info test
46+
47+
# 3. Spring Boot 애플리케이션 빌드
48+
- name: Build with Gradle
49+
uses: gradle/gradle-build-action@bd5760595778326ba7f1441bcf7e88b49de61a25 # v2.6.0
50+
with:
51+
arguments: clean bootJar -x test
52+
53+
# 4. Docker 이미지 빌드
54+
- name: docker image build
55+
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROD_DOCKERHUB_IMAGE }} .
56+
57+
# 5. DockerHub 로그인
58+
- name: docker login
59+
uses: docker/login-action@v2
60+
with:
61+
username: ${{ secrets.DOCKERHUB_USERNAME }}
62+
password: ${{ secrets.DOCKERHUB_PASSWORD }}
63+
64+
# 6. Docker Hub 이미지 푸시
65+
- name: docker Hub push
66+
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROD_DOCKERHUB_IMAGE }}
67+
68+
# Docker 이미지를 가져와서 AWS EC2 에 배포하는 과정
69+
deploy:
70+
needs: build-docker-image
71+
runs-on: ubuntu-latest
72+
steps:
73+
- name: ssh connect & production
74+
uses: appleboy/ssh-action@master
75+
with:
76+
host: ${{ secrets.PROD_HOSTS }}
77+
username: ${{ secrets.PROD_USERNAME }}
78+
key: ${{ secrets.PROD_PASSWORD }}
79+
script: |
80+
IFS=',' read -ra HOSTS <<< "${{ secrets.PROD_HOSTS }}"
81+
for host in "${HOSTS[@]}"; do
82+
setup_application_properties() {
83+
local env=$1
84+
local properties_content=$2
85+
mkdir -p ~/properties/$env
86+
cd ~/properties/$env
87+
touch ./application-$env.properties
88+
echo "$properties_content" > ./application-$env.properties
89+
}
90+
91+
setup_application_properties "dev" "${{ secrets.PROD_PROPERTIES }}"
92+
sudo docker login --username ${{ secrets.DOCKERHUB_USERNAME }} --password ${{ secrets.DOCKERHUB_PASSWORD }}
93+
sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROD_DOCKERHUB_IMAGE }}
94+
sudo docker ps -q | xargs -r sudo docker stop
95+
sudo docker ps -aq | xargs -r sudo docker rm
96+
sudo docker run --name ${{ secrets.PROD_DOCKERHUB_IMAGE }} -d -p 8080:8080 -v ~/logs:/logs -v ~/properties:/properties -e SPRING_PROFILES_ACTIVE=dev -e SPRING_CONFIG_LOCATION=/properties/dev/application-dev.properties ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROD_DOCKERHUB_IMAGE }}
97+
sudo docker system prune -f
98+
done
99+
100+
101+
102+
103+
104+
105+
106+
107+
108+
109+
110+
111+

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
mkdir -p ./src/test/resources
3030
cd ./src/test/resources
3131
touch ./application.properties
32-
echo "${{ secrets.DEV_TEST_PROPERTIES }}" > ./application-test.properties
32+
echo "${{ secrets.TEST_PROPERTIES }}" > ./application-test.properties
3333
shell: bash
3434

3535
# Gradle Test를 실행한다

0 commit comments

Comments
 (0)