Skip to content

Commit c898b9b

Browse files
authored
Merge pull request #11 from takmj00/develop
feat: CICD 구현
2 parents 1dde8c0 + 8b20395 commit c898b9b

6 files changed

Lines changed: 136 additions & 1 deletion

File tree

.dockerignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.git
2+
.idea
3+
*.iml
4+
.gradle
5+
build
6+
node_modules
7+
*.log

.github/workflows/cicd.yml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
name: ODG-Backend CI/CD v2
2+
3+
on:
4+
push:
5+
branches: [ "main", "develop" ]
6+
7+
jobs:
8+
build-and-deploy:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v3
14+
15+
- name: Login to Docker Hub
16+
uses: docker/login-action@v2
17+
with:
18+
username: ${{ secrets.DOCKERHUB_USERNAME }}
19+
password: ${{ secrets.DOCKERHUB_TOKEN }}
20+
21+
- name: Build and push Docker image
22+
uses: docker/build-push-action@v4
23+
with:
24+
context: .
25+
push: true
26+
tags: ${{ secrets.DOCKERHUB_USERNAME }}/team-b-25-1-be:latest
27+
28+
- name: Copy deployment files to EC2
29+
uses: appleboy/scp-action@master
30+
with:
31+
host: ${{ secrets.AWS_EC2_HOST }}
32+
username: ${{ secrets.AWS_EC2_USERNAME }}
33+
key: ${{ secrets.AWS_EC2_SSH_KEY }}
34+
source: "deploy/*"
35+
target: "/home/ubuntu/app"
36+
37+
- name: Deploy on EC2
38+
uses: appleboy/ssh-action@master
39+
with:
40+
host: ${{ secrets.AWS_EC2_HOST }}
41+
username: ${{ secrets.AWS_EC2_USERNAME }}
42+
key: ${{ secrets.AWS_EC2_SSH_KEY }}
43+
script: |
44+
export DOCKERHUB_USERNAME=${{ secrets.DOCKERHUB_USERNAME }}
45+
export DB_URL="${{ secrets.DB_URL }}"
46+
export DB_USERNAME=${{ secrets.DB_USERNAME }}
47+
export DB_PASSWORD=${{ secrets.DB_PASSWORD }}
48+
export GOOGLE_API_KEY=${{ secrets.GOOGLE_API_KEY }}
49+
50+
cd /home/ubuntu/app/deploy
51+
52+
cat << EOF > .env
53+
DB_URL=${{ secrets.DB_URL }}
54+
DB_USERNAME=${{ secrets.DB_USERNAME }}
55+
DB_PASSWORD=${{ secrets.DB_PASSWORD }}
56+
GOOGLE_API_KEY=${{ secrets.GOOGLE_API_KEY }}
57+
DOCKERHUB_USERNAME=${{ secrets.DOCKERHUB_USERNAME }}
58+
EOF
59+
60+
echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin
61+
62+
# docker-compose로 실행
63+
docker-compose -f docker-compose.yml pull
64+
docker-compose -f docker-compose.yml up -d --remove-orphans
65+
66+
docker image prune -af

Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Java 17 환경을 기반으로 하는 빌드 스테이지
2+
FROM openjdk:17-jdk-slim AS builder
3+
WORKDIR /workspace/app
4+
5+
# Gradle 래퍼와 소스코드 복사
6+
COPY gradlew .
7+
COPY gradle ./gradle
8+
COPY build.gradle .
9+
COPY settings.gradle .
10+
COPY src ./src
11+
12+
# 실행 권한 부여 및 Gradle 빌드
13+
RUN chmod +x ./gradlew
14+
RUN ./gradlew build -x test
15+
16+
# 실제 실행을 위한 경량 이미지 스테이지
17+
FROM openjdk:17-jdk-slim
18+
WORKDIR /app
19+
20+
# 빌드 스테이지에서 생성된 JAR 파일만 복사
21+
COPY --from=builder /workspace/app/build/libs/app.jar app.jar
22+
23+
# 8080 포트 노출
24+
EXPOSE 8080
25+
26+
# 컨테이너 시작 시 애플리케이션 실행
27+
ENTRYPOINT ["java","-jar","/app/app.jar"]

build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ dependencies {
3838
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
3939
implementation 'org.mapstruct:mapstruct:1.5.5.Final'
4040
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'
41-
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'
41+
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.6'
4242
}
4343

4444
tasks.named('test') {
4545
useJUnitPlatform()
4646
}
47+
48+
bootJar {
49+
archiveFileName = 'app.jar'
50+
}

deploy/docker-compose.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: '3.8'
2+
3+
services:
4+
app:
5+
image: ${DOCKERHUB_USERNAME}/team-b-25-1-be:latest
6+
container_name: odg-backend
7+
restart: always
8+
env_file:
9+
- ./.env
10+
11+
nginx:
12+
image: nginx:latest
13+
container_name: nginx-proxy
14+
restart: always
15+
ports:
16+
- "80:80"
17+
volumes:
18+
- ./nginx.conf:/etc/nginx/conf.d/default.conf
19+
depends_on:
20+
- app

deploy/nginx.conf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
server {
2+
listen 80;
3+
4+
location / {
5+
proxy_pass http://app:8080;
6+
proxy_set_header Host $host;
7+
proxy_set_header X-Real-IP $remote_addr;
8+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
9+
proxy_set_header X-Forwarded-Proto $scheme;
10+
}
11+
}

0 commit comments

Comments
 (0)