Skip to content

Commit

Permalink
[Feat/#268] AWS 이전 작업 (#271)
Browse files Browse the repository at this point in the history
* feat: ecs cd를 위한 구현

* chore: 오타 수정

* fix: aws 환경에 맞추어 AmazonS3Client 생성 방식 수정

* feat: logback-spring 추가

* feat: resource 라벨 설정 추가

* feat: hikari 로그 설정  추가
  • Loading branch information
belljun3395 authored Aug 1, 2024
1 parent d205f43 commit 571205f
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ script:
- changed-files:
- any-glob-to-any-file: '**/scripts/**'

# resource 폴더 하위에 있는 파일이 변경되거나, 브랜치 이름이 resource로 시작하면 resource 라벨을 붙인다.
resource:
- changed-files:
- any-glob-to-any-file: '**/resource/**'
- head-branch: ['^resource', 'resource']

# 브랜치 이름이 feat 또는 feature로 시작하면 feature 라벨을 붙인다.
feature:
- head-branch: ['^feat', 'feat', '^feature', 'feature']
Expand Down
95 changes: 95 additions & 0 deletions .github/workflows/ecs-cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: DEV - Deploy to Amazon ECS

on:
push:
branches:
- main
workflow_dispatch:
env:
AWS_REGION: ap-northeast-2
ECR_REPOSITORY: few-ecr
ECS_SERVICE: few-ecs-service
ECS_CLUSTER: few-ecs-cluster
ECS_TASK_DEFINITION: task-definition.json
TASK_DEFINITION_NAME: few-ecs-task
CONTAINER_NAME: few-container

jobs:
build-and-push-docker-image:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

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

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

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Grant execute permission for gradlew
run: chmod +x gradlew

- name: Jooq Code Generation
run: |
./gradlew jooqCodegenAll
- name: Test with Gradle
run: |
./gradlew test
- name: Generate OpenAPI3
run: |
./gradlew --info api:openapi3 -PserverUrl=https://api.fewletter.site
- name: Generate Swagger
run: |
./gradlew --info api:generateStaticSwaggerUIApi
- name: Build with Gradle bootBuildImage, tag, and push image to Amazon ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
run: |
GIT_HASH=$(git rev-parse --short HEAD)
./gradlew buildEcsDockerImage -PimageName=${ECR_REGISTRY}/${ECR_REPOSITORY}:latest
docker tag ${ECR_REGISTRY}/${ECR_REPOSITORY}:latest ${ECR_REGISTRY}/${ECR_REPOSITORY}:${GIT_HASH}
docker push ${ECR_REGISTRY}/${ECR_REPOSITORY} --all-tags
- name: Get ECR Repository image path
id: get-docker-image-path
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
run: |
GIT_HASH=$(git rev-parse --short HEAD)
echo ${ECR_REGISTRY}/${ECR_REPOSITORY}:${GIT_HASH}
echo "::set-output name=image::${ECR_REGISTRY}/${ECR_REPOSITORY}:${GIT_HASH}"
- name: Download task definition
run: |
aws ecs describe-task-definition --task-definition ${TASK_DEFINITION_NAME} --query taskDefinition > task-definition.json
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-definition
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ env.ECS_TASK_DEFINITION }}
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ steps.get-docker-image-path.outputs.image }}

- name: Deploy Amazon ECS task definition
id: ecs-deployment
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-definition.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: true
19 changes: 19 additions & 0 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,23 @@ tasks.register("buildDockerImage") {
)
}
}
}

tasks.register("buildEcsDockerImage") {
dependsOn("bootJar")

doLast {
exec {
workingDir(".")
commandLine(
"docker",
"build",
"-t",
imageName,
"--build-arg",
"RELEASE_VERSION=$releaseVersion",
'.'
)
}
}
}
38 changes: 38 additions & 0 deletions api/src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8" ?>
<configuration debug="true">
<property resource="application.yml"/>
<property name="TRACE_ID_CONSOLE_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %magenta([%thread]) %highlight([%-3level]) [%X{TraceId}] %logger{5} - %msg %n" />
<springProperty name="LOGS_ABSOLUTE_PATH" source="log.file.path"/>

<include resource="org/springframework/boot/logging/logback/defaults.xml"/>

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${TRACE_ID_CONSOLE_PATTERN}</pattern>
</encoder>
</appender>

<springProfile name="prd">
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
<logger name="com.few" level="DEBUG">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.jooq" level="DEBUG">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="com.zaxxer.hikari" level="DEBUG">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.springframework" level="INFO">
<appender-ref ref="CONSOLE"/>
</logger>
</springProfile>

<springProfile name="local">
<root level="DEBUG">
<appender-ref ref="CONSOLE"/>
</root>
</springProfile>
</configuration>
21 changes: 20 additions & 1 deletion storage/src/main/kotlin/com/few/storage/config/ClientConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.amazonaws.services.s3.AmazonS3ClientBuilder
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile

@Configuration
class ClientConfig(
Expand All @@ -17,8 +18,9 @@ class ClientConfig(
@Value("\${storage.region}") val region: String,
) {

@Profile("!prd")
@Bean
fun s3StorageClient(): AmazonS3Client {
fun localS3StorageClient(): AmazonS3Client {
val builder = AmazonS3ClientBuilder.standard()
.withCredentials(
AWSStaticCredentialsProvider(
Expand All @@ -39,4 +41,21 @@ class ClientConfig(
return client as AmazonS3Client
}
}

@Profile("prd")
@Bean
fun prdS3StorageClient(): AmazonS3Client {
AmazonS3Client.builder()
.withRegion(region)
.withCredentials(
AWSStaticCredentialsProvider(
BasicAWSCredentials(
accessKey,
secretKey
)
)
).build().let { client ->
return client as AmazonS3Client
}
}
}

0 comments on commit 571205f

Please sign in to comment.