diff --git a/.github/workflows/CD.yaml b/.github/workflows/CD.yaml index 749af41..d8c5365 100644 --- a/.github/workflows/CD.yaml +++ b/.github/workflows/CD.yaml @@ -10,45 +10,28 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: Set up JDK 17 - uses: actions/setup-java@v3 - with: - java-version: '17' - distribution: 'temurin' - - - name: Gradle Caching - uses: actions/cache@v3 - with: - path: | - ~/.gradle/caches - ~/.gradle/wrapper - key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} - restore-keys: | - ${{ runner.os }}-gradle- - - - name: Grant execute permission for gradlew - run: chmod +x gradlew + - name: checkout code + uses: actions/checkout@v3 - - name: Create application.yml - run: | - cd ./src/main/resources - touch ./application.yml - echo "${{ secrets.APPLICATION }}" > ./application.yml - shell: bash + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 - - name: Access permission to gradle build - run: chmod +x gradlew - - - name: Run gradle build - run: ./gradlew build - shell: bash + - name: Login to DockerHub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} - - name: Build docker image build and push - run: | - docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} - docker build -t ${{ secrets.DOCKER_REPOSITORY }}/${{ secrets.DOCKER_IMAGE }} . - docker push ${{ secrets.DOCKER_REPOSITORY }}/${{ secrets.DOCKER_IMAGE }} + - name: Build and push + uses: docker/build-push-action@v2 + with: + context: . + file: ./Dockerfile + platforms: linux/amd64 + push: true + tags: ${{ DOCKER_REPOSITORY }}/${{ DOCKER_IMAGE }} + cache-from: type=gha # 여기서 gha 는 Guthub Actions 용 캐시를 의미합니다. + cache-to: type=gha,mode=max - name: Pull from docker hub and deploy uses: appleboy/ssh-action@master @@ -58,5 +41,6 @@ jobs: key: ${{ secrets.REMOTE_KEY }} script: | cd /Backend + git pull origin main sudo docker compose down sudo docker compose up -d \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c39301c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,49 @@ +# 빌드 이미지로 OpenJDK 17 & Gradle을 지정 +FROM gradle:8.10.2-jdk17 AS build + +# Gradle 캐시 디렉토리 설정 +ENV GRADLE_USER_HOME=/home/gradle/.gradle + +# 소스 코드를 복사할 작업 디렉토리를 생성 +WORKDIR /app + +# 필요한 파일만 복사 +COPY build.gradle settings.gradle gradlew ./ +COPY gradle ./gradle + +# 종속성 캐시를 생성 +RUN gradle dependencies --no-daemon + +# 이후 전체 소스 복사 (캐시 미스가 발생하지 않으면 종속성 설치를 재실행하지 않음) +COPY . /app + +# Gradle 빌드를 실행하여 JAR 파일 생성 +RUN gradle build --no-daemon + +# OpenJDK 17 기반으로 빌드 +FROM openjdk:17-alpine + +# 작업 디렉토리 설정 +WORKDIR /app + +# bash 설치 추가 +RUN apk add --no-cache bash + +# 루트 권한으로 변경 +USER root + +# wait-for-it 스크립트 복사 및 실행 권한 부여 +COPY wait-for-it.sh /usr/local/bin/wait-for-it +RUN chmod +x /usr/local/bin/wait-for-it + +# 빌드 이미지에서 생성된 JAR 파일을 런타임 이미지로 복사 +COPY --from=build /app/build/libs/*.jar /app/ohlottery.jar + +# 다시 기본 사용자로 변경 +USER 1000 + +# 포트 노출 +EXPOSE 8080 + +# MySQL과 Redis가 준비될 때까지 대기 후 백엔드 실행 +ENTRYPOINT ["sh", "-c", "/usr/local/bin/wait-for-it mysql:3306 -- /usr/local/bin/wait-for-it redis:6379 -- java -jar /app/ohlottery.jar"] \ No newline at end of file