-
Notifications
You must be signed in to change notification settings - Fork 1
147 lines (136 loc) · 5.06 KB
/
gradle.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
name: Backend CI / CD
# master 브랜치에 push 되면 workflow 작동
on:
push:
branches:
- "main"
env:
AWS_REGION: ap-northeast-2
ECR_REPOSITORY_NAME: "team-4"
ENV_NAME: "prod"
APP: "686710509719.dkr.ecr.ap-northeast-2.amazonaws.com/team-4"
jobs:
build-and-push-image:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
# 1. Java 17 세팅
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew clean build -x test
# Github Secret과 local env 에 저장된 값 불러오기
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
# access key와 secret를 사용하여 ECR에 push할 수 있도록 인증
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
# Dockerfile 을 기준으로 이미지 빌드 후 ecr로 push
- name: Set up Docker Buildx
id: docker_build
uses: docker/setup-buildx-action@v2
- name: main Environment Docker build & Push Docker image to Amazon ECR
uses: docker/build-push-action@v2
if: contains(github.ref, 'main')
env:
ECR: ${{ steps.login-ecr.outputs.registry }}
TAG: ${{ github.sha }}
with:
context: .
push: true
tags: |
${{env.ECR}}/${{env.ECR_REPOSITORY_NAME}}:back-${{env.TAG}}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
ENV=main
- name: Checkout for Kustomize repository
uses: actions/checkout@v2
with:
# kubernetes yaml 파일 저장
repository: PDA4-Phoenix/kkanbustock-server-cicd # k8s yaml 파일이 있는 repo
ref: main # branch 이름
# 내 repository에 push 하기 위한 Personal Access Token이 필요
token: ${{ secrets.ACTION_TOKEN }} # Github Action token을 발급받아서 repo secrect에 등록해줘야한다
path: kkanbustock-server-cicd # 최상위 경로로 repository와 동일하게 설정.
# 새 이미지 버전으로 파일의 태그값 수정.
# cd path 수정
# kustomize 로 image tag 값 변경.
- name: Update Kubernetes resources
run: |
pwd
cd kkanbustock-server-cicd/overlays/${{ env.ENV_NAME }}/
kustomize edit set image ${{ env.APP }}:back-${{ github.sha }}
# 수정된 kustomization.yaml 파일 commit push
- name: Commit minifest files
run: |
cd kkanbustock-server-cicd
git config --global user.email "[email protected]"
git config --global user.name "ghdeo"
git commit -am "Update image"
git push -u origin main
# build-docker-image:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v3
# # 1. Java 17 세팅
# - name: Set up JDK 17
# uses: actions/setup-java@v3
# with:
# java-version: '17'
# distribution: 'temurin'
#
# # 2. Spring Boot 애플리케이션 빌드
# - name: Build with Gradle
# uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1
# with:
# arguments: clean bootJar
#
# # 3. Docker 이미지 빌드
# - name: docker image build
# run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/kkanbustock-v0 .
#
# # 4. DockerHub 로그인
# - name: docker login
# uses: docker/login-action@v2
# with:
# username: ${{ secrets.DOCKERHUB_USERNAME }}
# password: ${{ secrets.DOCKERHUB_PASSWORD }}
#
# # 5. Docker Hub 이미지 푸시
# - name: docker Hub push
# run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/kkanbustock-v0
#
# # 위 과정에서 푸시한 이미지를 ec2에서 풀받아서 실행시키는 과정
# run-docker-image-on-ec2:
# # build-docker-image (위)과정이 완료되어야 실행됩니다.
# needs: build-docker-image
# runs-on: self-hosted
#
# steps:
# # 1. 최신 이미지를 풀받습니다
# - name: docker pull
# run: sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/kkanbustock-v0
#
# # 2. 기존의 컨테이너를 중지시킵니다
# - name: docker stop container
# run: sudo docker stop $(sudo docker ps -q) 2>/dev/null || true
#
# # 3. 최신 이미지를 컨테이너화하여 실행시킵니다
# - name: docker run new container
# run: sudo docker run --name github-actions-demo --rm -d -p 8080:8080 ${{ secrets.DOCKERHUB_USERNAME }}/kkanbustock-v0
#
# # 4. 미사용 이미지를 정리합니다
# - name: delete old docker image
# run: sudo docker system prune -f