Skip to content

Commit 45c048e

Browse files
committed
chore: 1차 배포 스크립트 완성
1 parent fe415c0 commit 45c048e

File tree

1 file changed

+146
-61
lines changed

1 file changed

+146
-61
lines changed

.github/workflows/cicd.yml

Lines changed: 146 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,155 @@ name: cicd
22

33
on:
44
push:
5-
branches: [ "main" ]
5+
branches: [ "main", "dev", "deploy/cicd" ]
66

77
jobs:
88
build:
99
runs-on: ubuntu-latest
1010
steps:
11-
# - uses: actions/checkout@v4
12-
13-
# 이 아래 애들은 deploy 스텝에서 적용해야될듯?
14-
- name: Get Gihub Actions VM IP
15-
id: ip
16-
uses: haythem/public-ip@v1.3
17-
18-
- name: Print VM IP
19-
run: |
20-
echo ${{ steps.ip.outputs.ipv4 }}
21-
22-
- name: Configure AWS credentials
23-
uses: aws-actions/configure-aws-credentials@v4
24-
with:
25-
aws-access-key-id: ${{ secrets.AWS_IAM_ACCESS_KEY }}
26-
aws-secret-access-key: ${{ secrets.AWS_IAM_ACCESS_SECRET_KEY }}
27-
aws-region: 'ap-northeast-2'
28-
29-
- name: Add GitHub Actions IP
30-
run: |
31-
aws ec2 authorize-security-group-ingress \
32-
--group-id ${{ secrets.AWS_SECURITY_GROUP_ID }} \
33-
--protocol tcp \
34-
--port 22 \
35-
--cidr ${{ steps.ip.outputs.ipv4 }}/32
36-
37-
- name: Test EC2 Connection And Manipulation
38-
uses: appleboy/ssh-action@master
39-
with:
40-
username: ubuntu
41-
host: ${{ secrets.EC2_ELASTIC_IP }}
42-
key: ${{ secrets.EC2_SSH_KEY }}
43-
script_stop: true
44-
script: |
45-
echo "[✔] GitHub Actions Connection Success" | tee -a ~/cicd-log.txt
46-
echo "Time: $(date '+%Y-%m-%d %H:%M:%S')" | tee -a ~/cicd-log.txt
47-
echo "Branch: ${{ github.ref_name }}" | tee -a ~/cicd-log.txt
48-
echo "Commit: ${{ github.sha }}" | tee -a ~/cicd-log.txt
49-
echo "-------------------------------" | tee -a ~/cicd-log.txt
50-
51-
- name: Remove GitHub Actions IP
52-
run: |
53-
aws ec2 revoke-security-group-ingress \
54-
--group-id ${{ secrets.AWS_SECURITY_GROUP_ID }} \
55-
--protocol tcp \
56-
--port 22 \
57-
--cidr ${{ steps.ip.outputs.ipv4 }}/32
58-
59-
# uses: actions/setup-java@v4
60-
# with:
61-
# java-version: '17'
62-
# distribution: 'temurin'
63-
64-
# Configure Gradle for optimal use in GitHub Actions, including caching of downloaded dependencies.
65-
# See: https://github.com/gradle/actions/blob/main/setup-gradle/README.md
66-
# - name: Setup Gradle
67-
# uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0
68-
69-
# - name: Build with Gradle Wrapper
70-
# run: ./gradlew build
11+
- uses: actions/checkout@v3
12+
- name: Install JDK 21
13+
uses: actions/setup-java@v3
14+
with:
15+
java-version: '21'
16+
distribution: 'temurin'
17+
18+
- name: Build with Gradle And Test
19+
run: |
20+
echo ${{ secrets.APPLICATION_YML }} | base64 --decode > ./src/main/resources/application.yml
21+
22+
if [ -s ./src/main/resources/application.yml ]; then
23+
echo "application.yml Create Success"
24+
else
25+
echo "application.yml Create Fail"
26+
exit 1
27+
fi
28+
29+
chmod +x ./gradlew
30+
./gradlew clean build
31+
32+
- name: Login to DockerHub
33+
uses: docker/login-action@v1
34+
with:
35+
username: ${{ secrets.DOCKERHUB_USERNAME }}
36+
password: ${{ secrets.DOCKERHUB_ACCESS_TOKEN }}
37+
38+
- name: Build Docker
39+
run: docker build --platform linux/amd64 -t ${{ secrets.DOCKERHUB_USERNAME }}/saegil .
40+
41+
- name: Push Docker
42+
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/saegil:latest
43+
44+
deploy:
45+
needs: build
46+
runs-on: ubuntu-latest
47+
steps:
48+
- name: Get Github Actions VM IP
49+
id: ip
50+
uses: haythem/public-ip@v1.3
51+
52+
- name: Print VM IP
53+
run: |
54+
echo ${{ steps.ip.outputs.ipv4 }}
55+
56+
- name: Configure AWS credentials
57+
uses: aws-actions/configure-aws-credentials@v4
58+
with:
59+
aws-access-key-id: ${{ secrets.AWS_IAM_ACCESS_KEY }}
60+
aws-secret-access-key: ${{ secrets.AWS_IAM_ACCESS_SECRET_KEY }}
61+
aws-region: 'ap-northeast-2'
62+
63+
- name: Add GitHub Actions IP
64+
run: |
65+
aws ec2 authorize-security-group-ingress \
66+
--group-id ${{ secrets.AWS_SECURITY_GROUP_ID }} \
67+
--protocol tcp \
68+
--port 22 \
69+
--cidr ${{ steps.ip.outputs.ipv4 }}/32
70+
71+
- name: Test EC2 Connection And Manipulation
72+
uses: appleboy/ssh-action@master
73+
with:
74+
username: ubuntu
75+
host: ${{ secrets.EC2_ELASTIC_IP }}
76+
key: ${{ secrets.EC2_SSH_KEY }}
77+
script_stop: true
78+
script: |
79+
echo "[✔] GitHub Actions Connection Success" | tee -a ~/cicd-log.txt
80+
echo "Time: $(date '+%Y-%m-%d %H:%M:%S')" | tee -a ~/cicd-log.txt
81+
echo "Branch: ${{ github.ref_name }}" | tee -a ~/cicd-log.txt
82+
echo "Commit: ${{ github.sha }}" | tee -a ~/cicd-log.txt
83+
echo "-------------------------------" | tee -a ~/cicd-log.txt
84+
85+
- name: Set target IP
86+
run: |
87+
STATUS=$(curl -o /dev/null -w "%{http_code}" "https://${{ secrets.EC2_HTTPS_DOMAIN }}/env")
88+
echo $STATUS
89+
if [ $STATUS = 200 ]; then
90+
CURRENT_UPSTREAM=$(curl -s "https://${{ secrets.EC2_HTTPS_DOMAIN }}/env")
91+
else
92+
CURRENT_UPSTREAM=green
93+
fi
94+
echo CURRENT_UPSTREAM=$CURRENT_UPSTREAM >> $GITHUB_ENV
95+
if [ $CURRENT_UPSTREAM = blue ]; then
96+
echo "CURRENT_PORT=8080" >> $GITHUB_ENV
97+
echo "STOPPED_PORT=8081" >> $GITHUB_ENV
98+
echo "TARGET_UPSTREAM=green" >> $GITHUB_ENV
99+
elif [ $CURRENT_UPSTREAM = green ]; then
100+
echo "CURRENT_PORT=8081" >> $GITHUB_ENV
101+
echo "STOPPED_PORT=8080" >> $GITHUB_ENV
102+
echo "TARGET_UPSTREAM=blue" >> $GITHUB_ENV
103+
else
104+
echo "error"
105+
exit 1
106+
fi
107+
108+
- name: Docker compose
109+
uses: appleboy/ssh-action@master
110+
with:
111+
username: ubuntu
112+
host: ${{ secrets.EC2_ELASTIC_IP }}
113+
key: ${{ secrets.EC2_SSH_KEY }}
114+
script_stop: true
115+
script: |
116+
sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/saegil:latest
117+
sudo docker-compose -f docker-compose-${{env.TARGET_UPSTREAM}}.yml up -d
118+
119+
- name: Check deploy server URL
120+
uses: jtalk/url-health-check-action@v3
121+
with:
122+
url: https://${{ secrets.EC2_HTTPS_DOMAIN }}:${{env.STOPPED_PORT}}/env
123+
max-attempts: 5
124+
retry-delay: 10s
125+
126+
- name: Change nginx upstream (local nginx)
127+
uses: appleboy/ssh-action@master
128+
with:
129+
username: ubuntu
130+
host: ${{ secrets.EC2_ELASTIC_IP }}
131+
key: ${{ secrets.EC2_SSH_KEY }}
132+
script_stop: true
133+
script: |
134+
echo "set \$service_url ${{ env.TARGET_UPSTREAM }};" | sudo tee /etc/nginx/conf.d/service-env.inc
135+
sudo nginx -s reload
136+
137+
- name: Stop current server
138+
uses: appleboy/ssh-action@master
139+
with:
140+
username: ubuntu
141+
host: ${{ secrets.EC2_ELASTIC_IP }}
142+
key: ${{ secrets.EC2_SSH_KEY }}
143+
script_stop: true
144+
script: |
145+
sudo docker stop ${{env.CURRENT_UPSTREAM}}
146+
sudo docker rm ${{env.CURRENT_UPSTREAM}}
147+
sudo docker image prune -a -f
148+
149+
- name: Remove GitHub Actions IP
150+
run: |
151+
aws ec2 revoke-security-group-ingress \
152+
--group-id ${{ secrets.AWS_SECURITY_GROUP_ID }} \
153+
--protocol tcp \
154+
--port 22 \
155+
--cidr ${{ steps.ip.outputs.ipv4 }}/32
71156

0 commit comments

Comments
 (0)