Skip to content

Commit

Permalink
fix: test 환경은 test profile로 작동하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
LJH098 committed Nov 2, 2024
1 parent e243246 commit 19d6aef
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 73 deletions.
139 changes: 66 additions & 73 deletions .github/workflows/prod-deploy-container.yml
Original file line number Diff line number Diff line change
@@ -1,89 +1,82 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle

name: Java CI with Gradle
name: Deploy to EC2 on develop

on:
push:
branches: [ "develop" ]
branches: [ "deploy-#30" ]
pull_request:
branches: [ "develop" ]
branches: [ "deploy-#30" ]

jobs:
build-docker-image:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read

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

# Configure Gradle for optimal use in GitHub Actions, including caching of downloaded dependencies.
# See: https://github.com/gradle/actions/blob/main/setup-gradle/README.md
- name: Setup Gradle
uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0

- name: Build with Gradle Wrapper
run: sudo ./gradlew build

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

# Docker Login
- name: docker login
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

# Docker Hub로 push
- name: docker hub push
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}

# ec2에서 docker image pull
run-docker-image-on-ec2:
needs: build-docker-image
runs-on: self-hosted
# 소스 코드 체크아웃
- uses: actions/checkout@v4

# JDK 설정
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'

# Gradle 설정
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4
with:
gradle-version: '7.3'

# Gradle Wrapper 실행 권한 추가
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew

# Gradle 빌드
- name: Build with Gradle Wrapper
run: ./gradlew build

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

# Docker Hub에 로그인
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

# Docker 이미지 푸시
- name: Push Docker image to Docker Hub
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}

deploy-to-ec2:
needs: build-and-push-image
runs-on: ubuntu-latest


steps:
- name: docker stop container
run: |
if [ $(sudo docker ps -a -q -f name=${{ secrets.DOCKERHUB_IMAGE_NAME }}) ]; then
sudo docker stop ${{ secrets.DOCKERHUB_IMAGE_NAME }}
fi
- name: docker delete image
run: sudo docker rmi ${{ secrets.DOCKERHUB_IMAGE_NAME }})

- name: docker pull
run: sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}


- name: docker run new container
run: sudo docker run --rm -it -d -p 80:8080 --name ${{ secrets.DOCKERHUB_IMAGE_NAME }} ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}


# NOTE: The Gradle Wrapper is the default and recommended way to run Gradle (https://docs.gradle.org/current/userguide/gradle_wrapper.html).
# If your project does not have the Gradle Wrapper configured, you can use the following configuration to run Gradle with a specified version.
#
# - name: Setup Gradle
# uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0
# with:
# gradle-version: '8.9'
#
# - name: Build with Gradle 8.9
# run: gradle build
# SSH 비밀 키 설정
- name: Install SSH Key
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}

# EC2에 접속하여 Docker Compose 실행
- name: Deploy on EC2
env:
EC2_HOST: ${{ secrets.EC2_HOST }}
EC2_USER: ${{ secrets.EC2_USER }}
run: |
ssh -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST << EOF
# Docker Compose로 컨테이너 중지 및 업데이트
cd /path/to/your/docker-compose/directory # docker-compose.yml 파일 위치로 이동
docker-compose down # 기존 컨테이너 중지
docker-compose pull # 최신 이미지 가져오기
docker-compose up -d # 컨테이너 다시 실행
EOF
dependency-submission:

Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
implementation 'org.springdoc:springdoc-openapi-ui:1.6.12'
runtimeOnly 'com.h2database:h2'
}

tasks.named('test') {
Expand Down
12 changes: 12 additions & 0 deletions src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# src/main/resources/application-test.yml
spring:
h2:
console:
enabled: true
path: /h2-console

datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:db;MODE=MYSQL
username: sa
password:
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@ActiveProfiles("test")
@SpringBootTest
class DanguApplicationTests {

Expand Down

0 comments on commit 19d6aef

Please sign in to comment.