-
Notifications
You must be signed in to change notification settings - Fork 3
182 lines (154 loc) · 5.92 KB
/
deploy.yaml
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
name: Deployment
on:
pull_request:
types: [synchronize, closed]
branches:
- main
env:
ECR_URL: ${{ secrets.ECR_URL }}
jobs:
pushNewTag:
name: Push new tag
runs-on: ubuntu-latest
outputs:
NEW_VERSION: ${{ steps.increment_version.outputs.NEW_VERSION }}
ECR_URL: ${{ steps.increment_version.outputs.ECR_URL }}
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0 # get all history so we can checkout any branch
- name: Get latest tag
id: latesttag
run: |
LATEST_TAG=$(git describe --tags --abbrev=0)
echo "LATEST_TAG=$LATEST_TAG" >> $GITHUB_ENV
# Increment version number(ex) 5.0.1 -> 5.0.2)
# PR title contains "[patch]" -> 5.0.1 -> 5.0.2
# PR title contains "[minor]" -> 5.0.1 -> 5.1.0
# PR title contains "[major]" -> 5.0.1 -> 6.0.0
- name: Increment version based on commit message with commit hash
env:
PR_TITLE: ${{ github.event.pull_request.title }}
id: increment_version
run: |
current_version=${LATEST_TAG#"v"}
echo "CURRENT_VERSION=$current_version" >> $GITHUB_ENV
IFS='.' read -ra version_parts <<< "$current_version"
major=${version_parts[0]}
minor=${version_parts[1]}
patch=${version_parts[2]}
patch=$(echo $patch | cut -d'-' -f1)
echo The Title of your PR is $PR_TITLE
short_commit_hash=$(git rev-parse --short HEAD)
if [[ $PR_TITLE == *"[major]"* ]]; then
major=$(( major + 1 ))
minor=0
patch=0
elif [[ $PR_TITLE == *"[minor]"* ]]; then
minor=$(( minor + 1 ))
patch=0
else
patch=$(( patch + 1 ))
fi
new_version="$major.$minor.$patch-$short_commit_hash"
echo "Output new_version: [$new_version]"
echo "Output ecr_url: [${{ secrets.ECR_URL }}]"
echo "NEW_VERSION=$new_version" >> $GITHUB_OUTPUT
echo "ECR_URL=${{ secrets.ECR_URL }}" >> $GITHUB_OUTPUT
- name: Create and push new tag to Github
run: |
git config --global user.name 'GitHub Actions'
git config --global user.email 'actions@github.com'
git tag v${{ steps.increment_version.outputs.NEW_VERSION }}
git push origin v${{ steps.increment_version.outputs.NEW_VERSION }}
pushImageToEcr:
needs: pushNewTag
name: Push image to AWS-ECR
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Corretto openJDK 17
uses: actions/setup-java@v3 # check specific version in https://github.com/actions/setup-java
with:
distribution: 'corretto' # using Amazon openJDK
java-version: '17'
- name: Gradle caching
uses: actions/cache@v3
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
- name: Build project and create Dockerfiles
run: ./gradlew build --daemon --parallel -Pversion=${{ needs.pushNewTag.outputs.NEW_VERSION }}
- name: Build docker images
run: docker-compose -f docker-compose-prod.yaml build
- 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: ap-northeast-2
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Grant execute permission for push_to_ecr.sh
run: chmod +x ./push_to_ecr.sh
- name: Tag Push images to ECR
env:
ECR_URL: ${{ env.ECR_URL }}
run: ./push_to_ecr.sh ${ECR_URL} ${{ needs.pushNewTag.outputs.NEW_VERSION }}
DeployToEKSCluster:
name: Deplyo to AWS EKS Cluster
needs: [pushImageToEcr,pushNewTag]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
name: Code checkout
- name: Install kubectl
uses: azure/setup-kubectl@v3.0 # default is latest stable
id: install
- 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: ap-northeast-2
- name: Update kube config
run: |
aws eks update-kubeconfig --name spring-chat-k8s-cluster --region ap-northeast-2
kubectl config current-context
kubectl get nodes -o wide
- name: Deploy to AWS EKS Cluster
run: |
echo "connect to ECR: ${{ secrets.ECR_URL }}"
echo "new version will be deployed ${{ needs.pushNewTag.outputs.NEW_VERSION }}"
cd k8s/onlychat/deployment
sh write_image_to_deploy.sh ${{ secrets.ECR_URL }} ap-northeast-2 ${{ needs.pushNewTag.outputs.NEW_VERSION }}
cd ..
kubectl apply -f ./hpa/
kubectl apply -f ./eks/
kubectl apply -f redis.yaml
kubectl apply -f ./volume/
kubectl apply -f ./namespace/
kubectl apply -f ./service/
kubectl apply -f ./deployment/
pushSlack:
needs: [pushImageToEcr, pushNewTag, DeployToEKSCluster]
if: always()
name: Slack Alert
runs-on: ubuntu-latest
steps:
- uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
fields: job,took,author
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}