From 19897ec10fb34f86d205773b2a311ef944dae722 Mon Sep 17 00:00:00 2001 From: seheonnn Date: Fri, 26 Jan 2024 02:26:19 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=9A=20[CI/CD]:=20ci,=20cd=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EB=B6=84=EB=A6=AC=20(#45)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Kim Dae Hwi <121790935+marooo326@users.noreply.github.com> --- .github/workflows/cd_gradle.yml | 48 +++++++ .github/workflows/ci_gradle.yml | 69 ++++++++++ .github/workflows/gradle.yml | 230 ++++++++++++++++---------------- 3 files changed, 232 insertions(+), 115 deletions(-) create mode 100644 .github/workflows/cd_gradle.yml create mode 100644 .github/workflows/ci_gradle.yml diff --git a/.github/workflows/cd_gradle.yml b/.github/workflows/cd_gradle.yml new file mode 100644 index 00000000..0e483fee --- /dev/null +++ b/.github/workflows/cd_gradle.yml @@ -0,0 +1,48 @@ +name: Java CD with Gradle + +on: + push: + branches: [ "develop" ] + +jobs: + cd: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + + # DockerHub Login (2) + - name: Docker login + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + # AWS EC2 Server Connect & Docker command execution + - name: Deploy + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.HOST }} # EC2 instance public DNS + username: ec2-user + key: ${{ secrets.PRIVATE_KEY }} # pem key + script: | + sudo docker stop sponus-docker + sudo docker rm sponus-docker + sudo docker rmi ${{ secrets.DOCKER_USERNAME }}/sponus-docker + sudo docker pull ${{ secrets.DOCKER_USERNAME }}/sponus-docker + + sudo docker stop my-redis + sudo docker rm my-redis + sudo docker rmi redis + + sudo docker pull redis + sudo docker network create my-network + sudo docker run --name my-redis --network my-network -d redis + sudo docker run -e SPRING_PROFILES_ACTIVE=prod -it -d -p 8080:8080 --name sponus-docker -e TZ=Asia/Seoul --network my-network ${{ secrets.DOCKER_USERNAME }}/sponus-docker + diff --git a/.github/workflows/ci_gradle.yml b/.github/workflows/ci_gradle.yml new file mode 100644 index 00000000..d84d5dbd --- /dev/null +++ b/.github/workflows/ci_gradle.yml @@ -0,0 +1,69 @@ +name: Java CI with Gradle + +on: + pull_request: + branches: [ "develop" ] + +jobs: + ci: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + + java-version: '17' + distribution: 'temurin' + + - name: 2) Convert to prod config + run: | + cd ./src/main/resources + [ -e "application.yml" ] && rm application.yml + echo "spring:" > application.yml + echo " profiles:" >> application.yml + echo " active: prod" >> application.yml + + - name: 3) Set prod.yml + uses: microsoft/variable-substitution@v1 + with: + files: ./src/main/resources/application-prod.yml + env: + spring.datasource.url: ${{ secrets.DB_URL }} + spring.datasource.username: ${{ secrets.DB_USERNAME }} + spring.datasource.password: ${{ secrets.DB_PW }} + spring.jwt.secret: ${{ secrets.JWT_SECRET }} + spring.jwt.token.access-expiration-time: ${{ secrets.JWT_ACCESS_EXPIRATION_TIME }} + spring.jwt.token.refresh-expiration-time: ${{ secrets.JWT_REFRESH_EXPIRATION_TIME }} + spring.data.redis.host: ${{ secrets.REDIS_HOST }} + spring.data.redis.port: ${{ secrets.REDIS_PORT }} + cloud.aws.s3.bucket: ${{ secrets.BUCKET }} + cloud.aws.region.static: ${{ secrets.REGION }} + cloud.aws.credentials.accessKey: ${{ secrets.S3_ACCESS_KEY }} + cloud.aws.credentials.secretKey: ${{ secrets.S3_SECRET_KEY }} + + - name: 3) Set prod.yml - Debug + run: | + cat ./src/main/resources/application-prod.yml + + - name: Grant execute permission for gradlew and build + run: | + chmod +x ./gradlew + ./gradlew clean + ./gradlew build -x test + + # Docker image Build (1) + - name: Docker image build + run: docker build -t ${{ secrets.DOCKER_USERNAME }}/sponus-docker --pull --no-cache --platform linux/amd64 . + + # DockerHub Login (2) + - name: Docker login + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + # Docker Hub push (3) + - name: Docker Hub push + run: docker push ${{ secrets.DOCKER_USERNAME }}/sponus-docker diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index ce5c75a0..7c303a6c 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -1,115 +1,115 @@ -name: Java CI/CD with Gradle - -on: - pull_request: - branches: [ "develop" ] - push: - branches: [ "develop" ] - -jobs: - ci: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v3 - - name: Set up JDK 17 - uses: actions/setup-java@v3 - with: - - java-version: '17' - distribution: 'temurin' - - - name: 2) Convert to prod config - run: | - cd ./src/main/resources - [ -e "application.yml" ] && rm application.yml - echo "spring:" > application.yml - echo " profiles:" >> application.yml - echo " active: prod" >> application.yml - - - name: 3) Set prod.yml - uses: microsoft/variable-substitution@v1 - with: - files: ./src/main/resources/application-prod.yml - env: - spring.datasource.url: ${{ secrets.DB_URL }} - spring.datasource.username: ${{ secrets.DB_USERNAME }} - spring.datasource.password: ${{ secrets.DB_PW }} - spring.jwt.secret: ${{ secrets.JWT_SECRET }} - spring.jwt.token.access-expiration-time: ${{ secrets.JWT_ACCESS_EXPIRATION_TIME }} - spring.jwt.token.refresh-expiration-time: ${{ secrets.JWT_REFRESH_EXPIRATION_TIME }} - spring.data.redis.host: ${{ secrets.REDIS_HOST }} - spring.data.redis.port: ${{ secrets.REDIS_PORT }} - cloud.aws.s3.bucket: ${{ secrets.BUCKET }} - cloud.aws.region.static: ${{ secrets.REGION }} - cloud.aws.credentials.accessKey: ${{ secrets.S3_ACCESS_KEY }} - cloud.aws.credentials.secretKey: ${{ secrets.S3_SECRET_KEY }} - - - name: 3) Set prod.yml - Debug - run: | - cat ./src/main/resources/application-prod.yml - - - name: Grant execute permission for gradlew and build - run: | - chmod +x ./gradlew - ./gradlew clean - ./gradlew build -x test - - # Docker image Build (1) - - name: Docker image build - run: docker build -t ${{ secrets.DOCKER_USERNAME }}/sponus-docker --pull --no-cache --platform linux/amd64 . - - # DockerHub Login (2) - - name: Docker login - uses: docker/login-action@v2 - with: - username: ${{ secrets.DOCKER_USERNAME }} - password: ${{ secrets.DOCKER_PASSWORD }} - - # Docker Hub push (3) - - name: Docker Hub push - run: docker push ${{ secrets.DOCKER_USERNAME }}/sponus-docker - - cd: - runs-on: ubuntu-latest - needs: ci - - steps: - - name: Checkout - uses: actions/checkout@v3 - - - name: Set up JDK 17 - uses: actions/setup-java@v3 - with: - java-version: '17' - distribution: 'temurin' - - # DockerHub Login (2) - - name: Docker login - uses: docker/login-action@v2 - with: - username: ${{ secrets.DOCKER_USERNAME }} - password: ${{ secrets.DOCKER_PASSWORD }} - - # AWS EC2 Server Connect & Docker command execution - - name: Deploy - uses: appleboy/ssh-action@master - with: - host: ${{ secrets.HOST }} # EC2 instance public DNS - username: ec2-user - key: ${{ secrets.PRIVATE_KEY }} # pem key - script: | - sudo docker stop sponus-docker - sudo docker rm sponus-docker - sudo docker rmi ${{ secrets.DOCKER_USERNAME }}/sponus-docker - sudo docker pull ${{ secrets.DOCKER_USERNAME }}/sponus-docker - - sudo docker stop my-redis - sudo docker rm my-redis - sudo docker rmi redis - - sudo docker pull redis - sudo docker network create my-network - sudo docker run --name my-redis --network my-network -d redis - sudo docker run -e SPRING_PROFILES_ACTIVE=prod -it -d -p 8080:8080 --name sponus-docker -e TZ=Asia/Seoul --network my-network ${{ secrets.DOCKER_USERNAME }}/sponus-docker - +#name: Java CI/CD with Gradle +# +#on: +# pull_request: +# branches: [ "develop" ] +# push: +# branches: [ "develop" ] +# +#jobs: +# ci: +# runs-on: ubuntu-latest +# steps: +# - name: Checkout +# uses: actions/checkout@v3 +# - name: Set up JDK 17 +# uses: actions/setup-java@v3 +# with: +# +# java-version: '17' +# distribution: 'temurin' +# +# - name: 2) Convert to prod config +# run: | +# cd ./src/main/resources +# [ -e "application.yml" ] && rm application.yml +# echo "spring:" > application.yml +# echo " profiles:" >> application.yml +# echo " active: prod" >> application.yml +# +# - name: 3) Set prod.yml +# uses: microsoft/variable-substitution@v1 +# with: +# files: ./src/main/resources/application-prod.yml +# env: +# spring.datasource.url: ${{ secrets.DB_URL }} +# spring.datasource.username: ${{ secrets.DB_USERNAME }} +# spring.datasource.password: ${{ secrets.DB_PW }} +# spring.jwt.secret: ${{ secrets.JWT_SECRET }} +# spring.jwt.token.access-expiration-time: ${{ secrets.JWT_ACCESS_EXPIRATION_TIME }} +# spring.jwt.token.refresh-expiration-time: ${{ secrets.JWT_REFRESH_EXPIRATION_TIME }} +# spring.data.redis.host: ${{ secrets.REDIS_HOST }} +# spring.data.redis.port: ${{ secrets.REDIS_PORT }} +# cloud.aws.s3.bucket: ${{ secrets.BUCKET }} +# cloud.aws.region.static: ${{ secrets.REGION }} +# cloud.aws.credentials.accessKey: ${{ secrets.S3_ACCESS_KEY }} +# cloud.aws.credentials.secretKey: ${{ secrets.S3_SECRET_KEY }} +# +# - name: 3) Set prod.yml - Debug +# run: | +# cat ./src/main/resources/application-prod.yml +# +# - name: Grant execute permission for gradlew and build +# run: | +# chmod +x ./gradlew +# ./gradlew clean +# ./gradlew build -x test +# +# # Docker image Build (1) +# - name: Docker image build +# run: docker build -t ${{ secrets.DOCKER_USERNAME }}/sponus-docker --pull --no-cache --platform linux/amd64 . +# +# # DockerHub Login (2) +# - name: Docker login +# uses: docker/login-action@v2 +# with: +# username: ${{ secrets.DOCKER_USERNAME }} +# password: ${{ secrets.DOCKER_PASSWORD }} +# +# # Docker Hub push (3) +# - name: Docker Hub push +# run: docker push ${{ secrets.DOCKER_USERNAME }}/sponus-docker +# +# cd: +# runs-on: ubuntu-latest +# needs: ci +# +# steps: +# - name: Checkout +# uses: actions/checkout@v3 +# +# - name: Set up JDK 17 +# uses: actions/setup-java@v3 +# with: +# java-version: '17' +# distribution: 'temurin' +# +# # DockerHub Login (2) +# - name: Docker login +# uses: docker/login-action@v2 +# with: +# username: ${{ secrets.DOCKER_USERNAME }} +# password: ${{ secrets.DOCKER_PASSWORD }} +# +# # AWS EC2 Server Connect & Docker command execution +# - name: Deploy +# uses: appleboy/ssh-action@master +# with: +# host: ${{ secrets.HOST }} # EC2 instance public DNS +# username: ec2-user +# key: ${{ secrets.PRIVATE_KEY }} # pem key +# script: | +# sudo docker stop sponus-docker +# sudo docker rm sponus-docker +# sudo docker rmi ${{ secrets.DOCKER_USERNAME }}/sponus-docker +# sudo docker pull ${{ secrets.DOCKER_USERNAME }}/sponus-docker +# +# sudo docker stop my-redis +# sudo docker rm my-redis +# sudo docker rmi redis +# +# sudo docker pull redis +# sudo docker network create my-network +# sudo docker run --name my-redis --network my-network -d redis +# sudo docker run -e SPRING_PROFILES_ACTIVE=prod -it -d -p 8080:8080 --name sponus-docker -e TZ=Asia/Seoul --network my-network ${{ secrets.DOCKER_USERNAME }}/sponus-docker +#