Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT -> MAIN : CI/CD 파이프라인 구축 #1

Merged
merged 15 commits into from
Oct 29, 2023
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @CChuYong
10 changes: 9 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: 이미지 빌드 & 푸쉬
on:
workflow_call:
outputs:
image-url:
value: ${{ jobs.build.outputs.image-url }}
inputs:
ecr-repository-name:
required: true
Expand All @@ -24,6 +27,9 @@ jobs:
runs-on: [ ubuntu-latest ]
name: 이미지 빌드하기

outputs:
image-url: ${{ steps.build-image.outputs.image-url }}

permissions:
id-token: write
contents: read
Expand Down Expand Up @@ -54,7 +60,9 @@ jobs:
ECR_REGISTRY_PREFIX: '${{ secrets.AWS_ECR_REGISTRY_URL }}/'
run: |
chmod +x scripts/dockerBuild.sh &&
chmod +x ./gradlew &&
/bin/bash scripts/dockerBuild.sh \
${{ env.ECR_REGISTRY_PREFIX }}${{ inputs.ecr-repository-name }} \
${{ inputs.image-tag }} \
${{ inputs.spring-profile }}
${{ inputs.spring-profile }} &&
echo "image-url=${{ inputs.ecr-repository-name }}:${{ inputs.image-tag }}" >> $GITHUB_OUTPUT
56 changes: 52 additions & 4 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
name: AWS ECS 배포
on:
workflow_call:

inputs:
ecs-service:
required: true
type: string
ecs-cluster:
required: true
type: string
ecr-task-definition:
required: true
type: string
image-url:
required: true
type: string
environment:
required: true
type: string
secrets:
AWS_ASSUME_ROLE_ARN:
required: true
AWS_ECR_REGISTRY_URL:
required: true
AWS_REGION:
required: true
jobs:
deploy:
environment: ${{ inputs.environment }}
runs-on: [ ubuntu-latest ]
name: ECS 배포하기

Expand All @@ -12,6 +35,31 @@ jobs:
contents: read

steps:
- name: AWS ECR 로그인하기
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: AWS 인증정보 준비하기
uses: aws-actions/configure-aws-credentials@v2
with:
role-to-assume: ${{ secrets.AWS_ASSUME_ROLE_ARN }}
aws-region: ${{ secrets.AWS_REGION }}

- name: AWS 태스크 정의 파일 내려받기
run: |
aws ecs describe-task-definition \
--task-definition ${{ inputs.ecr-task-definition }} \
--query taskDefinition \
> task-definition.json

- name: AWS 태스크 정의 파일에 이미지 버전 업데이트하기
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: task-definition.json
container-name: api
image: "${{ secrets.AWS_ECR_REGISTRY_URL }}/${{ inputs.image-url }}"

- name: AWS 태스크 정의 배포하고 서비스 업데이트하기
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ inputs.ecs-service }}
cluster: ${{ inputs.ecs-cluster }}
wait-for-service-stability: true # ECS 배포후 롤링업데이트까지 성공해야 태스크가 완료됩니다.
31 changes: 30 additions & 1 deletion .github/workflows/prod.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: 프로덕션 워크플로우
on:
push:
branches: [ 'main' ]
#branches: [ 'main' ]
paths:
- 'src/**' # 어드민 API 코드가 변경된 경우
- '.github/workflows/**' # 워크플로우와 관련된 파일이 변경된 경우
Expand All @@ -11,6 +11,9 @@ on:
env:
ECR_REPOSITORY_NAME: nagaza-backend-prod
SPRING_PROFILE: prod
ECS_SERVICE: nagaza-backend
ECS_CLUSTER: nagaza-cluster-prod
ECR_TASK_DEFINITION: nagaza-backend-prod

concurrency:
group: api
Expand All @@ -23,6 +26,9 @@ jobs:
ecr-repository-name: ${{ steps.setup-env.outputs.ecr-repository-name }}
image-tag: ${{ steps.setup-env.outputs.image-tag }}
spring-profile: ${{ steps.setup-env.outputs.spring-profile }}
ecs-service: ${{ env.ECS_SERVICE }}
ecs-cluster: ${{ env.ECS_CLUSTER }}
ecr-task-definition: ${{ env.ECR_TASK_DEFINITION }}
steps:
- name: GitHub 에서 레포 받아오기
uses: actions/checkout@v3
Expand All @@ -33,6 +39,10 @@ jobs:
echo "ecr-repository-name=$ECR_REPOSITORY_NAME" >> $GITHUB_OUTPUT
echo "image-tag=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
echo "spring-profile=$SPRING_PROFILE" >> $GITHUB_OUTPUT
echo "ecs-service=$ECS_SERVICE" >> $GITHUB_OUTPUT
echo "ecs-cluster=$ECS_CLUSTER" >> $GITHUB_OUTPUT
echo "ecr-task-definition=$ECR_TASK_DEFINITION" >> $GITHUB_OUTPUT

call-build-workflow:
if: github.event_name == 'push'
needs: [ prepare-variables ]
Expand All @@ -49,3 +59,22 @@ jobs:
AWS_ASSUME_ROLE_ARN: ${{ secrets.AWS_ASSUME_ROLE_ARN }}
AWS_ECR_REGISTRY_URL: ${{ secrets.AWS_ECR_REGISTRY_URL }}
AWS_REGION: ${{ secrets.AWS_REGION }}

call-deploy-workflow:
if: github.event_name == 'push'
needs: [ prepare-variables, call-build-workflow ]
name: AWS ECS 배포
uses: ./.github/workflows/deploy.yaml
permissions:
id-token: write
contents: read
with:
environment: production
ecs-service: ${{ needs.prepare-variables.outputs.ecs-service }}
ecs-cluster: ${{ needs.prepare-variables.outputs.ecs-cluster }}
ecr-task-definition: ${{ needs.prepare-variables.outputs.ecr-task-definition }}
image-url: ${{ needs.call-build-workflow.outputs.image-url }}
secrets:
AWS_ASSUME_ROLE_ARN: ${{ secrets.AWS_ASSUME_ROLE_ARN }}
AWS_ECR_REGISTRY_URL: ${{ secrets.AWS_ECR_REGISTRY_URL }}
AWS_REGION: ${{ secrets.AWS_REGION }}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ dependencies {
runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.11.5")
implementation("com.github.f4b6a3:ulid-creator:5.2.0")
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0")
runtimeOnly("org.springdoc:springdoc-openapi-kotlin:1.7.0")
//runtimeOnly("org.springdoc:springdoc-openapi-kotlin:1.7.0")

implementation("com.querydsl:querydsl-jpa:5.0.0:jakarta")
kapt("com.querydsl:querydsl-apt:5.0.0:jakarta")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CafeEntity(
val addressOne: String?,

@Column(name = "addr_2")
val addressTwo: String?
val addressTwo: String?,
) {
fun toModel() = Cafe(
id = cafeId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface MeApi {
@Operation(summary = "내가 작성한 리뷰 목록 조회", description = "내가 작성한 리뷰 목록을 조회합니다.")
@GetMapping("/reviews")
fun getMyReviews(
@RequestUser userId: String
@RequestUser userId: String,
): List<CafeRoomReviewResponse>

@Operation(summary = "닉네임 변경", description = "내 닉네임을 변경합니다.")
Expand Down
17 changes: 9 additions & 8 deletions src/main/resources/db/migration/V1__init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ CREATE TABLE app_info
in_service BOOLEAN NOT NULL COMMENT '서비스 여부',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_At DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY app_info_pk(app_version)
) DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_unicode_ci COMMENT='앱정보';
PRIMARY KEY app_info_pk (app_version)
) DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci COMMENT ='앱정보';

CREATE TABLE user
(
Expand Down Expand Up @@ -43,7 +43,7 @@ CREATE TABLE cafe
cafe_id CHAR(26) NOT NULL,
franchise_id CHAR(26) DEFAULT NULL,
cafe_name VARCHAR(128) NOT NULL,
description TEXT NOT NULL DEFAULT '',
description TEXT NOT NULL,
address VARCHAR(255),
web_url VARCHAR(255),
phone_number VARCHAR(36),
Expand All @@ -64,7 +64,7 @@ CREATE TABLE cafe_room
timeout INT NOT NULL,
recommend_user INT NOT NULL,
room_img_url VARCHAR(128),
description TEXT NOT NULL DEFAULT '',
description TEXT NOT NULL,
PRIMARY KEY (room_id),
FOREIGN KEY cafe_room_fk1 (cafe_id) REFERENCES cafe (cafe_id) ON DELETE CASCADE
) DEFAULT CHARSET = utf8mb4
Expand Down Expand Up @@ -121,6 +121,7 @@ CREATE TABLE cafe_room_review_det_opt
) DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci comment '방탈출카페방리뷰디테일선택';

#
MOCK DML
INSERT INTO user (user_id, nickname, profile_img_url) VALUES ('01HDNFJHCNS5E2W35YTB030TJ8', '테스트용사용자', 'https://cdn.pixabay.com/photo/2016/08/08/09/17/avatar-1577909_960_720.png');
#MOCK DML
INSERT INTO user (user_id, nickname, profile_img_url)
VALUES ('01HDNFJHCNS5E2W35YTB030TJ8', '테스트용사용자',
'https://cdn.pixabay.com/photo/2016/08/08/09/17/avatar-1577909_960_720.png');
Loading