1+ name : CI/CD FOR DEVELOP
2+
3+ on :
4+ push :
5+ branches :
6+ - develop
7+
8+ env :
9+ DOCKERHUB_REPOSITORY : fontory-server
10+
11+ jobs :
12+ CI :
13+ name : Continuous Integration
14+ runs-on : ubuntu-latest
15+ permissions :
16+ contents : read
17+
18+ steps :
19+ - name : Get short SHA
20+ id : slug
21+ run : echo "sha7=$(echo ${GITHUB_SHA} | cut -c1-7)" >> $GITHUB_OUTPUT
22+
23+ - name : Discord Webhook Action
24+ 25+ with :
26+ webhook-url : ${{ secrets.DISCORD_WEBHOOK_URL }}
27+ content : |
28+ New Commit[${{ steps.slug.outputs.sha7 }}] detected on branch ${{ github.ref_name }}
29+ Commit Link: https://github.com/${{ github.repository }}/commit/${{ github.sha }}
30+ GitHub Action Link: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
31+
32+ - name : Checkout
33+ uses : actions/checkout@v4
34+
35+ - name : Set up JDK 17
36+ uses : actions/setup-java@v4
37+ with :
38+ java-version : ' 17'
39+ distribution : ' temurin'
40+
41+ - name : Setup MySQL
42+ uses :
mirromutth/[email protected] 43+ with :
44+ host port : 3308
45+ mysql database : ' TESTDB'
46+ mysql user : ' fontory'
47+ mysql password : ' fontoryPW'
48+
49+ - name : Setup Redis
50+ uses :
supercharge/[email protected] 51+ with :
52+ redis-version : 6
53+
54+ - name : Setup Gradle
55+ uses : gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0
56+
57+ - name : Generate application.properties
58+ run : |
59+ echo "commit.hash=${{ steps.slug.outputs.sha7 }}" >> ./src/main/resources/application-prod.properties
60+ echo "spring.datasource.url=jdbc:mysql://${{ secrets.DATASOURCE_DB_URL }}:3306/FONTORY?characterEncoding=UTF-8&serverTimezone=Asia/Seoul" >> ./src/main/resources/application-prod.properties
61+ echo "spring.datasource.username=${{ secrets.DATASOURCE_DB_USERNAME }}" >> ./src/main/resources/application-prod.properties
62+ echo "spring.datasource.password=${{ secrets.DATASOURCE_DB_PASSWORD }}" >> ./src/main/resources/application-prod.properties
63+
64+ - name : Build with Gradle Wrapper
65+ # run: ./gradlew test -i
66+ run : ./gradlew build
67+
68+ - name : Upload jar file to Artifact
69+ uses : actions/upload-artifact@v4
70+ with :
71+ name : jar_files
72+ path : build/libs/*.jar
73+
74+ - name : Upload Dockerfile to Artifact
75+ uses : actions/upload-artifact@v4
76+ with :
77+ name : Dockerfile
78+ path : ./Dockerfile
79+
80+ CD_Delivery :
81+ name : Delivery
82+ needs : CI
83+ runs-on : ubuntu-latest
84+
85+ permissions :
86+ contents : read
87+
88+ steps :
89+ - name : Download jar file from Artifact
90+ uses : actions/download-artifact@v4
91+ with :
92+ name : jar_files
93+ path : build/libs
94+
95+ - name : Download Dockerfile file from Artifact
96+ uses : actions/download-artifact@v4
97+ with :
98+ name : Dockerfile
99+ path : ./
100+
101+ - name : Log in to Docker Hub
102+ uses : docker/login-action@v3
103+ with :
104+ username : ${{ secrets.DOCKER_USERNAME }}
105+ password : ${{ secrets.DOCKER_PASSWORD }}
106+
107+ - name : Get short SHA
108+ id : slug
109+ run : echo "sha7=$(echo ${GITHUB_SHA} | cut -c1-7)" >> $GITHUB_OUTPUT
110+
111+ - name : Build, tag, and push image to DockerHub
112+ id : build-image
113+ env :
114+ USERNAME : ${{ secrets.DOCKER_USERNAME }}
115+ IMAGE_TAG : ${{ steps.slug.outputs.sha7 }}
116+
117+ run : |
118+ docker build -t $USERNAME/$DOCKERHUB_REPOSITORY:$IMAGE_TAG -t $USERNAME/$DOCKERHUB_REPOSITORY:latest .
119+ docker push $USERNAME/$DOCKERHUB_REPOSITORY --all-tags
120+ echo "image=$USERNAME/$DOCKERHUB_REPOSITORY:$IMAGE_TAG&latest" >> $GITHUB_OUTPUT
121+
122+ CD_Deploy :
123+ name : Deploy
124+ needs : CD_Delivery
125+ runs-on : ubuntu-latest
126+
127+ steps :
128+ - name : Get short SHA
129+ id : slug
130+ run : echo "sha7=$(echo ${GITHUB_SHA} | cut -c1-7)" >> $GITHUB_OUTPUT
131+
132+ - name : Executing remote ssh commands
133+ uses :
appleboy/[email protected] # ssh 접속하는 오픈소스 134+ with :
135+ host : ${{ secrets.REMOTE_IP }} # 인스턴스 IP
136+ username : ${{ secrets.REMOTE_USER }} # 우분투 아이디
137+ key : ${{ secrets.REMOTE_PRIVATE_KEY }} # ec2 instance pem key
138+ port : ${{ secrets.REMOTE_SSH_PORT }} # 접속포트
139+ script : | # 실행할 스크립트
140+ cd /home/ubuntu/cicd/scripts
141+ ./rolling-update.sh
142+
143+ - name : Discord Webhook Action
144+ 145+ with :
146+ webhook-url : ${{ secrets.DISCORD_WEBHOOK_URL }}
147+ content : |
148+ :o: Server successfully updated!
149+ Commit: [${{ github.sha }}]
150+ Branch: ${{ github.ref_name }}
151+ Commit Link: https://github.com/${{ github.repository }}/commit/${{ github.sha }}
152+ GitHub Action Link: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
153+
154+ failure_notification :
155+ name : Failure Notification
156+ runs-on : ubuntu-latest
157+ needs : [CI, CD_Delivery, CD_Deploy]
158+ if : failure()
159+
160+ steps :
161+ - name : Discord Webhook Action on Failure
162+ 163+ with :
164+ webhook-url : ${{ secrets.DISCORD_WEBHOOK_URL }}
165+ content : |
166+ :x: A job failed in the CI/CD pipeline!
167+ Commit: [${{ github.sha }}]
168+ Branch: ${{ github.ref_name }}
169+ Commit Link: https://github.com/${{ github.repository }}/commit/${{ github.sha }}
170+ GitHub Action Link: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
171+ Please check the logs for more details.
0 commit comments