-
Notifications
You must be signed in to change notification settings - Fork 0
101 lines (87 loc) · 3.93 KB
/
deploy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
name: CI/CD with GitHub Actions & ECR
# Workflow trigger: Runs when a push event occurs to the main branch
on:
push:
branches:
- main
env:
AWS_REGION: ${{ secrets.AWS_REGION }}
ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY }}
ECS_TASK_DEFINITION: ${{ secrets.ECS_TASK_DEFINITION }}
ECS_CLUSTER: DrinkhereCluster
ECS_SERVICE: drinkhere-ecs-service
CONTAINER_NAME: drinkhere-springboot-server
APP_SPEC: appspec.yml
CODEDEPLOY_APPLICATION: AppECS-DrinkhereCluster-drinkhere-ecs-service
CODEDEPLOY_DEPLOYMENT_GROUP: DgpECS-DrinkhereCluster-drinkhere-ecs-service
jobs:
build:
runs-on: ubuntu-latest
steps:
# Checkout the repository including submodules
- name: Checkout submodules
uses: actions/checkout@v3
with:
submodules: recursive
token: ${{ secrets.GIT_TOKEN_FOR_SUBMODULE }}
# Set up JDK 21 for the Spring Boot project
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'temurin'
# Grant execute permission to gradlew
- name: Grant execute permission to gradlew
run: chmod +x ./gradlew
# Build the Spring Boot project and generate a JAR file
- name: Build Spring Boot Project to JAR File
run: ./gradlew clean :execute:bootJar
# Configure AWS credentials for ECR and ECS
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
# Login to Amazon ECR
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
# Build, tag, and push the Docker image to Amazon ECR
- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
run: |
# 현재 날짜와 시간으로 태그 생성 (예: 20241126_143000)
IMAGE_TAG=$(date +'%Y%m%d_%H%M%S')
echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_ENV # 환경 변수에 IMAGE_TAG 저장
# Docker 이미지를 빌드하고 푸시
docker build --build-arg JAR_FILE=build/libs/*.jar -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
# $GITHUB_ENV : 작업(Job) 내 모든 스텝에서 사용 가능 - $VARIABLE 환경 변수로 직접 참조
# $GITHUB_OUTPUT : 특정 스텝에서만 설정한 출력값 전달 가능 - steps.<step_id>.outputs.<name>로 참조
# Download the current Amazon ECS task definition
- name: Download Amazon ECS task definition
run: |
aws ecs describe-task-definition --task-definition ${{ env.ECS_TASK_DEFINITION }} --query taskDefinition > task-definition.json
# Fill in the new image ID in the Amazon ECS task definition
- name: Fill in the new image ID in the ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: task-definition.json
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ steps.build-image.outputs.image }}
# Deploy the updated ECS task definition
- name: Deploy 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
codedeploy-appspec: ${{ env.APP_SPEC }}
codedeploy-application: ${{ env.CODEDEPLOY_APPLICATION }}
codedeploy-deployment-group: ${{ env.CODEDEPLOY_DEPLOYMENT_GROUP }}