diff --git a/.github/workflows/DOCKER-CD-STAGING.yml b/.github/workflows/DOCKER-CD-STAGING.yml new file mode 100644 index 00000000..cbe6ddde --- /dev/null +++ b/.github/workflows/DOCKER-CD-STAGING.yml @@ -0,0 +1,59 @@ +name: DOCKER-CD-STAGING + +on: + push: + branches: [ "staging" ] + +jobs: + build-and-push: + runs-on: ubuntu-latest + env: + working-directory: . + + steps: + - name: 체크아웃 + uses: actions/checkout@v3 + + - name: application.yml 생성 + run: | + mkdir -p ./src/main/resources && cd $_ + touch ./application.yml + echo "${{ secrets.YML }}" > ./application.yml + working-directory: ${{ env.working-directory }} + + - name: application-staging.yml 생성 + run: | + cd ./src/main/resources + touch ./application-staging.yml + echo "${{ secrets.PROD_STAGING }}" > ./application-staging.yml + working-directory: ${{ env.working-directory }} + + - name: Docker Hub 로그인 + uses: docker/login-action@v2 + with: + username: ${{ secrets.STAGING_DOCKER_LOGIN_USERNAME }} + password: ${{ secrets.STAGING_DOCKER_LOGIN_ACCESSTOKEN }} + + - name: Docker Image Build and Push (Native) + run: | + docker build -f Dockerfile-native-staging \ + -t ${{ secrets.STAGING_DOCKER_LOGIN_USERNAME }}/terning_kotlin:latest \ + -t ${{ secrets.STAGING_DOCKER_LOGIN_USERNAME }}/terning_kotlin:${{ github.sha }} . + + docker push ${{ secrets.STAGING_DOCKER_LOGIN_USERNAME }}/terning_kotlin --all-tags + working-directory: ${{ env.working-directory }} + + cd: + needs: build-and-push + runs-on: ubuntu-latest + + steps: + - name: Deploy to Staging Server + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.STAGING_SERVER_IP }} + username: ${{ secrets.STAGING_SERVER_USER }} + key: ${{ secrets.STAGING_SERVER_KEY }} + script: | + cd ~ + ./deploy-staging.sh ${{ github.sha }} diff --git a/Dockerfile-staging b/Dockerfile-staging new file mode 100644 index 00000000..735c1413 --- /dev/null +++ b/Dockerfile-staging @@ -0,0 +1,37 @@ +# ======================================================= +# Stage 1: Builder - 네이티브 실행 파일을 만드는 단계 +# ======================================================= +# GraalVM 공식 빌더 이미지를 사용합니다. +FROM oracle/graalvm-community:21-jdk as builder + +WORKDIR /build + +# 빌드에 필요한 소스코드 전체를 복사합니다. +COPY . . + +# Gradle을 사용하여 네이티브 실행 파일을 빌드합니다. +# 이 명령어는 시간이 다소 걸릴 수 있습니다. +RUN ./gradlew nativeCompile + + +# =============================================================== +# Stage 2: Runner - 최종 실행 이미지를 만드는 단계 +# =============================================================== +# JVM이 없는 초경량(Distroless) 이미지를 사용합니다. +FROM gcr.io/distroless/cc-debian12 + +WORKDIR /app + +# --- 중요 --- +# Builder(1단계)에서 만들어진 네이티브 실행 파일만 복사해옵니다. +COPY --from=builder /build/build/native/nativeCompile/server-kotlin . + +# 환경 변수는 동일하게 설정합니다. +ENV TZ=Asia/Seoul +ENV SPRING_PROFILES_ACTIVE=staging +ENV APP_PORT=8080 + +EXPOSE ${APP_PORT} + +# "java -jar"가 아닌, 실행 파일 자체를 실행합니다. +CMD ["./server-kotlin", "--server.port=${APP_PORT}"]