-
Notifications
You must be signed in to change notification settings - Fork 1
131 lines (120 loc) · 4.27 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# This is a basic workflow to help you get started with Actions
name: Deploy to Amazon EC2
# Controls when the workflow will run
on:
# Triggers the 'Deploy' workflow after the push but only for the master branch - (instead of Pull Request)
push:
branches: [ "master" ]
permissions:
contents: read
id-token: write
# Setting environment values
env:
# AWS 관련 환경변수
AWS_REGION: ap-northeast-2 # EC2 Region
S3_BUCKET_NAME: lots-deploy-bucket # S3 bucket name
CODE_DEPLOY_APPLICATION_NAME: arrival-codedeploy
CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: arrival-codedeploy-group
IAM_ROLE_ARN: ${{ secrets.IAM_ROLE_ARN }}
# APP 관련 환경변수
MYSQL_ROOT_PWD: ${{ secrets.MYSQL_ROOT_PWD }}
MYSQL_DB: ${{ secrets.MYSQL_DB }}
MYSQL_HOST: ${{ secrets.MYSQL_HOST }}
REDIS_HOST: ${{ secrets.REDIS_HOST }}
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "deploy"
deploy:
name: Deploy
# The type of runner that the job will run on
runs-on: ubuntu-latest
environment: production
services:
mysql:
image: mysql:8.0.37
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping --silent"
--health-interval=10s
--health-timeout=5s
--health-retries=3
env:
MYSQL_ROOT_PASSWORD: ${{ env.MYSQL_ROOT_PWD }}
MYSQL_DATABASE: ${{ env.MYSQL_DB }}
redis:
image: redis:7.0.15
ports:
- 6379:6379
options: >-
--health-cmd="redis-cli ping"
--health-interval=10s
--health-timeout=5s
--health-retries=3
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# (1) 기본 체크아웃 (v2->v3)
- name: Checkout
uses: actions/checkout@v3
# (2) JDK 17 세팅 (v1->v3)
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'adopt'
# (3) Gradle 권한설정
- name: Cache Gradle packages
uses: actions/cache@v2
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Grant execute permission for gradlew
run: chmod +x gradlew
shell: bash
# (4) Redis 설치
- name: Install redis-cli
run: sudo apt-get update && sudo apt-get install -y redis-tools
# (5) Mysql 세팅 대기
- name: Wait for MySQL
run: |
until mysqladmin ping -h ${{ env.MYSQL_HOST }} --silent; do
echo 'waiting for mysql to be connectable...'
sleep 5
done
# (6) Redis 세팅 대기
- name: Wait for Redis
run: |
until redis-cli -h ${{ env.REDIS_HOST }} ping; do
echo 'waiting for redis to be connectable...'
sleep 5
done
# (7) Gradle build (Test 제외)
- name: Build with Gradle
run: ./gradlew build -x test
shell: bash
# (8) AWS 인증 (IAM Role 사용 시도)
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v3
with:
aws-region: ${{ env.AWS_REGION }}
role-to-assume: ${{ env.IAM_ROLE_ARN }} # Use IAM role instead of IAM User (+Key, Id)
# (9) 빌드 결과물을 S3 버킷에 업로드
- name: Upload to AWS S3
run: |
aws deploy push \
--application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \
--ignore-hidden-files \
--s3-location s3://$S3_BUCKET_NAME/$GITHUB_SHA.zip \
--source .
# (10) S3 버킷에 있는 파일을 대상으로 CodeDeploy 실행
- name: Deploy to AWS EC2 from S3
run: |
aws deploy create-deployment \
--application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \
--deployment-config-name CodeDeployDefault.AllAtOnce \
--deployment-group-name ${{ env.CODE_DEPLOY_DEPLOYMENT_GROUP_NAME }} \
--s3-location bucket=$S3_BUCKET_NAME,key=$GITHUB_SHA.zip,bundleType=zip