Skip to content

Commit

Permalink
merge: cd pipeline (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
kanguk01 authored Oct 24, 2024
2 parents 203ebdf + 63f38d2 commit 8e905c0
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 58 deletions.
64 changes: 64 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: CD Pipeline

on:
push:
branches:
- develop # develop ๋ธŒ๋žœ์น˜์— merge๋  ๋•Œ ํŠธ๋ฆฌ๊ฑฐ
- 'weekly/**'
pull_request:
branches:
- develop
- 'weekly/**'

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Java 21
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '21'

- name: Build with Gradle # Spring Boot๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ
run: ./gradlew build

- name: Set up Docker
uses: docker/setup-buildx-action@v2

- name: Log in to Docker Hub
run: echo "${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}" | docker login -u "${{ secrets.DOCKER_HUB_USERNAME }}" --password-stdin

- name: Set Image Tag
id: image_tag
run: echo "IMAGE_TAG=$(date +'%Y%m%d%H%M%S')" >> $GITHUB_ENV

- name: Build and Push Docker image
run: docker buildx build --push --platform linux/amd64 -t kimsongmok/splanet:${{ env.IMAGE_TAG }} .

- name: Deploy to EC2
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_SSH_KEY }}
script: |
sudo docker pull kimsongmok/splanet:${{ env.IMAGE_TAG }}
sudo docker stop splanet || true
sudo docker rm splanet || true
sudo docker run -d --name splanet \
--network splanet \
-e MYSQL_PROD_URL=${{ secrets.MYSQL_PROD_URL }} \
-e MYSQL_PROD_USER=${{ secrets.MYSQL_PROD_USER }} \
-e MYSQL_PROD_PASSWORD=${{ secrets.MYSQL_PROD_PASSWORD }} \
-e MYSQL_DATABASE=${{ secrets.MYSQL_DATABASE }} \
-e CLIENT_ID=${{ secrets.CLIENT_ID }} \
-e CLIENT_SECRET=${{ secrets.CLIENT_SECRET }} \
-e JWT_SECRET=${{ secrets.JWT_SECRET }} \
-e REDIRECT_URL=${{ secrets.REDIRECT_URL }} \
-p 80:8080 --restart unless-stopped kimsongmok/splanet:${{ env.IMAGE_TAG }}\
-v ./src/main/resources/env.properties
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,16 @@ jobs:

- name: Build with Gradle
run: ./gradlew build
env:
SPRING_PROFILES_ACTIVE: local
MYSQL_DATABASE: ${{ secrets.MYSQL_DATABASE }}
MYSQL_USER: ${{ secrets.MYSQL_USER }}
MYSQL_PASSWORD: ${{ secrets.MYSQL_PASSWORD }}

- name: Run Tests
run: ./gradlew test
env:
SPRING_PROFILES_ACTIVE: local
MYSQL_DATABASE: ${{ secrets.MYSQL_DATABASE }}
MYSQL_USER: ${{ secrets.MYSQL_USER }}
MYSQL_PASSWORD: ${{ secrets.MYSQL_PASSWORD }}
Expand Down
11 changes: 9 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ COPY build.gradle .
COPY settings.gradle .
COPY src src
RUN chmod +x ./gradlew
RUN ./gradlew bootJar

# Gradle ๋นŒ๋“œ์—์„œ ํ”„๋กœํ•„์„ ์ง€์ •ํ•˜์—ฌ ์‹คํ–‰
RUN ./gradlew bootJar -Dspring.profiles.active=${SPRING_PROFILES_ACTIVE}

# ๋Ÿฐํƒ€์ž„ ๋‹จ๊ณ„
FROM eclipse-temurin:21
COPY --from=builder build/libs/*.jar app.jar

# ๋Ÿฐํƒ€์ž„์—์„œ๋„ ๋™์ผํ•˜๊ฒŒ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์‚ฌ์šฉ
ENV SPRING_PROFILES_ACTIVE=prod


ENTRYPOINT ["java", "-jar", "/app.jar"]
VOLUME /tmp
VOLUME /tmp
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'javax.xml.bind:jaxb-api:2.3.1'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
// implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'
implementation 'io.jsonwebtoken:jjwt:0.9.1'
Expand Down
78 changes: 39 additions & 39 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
version: '3'

services:
mysql:
container_name: mysql
image: mysql:8.0
restart: always
environment:
MYSQL_USER: ${MYSQL_USER}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
volumes:
- ./splanet-db/mysql:/var/lib/mysql
ports:
- 3306:3306
networks:
- splanet

redis:
container_name: redis
image: redis
ports:
- 6379:6379
networks:
- splanet


# ๊ฐœ๋ฐœํ• ๋•Œ๋Š” ์ฃผ์„์ฒ˜๋ฆฌํ•˜์—ฌ ๋กœ์ปฌ๋กœ ๊ฐœ๋ฐœํ•ฉ๋‹ˆ๋‹ค.
# springboot:
# container_name: springboot_splanet
# build:
# context: .
# dockerfile: Dockerfile
# mysql:
# container_name: mysql
# image: mysql:8.0
# restart: always
# depends_on:
# - mysql
# ports:
# - 8080:8080
# environment:
# SPRING_DATASOURCE_URL: ${SPRING_DATASOURCE_URL}
# SPRING_DATASOURCE_USERNAME: ${SPRING_DATASOURCE_USERNAME}
# SPRING_DATASOURCE_PASSWORD: ${SPRING_DATASOURCE_PASSWORD}
# SPRING_PROFILES_ACTIVE: ${SPRING_PROFILES_ACTIVE}
# MYSQL_USER: ${MYSQL_USER}
# MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
# MYSQL_PASSWORD: ${MYSQL_PASSWORD}
# MYSQL_DATABASE: ${MYSQL_DATABASE}
# volumes:
# - ./splanet-db/mysql:/var/lib/mysql
# ports:
# - 3306:3306
# networks:
# - splanet
#
# redis:
# container_name: redis
# image: redis
# ports:
# - 6379:6379
# networks:
# - splanet


# ๊ฐœ๋ฐœํ• ๋•Œ๋Š” ์ฃผ์„์ฒ˜๋ฆฌํ•˜์—ฌ ๋กœ์ปฌ๋กœ ๊ฐœ๋ฐœํ•ฉ๋‹ˆ๋‹ค.
springboot:
container_name: springboot_splanet
build:
context: .
dockerfile: Dockerfile
restart: always
depends_on:
- mysql
ports:
- 8080:8080
environment:
SPRING_DATASOURCE_URL: ${SPRING_DATASOURCE_URL}
SPRING_DATASOURCE_USERNAME: ${SPRING_DATASOURCE_USERNAME}
SPRING_DATASOURCE_PASSWORD: ${SPRING_DATASOURCE_PASSWORD}
SPRING_PROFILES_ACTIVE: ${SPRING_PROFILES_ACTIVE}
networks:
- splanet

networks:
splanet:
driver: bridge
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.splanet.splanet.core.properties;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "jwt")
public class JwtProperties {
private String secret;
}
10 changes: 6 additions & 4 deletions src/main/java/com/splanet/splanet/jwt/JwtTokenProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.splanet.splanet.core.exception.BusinessException;
import com.splanet.splanet.core.exception.ErrorCode;
import com.splanet.splanet.core.properties.JwtProperties;
import io.jsonwebtoken.*;
import jakarta.annotation.PostConstruct;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -18,16 +19,17 @@ public class JwtTokenProvider {

private Key secretKey;

@Value("${JWT_SECRET}")
private String secret;

private static final long TOKEN_VALIDITY_IN_MILLISECONDS = 3600000; // 1์‹œ๊ฐ„
private static final long REFRESH_TOKEN_VALIDITY_IN_MILLISECONDS = 604800000; // 7์ผ
private final JwtProperties jwtProperties;

public JwtTokenProvider(JwtProperties jwtProperties) {
this.jwtProperties = jwtProperties;
}

@PostConstruct
protected void init() {
this.secretKey = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), SignatureAlgorithm.HS256.getJcaName());
this.secretKey = new SecretKeySpec(jwtProperties.getSecret().getBytes(StandardCharsets.UTF_8), SignatureAlgorithm.HS256.getJcaName());
}

public String createAccessToken(Long userId) {
Expand Down
17 changes: 17 additions & 0 deletions src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
spring:
datasource:
url: jdbc:mysql://localhost:3306/${MYSQL_DATABASE}
username: ${MYSQL_USER}
password: ${MYSQL_PASSWORD}
driver-class-name: com.mysql.cj.jdbc.Driver
data:
redis:
host: localhost
port: 6379
security:
oauth2:
redirect-url: http://localhost:5173/oauth2/redirect
client:
registration:
kakao:
redirect-uri: http://localhost:8080/login/oauth2/code/kakao
17 changes: 17 additions & 0 deletions src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
spring:
datasource:
url: jdbc:mysql://${MYSQL_PROD_URL}:3306/${MYSQL_DATABASE}
username: ${MYSQL_PROD_USER}
password: ${MYSQL_PROD_PASSWORD}
driver-class-name: com.mysql.cj.jdbc.Driver
data:
redis:
host: redis
port: 6379
security:
oauth2:
redirect-url: https://splanet.co.kr/oauth2/redirect
client:
registration:
kakao:
redirect-uri: https://splanet.co.kr/login/oauth2/code/kakao
20 changes: 8 additions & 12 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
spring:
datasource:
url: jdbc:mysql://localhost:3306/${MYSQL_DATABASE}
username: ${MYSQL_USER}
password: ${MYSQL_PASSWORD}
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
Expand All @@ -12,23 +7,17 @@ spring:
hibernate:
dialect: org.hibernate.dialect.MySQLDialect
format_sql: true
data:
redis:
host: localhost # ๋กœ์ปฌ์—์„œ ์‹คํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ
port: 6379
config:
import: env.properties
import: optional:env.properties
security:
oauth2:
redirect-url: ${REDIRECT_URL}
client:
provider:
kakao:
authorization-uri: https://kauth.kakao.com/oauth/authorize
token-uri: https://kauth.kakao.com/oauth/token
user-info-uri: https://kapi.kakao.com/v2/user/me
user-name-attribute: id

registration:
kakao:
client-id: ${CLIENT_ID}
Expand All @@ -47,3 +36,10 @@ springdoc:
swagger-ui:
path: /swagger

jwt:
secret: ${JWT_SECRET}

clova:
speech:
client-secret: ${CLOVA_CLIENT_SECRET}
language: ko
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
// @SpringBootTest
class SplanetApplicationTests {

@Test
Expand Down
Empty file.

0 comments on commit 8e905c0

Please sign in to comment.