Skip to content

Commit

Permalink
cd: ecs로 cd 방식 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
kimday0326 committed Mar 7, 2024
1 parent bfbeb76 commit a766fdc
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 76 deletions.
86 changes: 86 additions & 0 deletions .aws/task-definition.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"taskDefinitionArn": "arn:aws:ecs:ap-northeast-2:533267244952:task-definition/tikitaza-task-def-dev:8",
"containerDefinitions": [
{
"name": "tikitaza-container",
"image": "533267244952.dkr.ecr.ap-northeast-2.amazonaws.com/tikitaza:2024-03-07T19-36-32",
"cpu": 0,
"portMappings": [
{
"name": "tikitaza-container-8080",
"containerPort": 8080,
"hostPort": 8080,
"protocol": "tcp",
"appProtocol": "http"
}
],
"essential": true,
"environment": [],
"mountPoints": [],
"volumesFrom": [],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-create-group": "true",
"awslogs-group": "/ecs/tikitaza-task-def-dev",
"awslogs-region": "ap-northeast-2",
"awslogs-stream-prefix": "ecs"
}
},
"systemControls": []
}
],
"family": "tikitaza-task-def-dev",
"taskRoleArn": "arn:aws:iam::533267244952:role/ecsTaskExecutionRole",
"executionRoleArn": "arn:aws:iam::533267244952:role/ecsTaskExecutionRole",
"networkMode": "awsvpc",
"revision": 8,
"volumes": [],
"status": "ACTIVE",
"requiresAttributes": [
{
"name": "com.amazonaws.ecs.capability.logging-driver.awslogs"
},
{
"name": "ecs.capability.execution-role-awslogs"
},
{
"name": "com.amazonaws.ecs.capability.ecr-auth"
},
{
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.19"
},
{
"name": "com.amazonaws.ecs.capability.task-iam-role"
},
{
"name": "ecs.capability.execution-role-ecr-pull"
},
{
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.18"
},
{
"name": "ecs.capability.task-eni"
},
{
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.29"
}
],
"placementConstraints": [],
"compatibilities": [
"EC2",
"FARGATE"
],
"requiresCompatibilities": [
"FARGATE"
],
"cpu": "1024",
"memory": "3072",
"runtimePlatform": {
"cpuArchitecture": "X86_64",
"operatingSystemFamily": "LINUX"
},
"registeredAt": "2024-03-07T10:36:56.404Z",
"registeredBy": "arn:aws:iam::533267244952:user/team-02",
"tags": []
}
131 changes: 55 additions & 76 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,28 @@ on:
push:
branches: [ "main" ]

env:
AWS_REGION: ap-northeast-2
ECR_REPOSITORY: 533267244952.dkr.ecr.ap-northeast-2.amazonaws.com/tikitaza
ECS_SERVICE: tikitaza-service
ECS_CLUSTER: tikitaza-cluster
ECS_TASK_DEFINITION: .aws/task-definition.json
CONTAINER_NAME: tikitaza-container

permissions:
contents: read

jobs:
build-and-push-docker:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v4
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

## gradle 캐싱
- name: Gradle Caching
uses: actions/cache@v4
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
## create application.yml
- name: make application-secret.yml
run: |
Expand All @@ -40,76 +37,58 @@ jobs:
run: echo "${{ secrets.APPLICATION_SECRET }}" > ./api/src/main/resources/application-secret.yml
shell: bash

# 빌드 및 테스트 단계.
## gradlew 권한 부여
- name: Grant execute permission for gradlew
run: chmod +x gradlew

# gradle build
## gradle build
- name: Build with Gradle
run: ./gradlew build -x test
run: ./gradlew clean build

# push 하기 위해 로그인
- name: Docker Hub 로그인
uses: docker/login-action@v3
## 현재 시간 가져오기
- name: Get current time
uses: 1466587594/get-current-time@v2
id: current-time
with:
username: ${{ secrets.DOCKER_ID }}
password: ${{ secrets.DOCKER_PASSWORD }}
format: YYYY-MM-DDTHH-mm-ss
utcOffset: "+09:00"

#도커 빌드 & 이미지 push
- name: Docker build & Push
run: |
docker build -f Dockerfile -t ${{ secrets.DOCKER_ID }}/tiki-taza .
docker push ${{ secrets.DOCKER_ID }}/tiki-taza
- name: Show Current Time
run: echo "CurrentTime=${{ steps.current-time.outputs.formattedTime }}"

# Docker 파일을 EC2 서버에 배포
- name: Deploy to Prod
uses: appleboy/ssh-action@master
id: deploy-prod
## AWS에 로그인
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_PEM_KEY }}
port: 22
script: |
if [ ! -z "$(docker ps -q)" ]; then
docker stop $(docker ps -q)
fi
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}

if [ ! -z "$(docker ps -aq)" ]; then
docker rm $(docker ps -aq)
fi
if [ ! -z "$(docker network ls -qf name=tikitaza)" ]; then
docker network rm tikitaza
fi
docker login -u ${{ secrets.DOCKER_ID }} -p ${{ secrets.DOCKER_PASSWORD }}
docker pull ${{ secrets.DOCKER_ID }}/tiki-taza
docker network create tikitaza
docker run -d \
--name tiki-taza \
--network=tikitaza \
-p 80:8080 \
-e TZ=Asia/Seoul \
${{ secrets.DOCKER_ID }}/tiki-taza
docker run -d \
--name zookeeper \
--network=tikitaza \
-p 2181:2181 \
wurstmeister/zookeeper
# Kafka 컨테이너 실행
docker run -d \
--name kafka \
--network=tikitaza \
-p 9092:9092 \
-e KAFKA_ADVERTISED_HOST_NAME=${{ secrets.EC2_HOST }}\
-e KAFKA_ADVERTISED_PORT=9092 \
-e KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 \
wurstmeister/kafka
## ECR에 로그인
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

docker system prune -f
- name: Build, tag, and push image to Amazon ECR
id: build-image
run: |
docker build -t tikitaza:${{ steps.current-time.outputs.formattedTime }} .
docker tag tikitaza:${{ steps.current-time.outputs.formattedTime }} ${{ env.ECR_REPOSITORY }}:${{ steps.current-time.outputs.formattedTime }}
docker push ${{ env.ECR_REPOSITORY }}:${{ steps.current-time.outputs.formattedTime }}
echo "image=${{ env.ECR_REPOSITORY }}:${{ steps.current-time.outputs.formattedTime }}" >> $GITHUB_OUTPUT
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ env.ECS_TASK_DEFINITION }}
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ steps.build-image.outputs.image }}

- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: true

0 comments on commit a766fdc

Please sign in to comment.