Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] 백엔드 CD 자동화 환경 구축 #67

Merged
merged 8 commits into from
Jul 25, 2024
116 changes: 116 additions & 0 deletions .github/workflows/backend-cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
name: Backend CD

on:
workflow_dispatch:
push:
branches: ['develop']

jobs:
detect-changes:
runs-on: ubuntu-latest
permissions:
pull-requests: read
outputs:
backend: ${{ steps.filter.outputs.backend }}
frontend: ${{ steps.filter.outputs.frontend }}
manual: ${{ steps.manualcheck.outputs.manual }}
steps:
- id: manualcheck
if: ${{ github.event_name == 'workflow_dispatch' }}
run: echo "manual=true" >> "$GITHUB_OUTPUT"

- uses: actions/checkout@v4 # Push 이벤트이기 때문에 checkout 해야 함
with:
ref: develop
submodules: recursive
token: ${{ secrets.PAT_TOKEN }}
- uses: dorny/paths-filter@v3
if: ${{ github.event_name != 'workflow_dispatch' }}
id: filter
with:
base: 'develop' # 해당 브랜치의 last commit과 변경점 비교
filters: |
backend:
- 'backend/**'
frontend:
- 'frontend/**'
be-build:
needs: detect-changes
if: ${{ needs.detect-changes.outputs.backend == 'true' || needs.detect-changes.outputs.manual == 'true' }}
runs-on: ubuntu-latest
defaults:
run:
shell: bash
working-directory: ./backend
permissions:
contents: read
packages: write

steps:
- uses: actions/checkout@v4
with:
ref: develop
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
cache: gradle

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3

- name: Grant gradlew execute permission
run: chmod +x ./gradlew

- name: Build with Gradle (clean)
run: ./gradlew clean bootJar

# Docker 이미지 빌드
- name: Docker image build
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/momo-api .

# DockerHub 로그인
- name: Docker login
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

# Docker Hub 이미지 푸시
- name: Docker Hub push
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/momo-api

be-depoly:
needs: be-build
runs-on: self-hosted
defaults:
run:
shell: bash
working-directory: ./

steps:
- name: checkout security submodule
uses: actions/checkout@v4
with:
repository: woowacourse-teams/2024-momo-config
token: ${{ secrets.PAT_TOKEN }}

- name: copy security config
run: mkdir -p $HOME/security; \cp -f *.yml ~/security

# 1. 최신 도커 이미지 pull
- name: docker pull
run: docker pull ${{ secrets.DOCKERHUB_USERNAME }}/momo-api

# 2. 기존 컨테이너 중지
- name: docker stop container
run: docker stop $(docker ps -q) 2>/dev/null || true

# 3. 도커 컨테이너 실행
- name: docker run new container
run: docker run --name momo-api --rm -d -p 80:8080 --volume=$HOME/security:/security:ro ${{ secrets.DOCKERHUB_USERNAME }}/momo-api

# 4. 미사용 이미지를 정리
- name: delete old docker image
run: docker system prune -f
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "backend/src/main/resources/security"]
path = backend/src/main/resources/security
url = https://github.com/woowacourse-teams/2024-momo-config
4 changes: 4 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM --platform=linux/arm64 openjdk:17-ea-11

COPY build/libs/*.jar momo-api.jar
ENTRYPOINT ["java","-jar","/momo-api.jar", "--spring.config.location=file:/security/"]
11 changes: 11 additions & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,14 @@ dependencies {
test {
useJUnitPlatform()
}

bootJar {
exclude('*.yml')
}

processResources.dependsOn('copyYML')
tasks.register('copyYML', Copy) {
from 'src/main/resources/security' // 서브 모듈 경로
include "*.yml"
into 'src/main/resources'
}
1 change: 1 addition & 0 deletions backend/src/main/resources/security
Submodule security added at d7f928