-
Notifications
You must be signed in to change notification settings - Fork 0
147 lines (128 loc) · 4.47 KB
/
ci.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
name: Continuous Integration
# 워크플로우 트리거 설정
on:
push:
branches: [main]
paths-ignore:
- '.github/**'
workflow_dispatch:
inputs:
version_type:
description: '버전 증가 유형'
required: true
default: 'minor'
type: choice
options:
- minor
- major
# 환경 변수 설정
env:
AWS_REGION: ap-northeast-2
ECR_REPOSITORY: threedays-app
jobs:
build:
runs-on: ubuntu-latest
steps:
# 저장소 체크아웃
- name: Checkout repository
uses: actions/checkout@v4
with:
submodules: 'recursive'
fetch-depth: 0
# JDK 21 설정
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'liberica'
# Gradle 패키지 캐싱
- name: Cache Gradle packages
uses: actions/cache@v4
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
restore-keys: ${{ runner.os }}-gradle
# AWS 자격 증명 구성
- 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 }}
# Amazon ECR 로그인
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
# 버전 태그 생성
- name: Generate version tag
id: generate-version
run: |
# 최신 태그 가져오기
latest_tag="$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")"
echo "최신 태그: $latest_tag"
# major, minor, patch 추출
IFS='.' read -r major minor patch <<< "${latest_tag#v}"
# 버전 증가 유형 결정
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
increment_type="${{ github.event.inputs.version_type }}"
else
increment_type="patch"
fi
# 버전 증가 로직
case $increment_type in
major)
new_major=$((major + 1))
new_minor=0
new_patch=0
;;
minor)
new_major=$major
new_minor=$((minor + 1))
new_patch=0
;;
patch|*)
new_major=$major
new_minor=$minor
new_patch=$((patch + 1))
;;
esac
# 새 버전 태그 생성
new_version="v$new_major.$new_minor.$new_patch"
# 새 버전이 최신 태그보다 큰지 확인
if [ $(echo "$new_version $latest_tag" | tr ' ' '\n' | sort -V | tail -n 1) != "$new_version" ]; then
echo "오류: 새 버전($new_version)이 최신 태그($latest_tag)보다 크지 않습니다."
exit 1
fi
# 결과 출력
echo "version=$new_version" >> $GITHUB_OUTPUT
echo "새 버전: $new_version"
# Amazon ECR에 이미지 빌드 및 푸시
- name: Build and push image to Amazon ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
VERSION_TAG: ${{ steps.generate-version.outputs.version }}
run: |
./gradlew jib \
-Djib.to.image="$ECR_REGISTRY/$ECR_REPOSITORY:$VERSION_TAG" \
-Djib.to.tags="latest" \
-Djib.console=plain
# 이미지 정보 저장
- name: Save image info
run: |
echo "${{ steps.login-ecr.outputs.registry }}/$ECR_REPOSITORY:${{ steps.generate-version.outputs.version }}" > image_info.txt
# 아티팩트 업로드
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: image-info
path: image_info.txt
# Git 태그 생성
- name: Create Git tag
if: github.event_name == 'workflow_dispatch' || (github.event_name == 'push' && github.ref == 'refs/heads/main')
run: |
git config user.name github-actions
git config user.email github-actions@github.com
git tag ${{ steps.generate-version.outputs.version }}
git push origin ${{ steps.generate-version.outputs.version }}
env:
GITHUB_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}