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:mariadb://${{ 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+ echo "spring.data.redis.host=${{ secrets.REDIS_URL }}" >> ./src/main/resources/application-prod.properties
64+
65+ - name : Build with Gradle Wrapper
66+ # run: ./gradlew test -i
67+ run : ./gradlew build
68+
69+ - name : Upload jar file to Artifact
70+ uses : actions/upload-artifact@v4
71+ with :
72+ name : jar_files
73+ path : build/libs/*.jar
74+
75+ - name : Upload Dockerfile to Artifact
76+ uses : actions/upload-artifact@v4
77+ with :
78+ name : Dockerfile
79+ path : ./Dockerfile
80+
81+ CD_Delivery :
82+ name : Delivery
83+ needs : CI
84+ runs-on : ubuntu-latest
85+
86+ permissions :
87+ contents : read
88+
89+ steps :
90+ - name : Download jar file from Artifact
91+ uses : actions/download-artifact@v4
92+ with :
93+ name : jar_files
94+ path : build/libs
95+
96+ - name : Download Dockerfile file from Artifact
97+ uses : actions/download-artifact@v4
98+ with :
99+ name : Dockerfile
100+ path : ./
101+
102+ - name : Log in to Docker Hub
103+ uses : docker/login-action@v3
104+ with :
105+ username : ${{ secrets.DOCKER_USERNAME }}
106+ password : ${{ secrets.DOCKER_PASSWORD }}
107+
108+ - name : Get short SHA
109+ id : slug
110+ run : echo "sha7=$(echo ${GITHUB_SHA} | cut -c1-7)" >> $GITHUB_OUTPUT
111+
112+ - name : Build, tag, and push image to DockerHub
113+ id : build-image
114+ env :
115+ USERNAME : ${{ secrets.DOCKER_USERNAME }}
116+ IMAGE_TAG : ${{ steps.slug.outputs.sha7 }}
117+
118+ run : |
119+ docker build -t $USERNAME/$DOCKERHUB_REPOSITORY:$IMAGE_TAG -t $USERNAME/$DOCKERHUB_REPOSITORY:latest .
120+ docker push $USERNAME/$DOCKERHUB_REPOSITORY --all-tags
121+ echo "image=$USERNAME/$DOCKERHUB_REPOSITORY:$IMAGE_TAG&latest" >> $GITHUB_OUTPUT
122+
123+ CD_Deploy :
124+ name : Deploy
125+ needs : CD_Delivery
126+ runs-on : ubuntu-latest
127+
128+ steps :
129+ - name : Get short SHA
130+ id : slug
131+ run : echo "sha7=$(echo ${GITHUB_SHA} | cut -c1-7)" >> $GITHUB_OUTPUT
132+
133+ - name : Executing remote ssh commands
134+ uses :
appleboy/[email protected] # ssh 접속하는 오픈소스 135+ with :
136+ host : ${{ secrets.REMOTE_IP }} # 인스턴스 IP
137+ username : ${{ secrets.REMOTE_USER }} # 우분투 아이디
138+ key : ${{ secrets.REMOTE_PRIVATE_KEY }} # ec2 instance pem key
139+ port : ${{ secrets.REMOTE_SSH_PORT }} # 접속포트
140+ script : | # 실행할 스크립트
141+ cd /home/ubuntu/cicd/scripts
142+ ./rolling-update.sh
143+
144+ - name : Discord Webhook Action
145+ 146+ with :
147+ webhook-url : ${{ secrets.DISCORD_WEBHOOK_URL }}
148+ content : |
149+ :o: Server successfully updated!
150+ Commit: [${{ github.sha }}]
151+ Branch: ${{ github.ref_name }}
152+ Commit Link: https://github.com/${{ github.repository }}/commit/${{ github.sha }}
153+ GitHub Action Link: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
154+
155+ failure_notification :
156+ name : Failure Notification
157+ runs-on : ubuntu-latest
158+ needs : [CI, CD_Delivery, CD_Deploy]
159+ if : failure()
160+
161+ steps :
162+ - name : Discord Webhook Action on Failure
163+ 164+ with :
165+ webhook-url : ${{ secrets.DISCORD_WEBHOOK_URL }}
166+ content : |
167+ :x: A job failed in the CI/CD pipeline!
168+ Commit: [${{ github.sha }}]
169+ Branch: ${{ github.ref_name }}
170+ Commit Link: https://github.com/${{ github.repository }}/commit/${{ github.sha }}
171+ GitHub Action Link: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
172+ Please check the logs for more details.
0 commit comments