Skip to content

Commit

Permalink
[BE] 운영 환경 추가로 인한 workflow 환경설정 추가 (#231)
Browse files Browse the repository at this point in the history
* chore: dev 환경 CI/CD 파일명 수정

* chore: prod 환경 CI/CD 파일 추가

* chore: dev workflow 환경설정 오류 수정

* chore: dev workflow 환경설정 오류 추가 수정

* chore: dev-cd 각 이름 내 postfix 추가

* chore: prod-cd 이름 내 postfix 추가

* chore: prod CD 파일 낸 ref 브랜치 수정
  • Loading branch information
ikjo39 authored Aug 14, 2024
1 parent 167bbc1 commit 0ba5cc0
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Backend CD
name: Backend Dev CD

on:
workflow_dispatch:
Expand All @@ -13,9 +13,9 @@ jobs:
outputs:
backend: ${{ steps.filter.outputs.backend }}
frontend: ${{ steps.filter.outputs.frontend }}
manual: ${{ steps.manualcheck.outputs.manual }}
manual: ${{ steps.manual-check.outputs.manual }}
steps:
- id: manualcheck
- id: manual-check
if: ${{ github.event_name == 'workflow_dispatch' }}
run: echo "manual=true" >> "$GITHUB_OUTPUT"

Expand Down Expand Up @@ -68,7 +68,7 @@ jobs:

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

# DockerHub 로그인
- name: Docker login
Expand All @@ -79,11 +79,11 @@ jobs:

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

be-depoly:
needs: be-build
runs-on: self-hosted
runs-on: [self-hosted, linux, ARM64, dev]
defaults:
run:
shell: bash
Expand All @@ -101,7 +101,7 @@ jobs:

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

# 2. 기존 컨테이너 중지
- name: docker stop container
Expand All @@ -110,12 +110,12 @@ jobs:
# 3. 도커 컨테이너 실행
- name: docker run new container
run: >-
docker run --name momo-api
docker run --name momo-api-dev
--rm -d -p 8080:8080
--volume=$HOME/security:/momo/security:ro
--volume=$HOME/logs:/momo/logs
--env SPRING_PROFILE=dev
${{ secrets.DOCKERHUB_USERNAME }}/momo-api
${{ secrets.DOCKERHUB_USERNAME }}/momo-api-dev
# 4. 미사용 이미지를 정리
- name: delete old docker image
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Backend CI
name: Backend Dev CI

on:
workflow_dispatch:
Expand All @@ -17,9 +17,9 @@ jobs:
outputs:
backend: ${{ steps.filter.outputs.backend }}
frontend: ${{ steps.filter.outputs.frontend }}
manual: ${{ steps.manualcheck.outputs.manual }}
manual: ${{ steps.manual-check.outputs.manual }}
steps:
- id: manualcheck
- id: manual-check
if: ${{ github.event_name == 'workflow_dispatch' }}
run: echo "manual=true" >> "$GITHUB_OUTPUT"

Expand Down
122 changes: 122 additions & 0 deletions .github/workflows/backend-prod-cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
name: Backend Prod CD

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

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

- uses: actions/checkout@v4 # Push 이벤트이기 때문에 checkout 해야 함
with:
ref: main
submodules: recursive
token: ${{ secrets.PAT_TOKEN }}
- uses: dorny/paths-filter@v3
if: ${{ github.event_name != 'workflow_dispatch' }}
id: filter
with:
base: 'main' # 해당 브랜치의 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: main
- 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-prod .

# 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-prod

be-depoly:
needs: be-build
runs-on: [self-hosted, linux, ARM64, prod]
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-prod

# 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-prod
--rm -d -p 8080:8080
--volume=$HOME/security:/momo/security:ro
--volume=$HOME/logs:/momo/logs
--env SPRING_PROFILE=dev
${{ secrets.DOCKERHUB_USERNAME }}/momo-api-prod
# 4. 미사용 이미지를 정리
- name: delete old docker image
run: docker system prune -f
75 changes: 75 additions & 0 deletions .github/workflows/backend-prod-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: Backend Prod CI

on:
workflow_dispatch:
pull_request:
branches: ['main']

permissions:
checks: write
pull-requests: write

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

- uses: dorny/paths-filter@v3
if: ${{ github.event_name != 'workflow_dispatch' }}
id: filter
with:
filters: |
backend:
- 'backend/**'
frontend:
- 'frontend/**'
list-files: 'csv'

be-test:
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
steps:
- uses: actions/checkout@v4
- 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: Test with Gradle
run: ./gradlew clean test

- name: Publish unit test results
uses: EnricoMi/publish-unit-test-result-action@v2
if: always()
with:
files: |
backend/build/test-results/test/TEST-*.xml
- name: Add comments to PR
uses: mikepenz/action-junit-report@v4
if: always()
with:
report_paths: |
backend/build/test-results/test/TEST-*.xml

0 comments on commit 0ba5cc0

Please sign in to comment.