Skip to content

Commit

Permalink
feature: CD를 Github Actions에서 CodeDeploy으로 변경 (#152)
Browse files Browse the repository at this point in the history
* feature: codedeploy test

* feature: codedeploy test

* feature: codedeploy test

* feature: add buildspec.yml

* feature: add buildspec.yml

* feature: add buildspec.yml

* feature: add compatibility for correcto17

* feature: add compatibility for correcto17

* fix: docker authentication issue

* feature: code deploy

* feature: code deploy

* feature: ci test

* feature: ci test

* feature: cd test

* feature: cd test

* feature: cicd test

* feature: cicd test

* feature: cicd test

* feature: cicd test

* feature: cicd test

* feature: cicd test

* feature: cicd test

* feature: cicd test

* feature: cicd test

* feature: cicd test

* feature: cicd test

* feature: cicd test

* feature: cicd test

* feature: cicd test

* feature: cicd with CodeDeploy

* fix: issue template

* fix: revert brand name filter

* fix: add brandname search for keyword filter
  • Loading branch information
mushroom1324 authored Sep 15, 2024
1 parent 7e0a15f commit 225a45e
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/fix.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ assignees: ''

---

# [Fix] <!--{ 작업 내용 }-->
# [Fix] <!--{ 작업 내용 }-->

### 📝 Description

Expand Down
82 changes: 82 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Push to ECR

on:
push:
branches: [ "develop" ]

env:
AWS_REGION: ap-northeast-2
ECR_REGISTRY: 654654448479.dkr.ecr.ap-northeast-2.amazonaws.com
ECR_REPOSITORY: repick-repo
ZIP_FILE_NAME: deploy-package.zip

permissions:
contents: read

jobs:
build:
name: CI
runs-on: ubuntu-latest
environment: production

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Make application-secret.yml and set secrets
run: |
mkdir -p ./src/main/resources
mkdir -p ./src/main/resources/key
if [ -f ./src/main/resources/application.yml ]; then
rm ./src/main/resources/application.yml
fi
touch ./src/main/resources/application.yml
touch ./src/main/resources/${{ secrets.APPLE_AUTH_KEY_ID }}.p8
echo "${{ secrets.APPLICATION_SECRET }}" > ./src/main/resources/application.yml
echo "${{ secrets.APPLE_AUTH_KEY }}" > ./src/main/resources/key/${{ secrets.APPLE_AUTH_KEY_ID }}.p8
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'

## gradle build
- name: Build with Gradle
run: ./gradlew bootJar

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

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
with:
mask-password: true

- name: Build, tag, and push image to Amazon ECR
id: build-image
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY .
docker push $ECR_REGISTRY/$ECR_REPOSITORY
- name: Zip deployment files
run: |
zip -r ${{ env.ZIP_FILE_NAME }} ./aws/
- name: Upload AppSpec and scripts to S3
run: |
aws s3 cp ${{ env.ZIP_FILE_NAME }} s3://${{ secrets.S3_BUCKET }}/${{ env.ZIP_FILE_NAME }}
- name: Trigger CodeDeploy
run: |
aws deploy create-deployment \
--application-name repick-codedeploy-app \
--deployment-group-name repick-codedeploy-deployment-group \
--s3-location bucket=${{ secrets.S3_BUCKET }},bundleType=zip,key=${{ env.ZIP_FILE_NAME }} \
--file-exists-behavior OVERWRITE \
--ignore-application-stop-failures
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Push to ECR

on:
push:
branches: [ "develop" ]
branches: [ "legacy-githubactions" ]

env:
AWS_REGION: ap-northeast-2
Expand Down
20 changes: 20 additions & 0 deletions aws/appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: 0.0
os: linux
files:
- source: /
destination: /home/ec2-user
overwrite: yes
permissions:
- object: /home/ec2-user
pattern: "**"
owner: ec2-user
group: ec2-user
hooks:
ApplicationStop:
- location: kill_process.sh
timeout: 100
runas: ec2-user
ApplicationStart:
- location: start_process.sh
timeout: 3600
runas: ec2-user
41 changes: 41 additions & 0 deletions aws/buildspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
version: 0.2

env:
variables:
AWS_REGION: "ap-northeast-2"
ECR_REPOSITORY: "repick-repo:latest"

parameter-store:
ECR_REGISTRY: "ECR_REGISTRY"
APPLICATION_SECRET: "application-secrets"

phases:
install:
runtime-versions:
java: corretto17
pre_build:
commands:
- echo "Setting up resources and secrets"
- mkdir -p ./src/main/resources
- mkdir -p ./src/main/resources/key
- rm -f ./src/main/resources/application.yml
- touch ./src/main/resources/application.yml
build:
commands:
- echo "Building application with Gradle"
- ./gradlew bootJar
- echo "Building Docker image"
- docker build -t $ECR_REGISTRY/$ECR_REPOSITORY .

post_build:
commands:
- echo "Logging into Amazon ECR"
- aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ECR_REGISTRY
- echo "Pushing Docker image to Amazon ECR"
- docker push $ECR_REGISTRY/$ECR_REPOSITORY
artifacts:
files:
- appspec.yml
- kill_process.sh
- start_process.sh
discard-paths: yes
11 changes: 11 additions & 0 deletions aws/kill_process.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

# 실행 중이거나 중지된 컨테이너 목록을 가져옴
containers=$(docker ps -qa)

# 컨테이너가 존재하면 삭제
if [ -n "$containers" ]; then
sudo docker rm -f $containers
else
echo "No containers to remove."
fi
15 changes: 15 additions & 0 deletions aws/start_process.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -e

ECR_REGISTRY=$(aws ssm get-parameter --name "ECR_REGISTRY" --with-decryption --region ap-northeast-2 --query "Parameter.Value" --output text)
ECR_DOCKER_TAG=latest

echo $ECR_REGISTRY
aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin $ECR_REGISTRY

sudo docker pull $ECR_REGISTRY:$ECR_DOCKER_TAG

sudo docker run -d --name dc -p 8080:8080 $ECR_REGISTRY:$ECR_DOCKER_TAG

sudo docker image prune -f
3 changes: 2 additions & 1 deletion dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM openjdk:17
ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","-Duser.timezone=Asia/Seoul","/app.jar"]
ENTRYPOINT ["java","-jar","-Duser.timezone=Asia/Seoul","/app.jar"]

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public record ProductFilter(
@Schema(description = "스타일") List<String> styles,
@Schema(description = "최소 가격") Long minPrice,
@Schema(description = "최대 가격") Long maxPrice,
@Schema(description = "브랜드") String brandName,
@Schema(description = "브랜드") List<String> brandNames,
@Schema(description = "상품등급") List<String> qualityRates,
@Schema(description = "사이즈") List<String> sizes,
@Schema(description = "내 사이즈 여부") Boolean isMySize,
Expand All @@ -28,7 +28,7 @@ public ProductFilter withMySize(List<String> sizes) {
this.styles,
this.minPrice,
this.maxPrice,
this.brandName,
this.brandNames,
this.qualityRates,
sizes,
this.isMySize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private Page<GetProductThumbnail> findProducts(
categoryFilter(productFilter.category(), productFilter.isParentCategory()),
stylesFilter(productFilter.styles()),
priceFilter(productFilter.minPrice(), productFilter.maxPrice()),
brandFilter(productFilter.brandName()),
brandFilter(productFilter.brandNames()),
qualityFilter(productFilter.qualityRates()),
sizesFilter(productFilter.sizes()),
materialsFilter(productFilter.materials()),
Expand Down Expand Up @@ -200,7 +200,8 @@ private BooleanExpression likeFilter(Long userId) {
}

private BooleanExpression keywordFilter(String keyword) {
return keyword != null ? product.productName.contains(keyword) : null;
return keyword != null ? product.productName.contains(keyword)
.or(product.brandName.contains(keyword)) : null;
}

private BooleanExpression genderFilter(String gender) {
Expand Down Expand Up @@ -230,8 +231,8 @@ private BooleanExpression priceFilter(Long minPrice, Long maxPrice) {
return (minPrice != null && maxPrice != null) ? product.price.between(minPrice, maxPrice) : null;
}

private BooleanExpression brandFilter(String brandName) {
return brandName != null ? product.brandName.like("%" + brandName + "%") : null;
private BooleanExpression brandFilter(List<String> brandNames) {
return brandNames != null ? product.brandName.in(brandNames) : null;
}

private BooleanExpression qualityFilter(List<String> qualityRates) {
Expand Down

0 comments on commit 225a45e

Please sign in to comment.