Skip to content

Commit

Permalink
feat: CD in prod (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
devmizz authored Aug 17, 2024
1 parent c76fa4a commit d2dbeab
Show file tree
Hide file tree
Showing 10 changed files with 531 additions and 150 deletions.
95 changes: 95 additions & 0 deletions .github/workflows/showpot-prod-cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: prod-cd

on:
push:
branches:
- prod

env:
AWS_REGION: ap-northeast-2
ECR_REPOSITORY: showpot-application

jobs:
build-and-deploy:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'liberica'
cache: gradle

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3

- name: Copy Secrets
uses: microsoft/variable-substitution@v1
with:
files: './app/src/main/resources/application-prod.yml, ./app/src/main/resources/application-cloud-prod.yml, ./app/domain/common-domain/src/main/resources/application-domain-prod.yml'
env:
token.secret-key: ${{ secrets.TOKEN_SECRET_KEY }}
cloud.aws.credentials.accessKey: ${{ secrets.AWS_ACCESS_KEY }}
cloud.aws.credentials.secretKey: ${{ secrets.AWS_SECRET_KEY }}
cloud.aws.region: ${{ secrets.AWS_REGION }}
cloud.aws.s3.bucket: ${{ secrets.AWS_BUCKET }}
spring.datasource.url: ${{ secrets.APPLICATION_DATASOURCE_URL_PROD }}
spring.datasource.username: ${{ secrets.APPLICATION_DATASOURCE_USERNAME }}
spring.datasource.password: ${{ secrets.APPLICATION_DATASOURCE_PASSWORD }}
spring.data.redis.host: ${{ secrets.REDIS_HOST_PROD }}
spring.data.redis.port: ${{ secrets.REDIS_PORT_PROD }}

- name: Build with Gradle Wrapper
run: ./gradlew clean build -Dspring.profiles.active=prod

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }}
aws-region: ${{ env.AWS_REGION }}

- name: Set image tag
id: set-tag
run: |
DATE_TAG=$(TZ='Asia/Seoul' date +'%Y%m%d_%H%M')
SHORT_SHA=$(echo $GITHUB_SHA | cut -c1-8)
echo "IMAGE_TAG=${DATE_TAG}_${SHORT_SHA}" >> $GITHUB_ENV
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build and Push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ env.IMAGE_TAG }}
run: |
docker build --no-cache -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f dockerfile-prod .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
IMAGE_URI=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "IMAGE_URI=$IMAGE_URI" >> $GITHUB_ENV
- name: Update ECS task definition
id: update-task
run: |
TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition showpot-core --region $AWS_REGION)
NEW_TASK_DEFINITION=$(echo $TASK_DEFINITION | jq --arg IMAGE_URI "$IMAGE_URI" '
.taskDefinition |
.containerDefinitions[0].image=$IMAGE_URI |
del(.taskDefinitionArn, .revision, .status, .requiresAttributes, .compatibilities, .registeredAt, .registeredBy)')
echo $NEW_TASK_DEFINITION > new-task-def.json
REGISTERED_TASK_DEFINITION=$(aws ecs register-task-definition --cli-input-json file://new-task-def.json)
TASK_REVISION=$(echo $REGISTERED_TASK_DEFINITION | jq -r '.taskDefinition.taskDefinitionArn')
echo "Registered new task definition revision: $TASK_REVISION"
echo "TASK_REVISION=$TASK_REVISION" >> $GITHUB_ENV
- name: Deploy to ECS service
run: |
aws ecs update-service --cluster showpot-cluster --service showpot-core-service --task-definition $TASK_REVISION --force-new-deployment --region $AWS_REGION
515 changes: 401 additions & 114 deletions app/domain/common-domain/src/main/resources/data.sql

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions app/domain/common-domain/src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ create table social_login
updated_at timestamp(3) not null,
is_deleted boolean not null,
user_id uuid not null,
identifier varchar(255) not null,
identifier varchar(1000) not null,
social_login_type varchar(255) not null check (social_login_type in ('GOOGLE', 'KAKAO', 'APPLE')),
primary key (id),
constraint unq_social_login_type_identifier unique (social_login_type, identifier)
Expand All @@ -212,7 +212,7 @@ create table users
updated_at timestamp(3) not null,
is_deleted boolean not null,
birth date not null,
fcm_token varchar(255) not null,
fcm_token varchar(1000) not null,
gender varchar(255) not null check (gender in ('MAN', 'WOMAN', 'NOT_CHOSEN')),
nickname varchar(255) not null unique,
role varchar(255) not null check (role in ('GUEST', 'USER', 'ADMIN')),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.GenericToStringSerializer;

@Configuration
@EnableRedisRepositories(basePackages = "org.example")
@EnableConfigurationProperties(RedisProperty.class)
@ComponentScan(basePackages = "org.example")
@RequiredArgsConstructor
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
profile:
active: dev

spring:
mvc:
hidden-method:
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
profile:
active: local

spring:
mvc:
hidden-method:
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
profile:
active: prod

spring:
mvc:
hidden-method:
filter:
enabled: true
docker:
compose:
enabled: false
enabled: false
data:
redis:
host: ${REDIS_HOST_PROD}
port: ${REDIS_PORT_PROD}

token:
secret-key: ${TOKEN_SECRET_KEY}
access-token-expiration-seconds: 3600000 # 1hour = 1000(=1s) * 60 * 60
refresh-token-expiration-seconds: 1209600000 # 2weeks = 1000(=1s) * 60 * 60 * 24 * 14
16 changes: 11 additions & 5 deletions app/src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<property name="CONSOLE_LOG_PATTERN"
value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>

<springProperty name="ACTIVE_PROFILE" source="spring.profiles.active"/>
<springProperty name="ACTIVE_PROFILE" source="profile.active"/>
<springProperty name="AWS_ACCESS_KEY" source="cloud.aws.credentials.accessKey"/>
<springProperty name="AWS_SECRET_KEY" source="cloud.aws.credentials.secretKey"/>

Expand All @@ -36,7 +36,7 @@
<secretAccessKey>${AWS_SECRET_KEY}</secretAccessKey>

<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
<level>INFO</level>
</filter>

<encoder>
Expand All @@ -45,14 +45,20 @@
</encoder>
</appender>

<springProfile name="local, dev, prod">
<springProfile name="local">
<root level="INFO">
<appender-ref ref="Console"/>
</root>
</springProfile>

<springProfile name="dev, prod">
<root level="WARN">
<springProfile name="dev">
<root level="INFO">
<appender-ref ref="CloudWatchAppender"/>
</root>
</springProfile>

<springProfile name="prod">
<root level="INFO">
<appender-ref ref="CloudWatchAppender"/>
</root>
</springProfile>
Expand Down
27 changes: 0 additions & 27 deletions docker-compose-prod.yml

This file was deleted.

2 changes: 1 addition & 1 deletion dockerfile-prod
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ COPY --from=build ${EXTRACTED}/spring-boot-loader/ ./
COPY --from=build ${EXTRACTED}/snapshot-dependencies/ ./
COPY --from=build ${EXTRACTED}/application/ ./

ENTRYPOINT java -Dspring.profiles.active=dev org.springframework.boot.loader.launch.JarLauncher
ENTRYPOINT java -Dspring.profiles.active=prod org.springframework.boot.loader.launch.JarLauncher

0 comments on commit d2dbeab

Please sign in to comment.