From 03a288e8dcc37b4e4aad408c73e14b7fac64c26a Mon Sep 17 00:00:00 2001 From: gimhanul Date: Mon, 31 Oct 2022 19:13:45 +0900 Subject: [PATCH] SET :: CD (#1) --- .github/workflows/CD.yml | 89 ++++++++++++++++++++++++++++++++++++++++ Dockerfile | 11 ++--- 2 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/CD.yml diff --git a/.github/workflows/CD.yml b/.github/workflows/CD.yml new file mode 100644 index 00000000..67a9dd0b --- /dev/null +++ b/.github/workflows/CD.yml @@ -0,0 +1,89 @@ +name: Java CD with Gradle + +on: + push: + branches: [ main, deploy/cd ] + pull_request: + branches: [ main ] + +jobs: + CD: + runs-on: ubuntu-latest + steps: + ## jdk setting + - uses: actions/checkout@v3 + - name: Set up JDK 11 + uses: actions/setup-java@v3 + with: + java-version: '11' + distribution: 'temurin' # https://github.com/actions/setup-java + + ## gradle caching + - 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- + + ## gradle build + - name: Build with Gradle + run: ./gradlew build -x test + + ## text application build + - name: Test with Gradle + run: ./gradlew test + + ## docker build & push to production + - name: Docker build & push to prod + run: | + docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} + docker build -t ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPO }} . + docker push ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPO }} + + ## deploy to production + - name: Deploy to prod + uses: appleboy/ssh-action@master + id: deploy-prod + with: + host: ${{ secrets.HOST_PROD }} + username: ec2-user + key: ${{ secrets.PRIVATE_KEY }} + envs: GITHUB_SHA + script: | + sudo docker rm -f $(docker ps -qa) + sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPO }} + + touch .env + echo DB_PORT=${{ secrets.DB_PORT }} >> .env + echo DB_NAME=${{ secrets.DB_NAME }} >> .env + echo DB_USERNAME=${{ secrets.DB_USERNAME }} >> .env + echo DB_PASSWORD=${{ secrets.DB_PASSWORD }} >> .env + echo DB_ROOT_PASSWORD=${{ secrets.DB_ROOT_PASSWORD }} >> .env + echo JWT_SECRET=${{ secrets.JWT_SECRET }} >> .env + echo REDIS_HOST=${{ secrets.REDIS_HOST }} >> .env + echo REDIS_PORT=${{ secrets.REDIS_PORT }} >> .env + echo SOCKET_PORT=${{ secrets.SOCKET_PORT }} >> .env + cat .env + + docker-compose up -d + docker image prune -f + + ## time + current-time: + needs: CD + runs-on: ubuntu-latest + steps: + - name: Get Current Time + uses: 1466587594/get-current-time@v2 + id: current-time + with: + format: YYYY-MM-DDTHH:mm:ss + utcOffset: "+09:00" # 기준이 UTC이기 때문에 한국시간인 KST를 맞추기 위해 +9시간 추가 + + - name: Print Current Time + run: echo "Current Time=${{steps.current-time.outputs.formattedTime}}" # current-time 에서 지정한 포맷대로 현재 시간 출력 + shell: bash \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 1c2ded11..4f8e869e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,5 @@ FROM openjdk:11-jdk -FROM openjdk:11-jdk AS builder -COPY ./ ./ -RUN ./gradlew clean -RUN chmod +x ./gradlew -RUN ./gradlew bootJAR - -FROM openjdk:11-jdk -COPY --from=builder build/libs/*.jar app.jar +ARG JAR_FILE=build/libs/*.jar +COPY ${JAR_FILE} app.jar +ENV TZ=Asia/Seoul ENTRYPOINT ["java","-jar","-Dspring.profiles.active=prod","/app.jar"] \ No newline at end of file