From d1a38cc6854407e1f262133ee70dce8ad1fa6d9c Mon Sep 17 00:00:00 2001 From: seheonnn Date: Thu, 18 Jan 2024 15:32:45 +0900 Subject: [PATCH 1/9] =?UTF-8?q?=F0=9F=99=88=20chore:=20gitignore=20&=20yml?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ src/main/resources/application-prod.yml | 29 +++++++++++++++++++++++++ src/main/resources/application.yml | 29 ++----------------------- 3 files changed, 33 insertions(+), 27 deletions(-) create mode 100644 src/main/resources/application-prod.yml diff --git a/.gitignore b/.gitignore index 28be781b..b2d6da14 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ # Created by https://www.toptal.com/developers/gitignore/api/java,gradle # Edit at https://www.toptal.com/developers/gitignore?templates=java,gradle +*-local.yml + # Mac os *.Ds_Store diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml new file mode 100644 index 00000000..627a6dcf --- /dev/null +++ b/src/main/resources/application-prod.yml @@ -0,0 +1,29 @@ +server: + port: 8080 + +spring: + datasource: + driver-class-name: org.postgresql.Driver + url: ${DB_URL} + username: ${DB_USERNAME} + password: ${DB_PW} + + jpa: + database: postgresql + hibernate: + ddl-auto: update + open-in-view: false + show-sql: true + generate-ddl: true + + jwt: + secret: ${JWT_SECRET} + token: + access-expiration-time: ${JWT_ACCESS_EXPIRATION_TIME} + refresh-expiration-time: ${JWT_REFRESH_EXPIRATION_TIME} + + data: + redis: + host: ${REDIS_HOST} + port: ${REDIS_PORT} + diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 06a4e746..d74c444c 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,28 +1,3 @@ -server: - port: 8080 - spring: - datasource: - driver-class-name: org.postgresql.Driver - url: jdbc:postgresql://localhost:5432/test - username: root - password: root - - jpa: - database: postgresql - hibernate: - ddl-auto: update - open-in-view: false - show-sql: true - generate-ddl: true - - jwt: - secret: "dkqorhvmekgkwlaksdltlrksdpanjfajrdmausdksehlrpTwlckawk" - token: - access-expiration-time: 43200 - refresh-expiration-time: 604800 - - data: - redis: - host: localhost - port: 6379 + profiles: + active: local From d72fec1c2ca4ebddb477034fa80101bfb072b983 Mon Sep 17 00:00:00 2001 From: seheonnn Date: Thu, 18 Jan 2024 15:47:23 +0900 Subject: [PATCH 2/9] =?UTF-8?q?=F0=9F=92=9A=20[CI/CD]=20Actions=20?= =?UTF-8?q?=EC=8A=A4=ED=81=AC=EB=A6=BD=ED=8A=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/gradle.yml | 55 +++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index af2b51c0..19c9daa9 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -19,11 +19,33 @@ jobs: java-version: '17' distribution: 'temurin' - - name: Make application.yml + - name: 2) Convert to prod config run: | cd ./src/main/resources - touch ./application.yml - echo "${{ secrets.APPLICATION_YML }}" > ./application.yml + [ -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 }} + + - name: Grant execute permission for gradlew and build + run: | + chmod +x ./gradlew + ./gradlew clean + ./gradlew build -x test cd: runs-on: ubuntu-latest @@ -68,16 +90,17 @@ jobs: 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 -it -d -p 8080:8080 --name sponus-docker -e TZ=Asia/Seoul --network my-network ${{ secrets.DOCKER_USERNAME }}/sponus-docker + 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 + From 627f0ea70549ab7db5e2a514a8c4d2bcdb37e79e Mon Sep 17 00:00:00 2001 From: seheonnn Date: Thu, 18 Jan 2024 16:57:35 +0900 Subject: [PATCH 3/9] =?UTF-8?q?=F0=9F=99=88=20chore:=20yml=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application-prod.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 627a6dcf..e16acb11 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -4,9 +4,9 @@ server: spring: datasource: driver-class-name: org.postgresql.Driver - url: ${DB_URL} - username: ${DB_USERNAME} - password: ${DB_PW} + url: ${url} + username: ${username} + password: ${password} jpa: database: postgresql From 9accbd9b073ae111014e2454bced29e237918943 Mon Sep 17 00:00:00 2001 From: seheonnn Date: Thu, 18 Jan 2024 21:23:08 +0900 Subject: [PATCH 4/9] =?UTF-8?q?=F0=9F=99=88=20chore:=20yml=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application-prod.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index e16acb11..627a6dcf 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -4,9 +4,9 @@ server: spring: datasource: driver-class-name: org.postgresql.Driver - url: ${url} - username: ${username} - password: ${password} + url: ${DB_URL} + username: ${DB_USERNAME} + password: ${DB_PW} jpa: database: postgresql From 4d3a50ebd5c3004378f3ee93c8f4e919bc97dcf7 Mon Sep 17 00:00:00 2001 From: seheonnn Date: Thu, 18 Jan 2024 21:28:27 +0900 Subject: [PATCH 5/9] =?UTF-8?q?=F0=9F=92=9A=20[CI/CD]=20Actions=20?= =?UTF-8?q?=EC=8A=A4=ED=81=AC=EB=A6=BD=ED=8A=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/gradle.yml | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 19c9daa9..9f7a26cf 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -27,19 +27,29 @@ jobs: 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 }} - 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 }} + run: | + sed -i "s|{{DB_URL}}|${{ secrets.DB_URL }}|" ./src/main/resources/application-prod.yml + sed -i "s|{{DB_USERNAME}}|${{ secrets.DB_USERNAME }}|" ./src/main/resources/application-prod.yml + sed -i "s|{{DB_PW}}|${{ secrets.DB_PW }}|" ./src/main/resources/application-prod.yml + sed -i "s|{{JWT_SECRET}}|${{ secrets.JWT_SECRET }}|" ./src/main/resources/application-prod.yml + sed -i "s|{{JWT_ACCESS_EXPIRATION_TIME}}|${{ secrets.JWT_ACCESS_EXPIRATION_TIME }}|" ./src/main/resources/application-prod.yml + sed -i "s|{{JWT_REFRESH_EXPIRATION_TIME}}|${{ secrets.JWT_REFRESH_EXPIRATION_TIME }}|" ./src/main/resources/application-prod.yml + sed -i "s|{{REDIS_HOST}}|${{ secrets.REDIS_HOST }}|" ./src/main/resources/application-prod.yml + sed -i "s|{{REDIS_PORT}}|${{ secrets.REDIS_PORT }}|" ./src/main/resources/application-prod.yml - name: Grant execute permission for gradlew and build run: | From c59c24ef22573d5abe34f4b47ea79f3566dfcd4a Mon Sep 17 00:00:00 2001 From: seheonnn Date: Thu, 18 Jan 2024 21:33:28 +0900 Subject: [PATCH 6/9] =?UTF-8?q?=F0=9F=92=9A=20[CI/CD]=20Actions=20?= =?UTF-8?q?=EC=8A=A4=ED=81=AC=EB=A6=BD=ED=8A=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/gradle.yml | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 9f7a26cf..19c9daa9 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -27,29 +27,19 @@ jobs: 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 }} - name: 3) Set prod.yml - run: | - sed -i "s|{{DB_URL}}|${{ secrets.DB_URL }}|" ./src/main/resources/application-prod.yml - sed -i "s|{{DB_USERNAME}}|${{ secrets.DB_USERNAME }}|" ./src/main/resources/application-prod.yml - sed -i "s|{{DB_PW}}|${{ secrets.DB_PW }}|" ./src/main/resources/application-prod.yml - sed -i "s|{{JWT_SECRET}}|${{ secrets.JWT_SECRET }}|" ./src/main/resources/application-prod.yml - sed -i "s|{{JWT_ACCESS_EXPIRATION_TIME}}|${{ secrets.JWT_ACCESS_EXPIRATION_TIME }}|" ./src/main/resources/application-prod.yml - sed -i "s|{{JWT_REFRESH_EXPIRATION_TIME}}|${{ secrets.JWT_REFRESH_EXPIRATION_TIME }}|" ./src/main/resources/application-prod.yml - sed -i "s|{{REDIS_HOST}}|${{ secrets.REDIS_HOST }}|" ./src/main/resources/application-prod.yml - sed -i "s|{{REDIS_PORT}}|${{ secrets.REDIS_PORT }}|" ./src/main/resources/application-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 }} - name: Grant execute permission for gradlew and build run: | From 092b1a0136b714c378db30227e4294462a33a71b Mon Sep 17 00:00:00 2001 From: seheonnn Date: Fri, 19 Jan 2024 13:30:27 +0900 Subject: [PATCH 7/9] =?UTF-8?q?=F0=9F=92=9A=20[CI/CD]=20Actions=20?= =?UTF-8?q?=EC=8A=A4=ED=81=AC=EB=A6=BD=ED=8A=B8=20=EB=94=94=EB=B2=84?= =?UTF-8?q?=EA=B9=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/gradle.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 19c9daa9..31cc887b 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -41,6 +41,10 @@ jobs: spring.data.redis.host: ${{ secrets.REDIS_HOST }} spring.data.redis.port: ${{ secrets.REDIS_PORT }} + - 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 From 04ca140d8d1443a1ae4b6caad8026dd066385f25 Mon Sep 17 00:00:00 2001 From: seheonnn Date: Fri, 19 Jan 2024 13:57:30 +0900 Subject: [PATCH 8/9] =?UTF-8?q?=F0=9F=99=88=20chore:=20Dockerfile=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 79232681..ba192f07 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ FROM openjdk:17-jdk ARG JAR_FILE=./build/libs/*.jar COPY ${JAR_FILE} app.jar -ENTRYPOINT ["java", "-Dspring.profiles.active=docker", "-jar", "/app.jar"] +ENTRYPOINT ["java", "-Dspring.profiles.active=prod", "-jar", "/app.jar"] From ad6d2a843861b190457d6490d87043336eba1c5c Mon Sep 17 00:00:00 2001 From: seheonnn Date: Fri, 19 Jan 2024 14:14:34 +0900 Subject: [PATCH 9/9] =?UTF-8?q?=F0=9F=92=9A=20[CI/CD]=20Actions=20?= =?UTF-8?q?=EC=8A=A4=ED=81=AC=EB=A6=BD=ED=8A=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/gradle.yml | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 31cc887b..ef8ceb8b 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -51,6 +51,21 @@ jobs: ./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 @@ -65,16 +80,6 @@ jobs: java-version: '17' distribution: 'temurin' - - 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 @@ -82,10 +87,6 @@ jobs: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - # Docker Hub push (3) - - name: Docker Hub push - run: docker push ${{ secrets.DOCKER_USERNAME }}/sponus-docker - # AWS EC2 Server Connect & Docker command execution - name: Deploy uses: appleboy/ssh-action@master