-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
61 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,117 +56,98 @@ jobs: | |
source: "./splanet-firebase.json" | ||
target: "/home/ubuntu/" | ||
|
||
- name: Build and Push Docker image | ||
run: docker buildx build --push --platform linux/amd64 -t kimsongmok/splanet:${{ env.IMAGE_TAG }} . | ||
|
||
- name: Determine Current Version | ||
id: current_version | ||
uses: appleboy/[email protected] | ||
with: | ||
host: ${{ secrets.EC2_HOST }} | ||
username: ubuntu | ||
key: ${{ secrets.EC2_SSH_KEY }} | ||
script: | | ||
id: determine_current_version | ||
run: | | ||
VERSION=$(ssh -i ${{ secrets.EC2_SSH_KEY }} ubuntu@${{ secrets.EC2_HOST }} ' | ||
if sudo docker ps --filter "name=splanet_blue" --format "{{.Names}}" | grep -q "splanet_blue"; then | ||
echo "blue" > current_version.txt | ||
echo "blue" | ||
elif sudo docker ps --filter "name=splanet_green" --format "{{.Names}}" | grep -q "splanet_green"; then | ||
echo "green" > current_version.txt | ||
echo "green" | ||
else | ||
echo "none" > current_version.txt | ||
echo "none" | ||
fi | ||
cat current_version.txt | ||
') | ||
echo "version=$VERSION" >> $GITHUB_OUTPUT | ||
- name: Set Current Version | ||
run: echo "CURRENT_VERSION=${{ steps.determine_current_version.outputs.version }}" >> $GITHUB_ENV | ||
|
||
- name: Determine New Version | ||
run: | | ||
CURRENT_VERSION=$(cat current_version.txt) | ||
echo "현재 버전은 $CURRENT_VERSION 입니다" | ||
if [ "$CURRENT_VERSION" == "blue" ]; then | ||
echo "NEW_VERSION=green" > new_version.txt | ||
echo "NEW_PORT=8081" >> new_version.txt | ||
if [ "${{ env.CURRENT_VERSION }}" == "blue" ]; then | ||
echo "NEW_VERSION=green" >> $GITHUB_ENV | ||
echo "NEW_PORT=8081" >> $GITHUB_ENV | ||
elif [ "${{ env.CURRENT_VERSION }}" == "green" ]; then | ||
echo "NEW_VERSION=blue" >> $GITHUB_ENV | ||
echo "NEW_PORT=8080" >> $GITHUB_ENV | ||
else | ||
echo "NEW_VERSION=blue" > new_version.txt | ||
echo "NEW_PORT=8080" >> new_version.txt | ||
echo "NEW_VERSION=blue" >> $GITHUB_ENV | ||
echo "NEW_PORT=8080" >> $GITHUB_ENV | ||
fi | ||
cat new_version.txt | ||
- name: Print Current and New Version | ||
run: | | ||
echo "Current Version: $CURRENT_VERSION" | ||
echo "New Version: $NEW_VERSION" | ||
echo "New Port: $NEW_PORT" | ||
- name: Deploy New Version to EC2 | ||
uses: appleboy/[email protected] | ||
with: | ||
host: ${{ secrets.EC2_HOST }} | ||
username: ubuntu | ||
key: ${{ secrets.EC2_SSH_KEY }} | ||
script: | | ||
source new_version.txt # NEW_VERSION과 NEW_PORT 값을 불러옵니다 | ||
echo "새 버전 $NEW_VERSION, 포트 $NEW_PORT 에 배포를 시작합니다" | ||
run: | | ||
ssh -i ${{ secrets.EC2_SSH_KEY }} ubuntu@${{ secrets.EC2_HOST }} " | ||
sudo docker pull kimsongmok/splanet:${{ env.IMAGE_TAG }} | ||
sudo docker network inspect splanet >/dev/null 2>&1 || sudo docker network create splanet | ||
sudo docker run -d --name splanet_$NEW_VERSION \ | ||
sudo docker run -d --name splanet_${{ env.NEW_VERSION }} \ | ||
--network splanet \ | ||
--env-file /home/ubuntu/env.properties \ | ||
-p $NEW_PORT:8080 --restart unless-stopped kimsongmok/splanet:${{ env.IMAGE_TAG }} | ||
-p ${{ env.NEW_PORT }}:8080 --restart unless-stopped kimsongmok/splanet:${{ env.IMAGE_TAG }} | ||
" | ||
- name: Wait for Spring Boot Application to Start | ||
uses: appleboy/[email protected] | ||
with: | ||
host: ${{ secrets.EC2_HOST }} | ||
username: ubuntu | ||
key: ${{ secrets.EC2_SSH_KEY }} | ||
envs: | | ||
NEW_PORT=${{ env.NEW_PORT }} | ||
NEW_VERSION=${{ env.NEW_VERSION }} | ||
script: | | ||
echo "Waiting for the application to be healthy on http://api.splanet.co.kr:${NEW_PORT}/actuator/health..." | ||
run: | | ||
ssh -i ${{ secrets.EC2_SSH_KEY }} ubuntu@${{ secrets.EC2_HOST }} " | ||
echo 'Waiting for the application to be healthy on http://api.splanet.co.kr:${NEW_PORT}/actuator/health...' | ||
for i in {1..30}; do | ||
if curl -s http://api.splanet.co.kr:${NEW_PORT}/actuator/health | grep '"status":"UP"' > /dev/null; then | ||
echo "Application is healthy and ready to receive traffic." | ||
if curl -s http://api.splanet.co.kr:${NEW_PORT}/actuator/health | grep '\"status\":\"UP\"' > /dev/null; then | ||
echo 'Application is healthy and ready to receive traffic.' | ||
break | ||
fi | ||
echo "Waiting for application to start... (Attempt $i)" | ||
echo 'Waiting for application to start... (Attempt $i)' | ||
sleep 5 | ||
done | ||
if [ "$i" -eq 30 ]; then | ||
echo "Application did not start successfully within the expected time." | ||
if [ \"\$i\" -eq 30 ]; then | ||
echo 'Application did not start successfully within the expected time.' | ||
sudo docker logs splanet_${NEW_VERSION} | ||
exit 1 | ||
fi | ||
" | ||
- name: Update Load Balancer Target Group | ||
uses: appleboy/[email protected] | ||
with: | ||
host: ${{ secrets.EC2_HOST }} | ||
username: ubuntu | ||
key: ${{ secrets.EC2_SSH_KEY }} | ||
envs: | | ||
CURRENT_VERSION=${{ steps.current_version.outputs.version }} | ||
script: | | ||
if [ "$CURRENT_VERSION" == "blue" ]; then | ||
TARGET_GROUP_ARN="arn:aws:elasticloadbalancing:ap-northeast-2:${{ secrets.AWS_ACCOUNT_ID }}:targetgroup/splanet-8081/${{ secrets.ARN_ID_8081 }}" | ||
else | ||
TARGET_GROUP_ARN="arn:aws:elasticloadbalancing:ap-northeast-2:${{ secrets.AWS_ACCOUNT_ID }}:targetgroup/splanet/${{ secrets.ARN_ID_8080 }}" | ||
fi | ||
aws elbv2 modify-listener --listener-arn arn:aws:elasticloadbalancing:ap-northeast-2:${{ secrets.AWS_ACCOUNT_ID }}:listener/app/splanet/${{ secrets.ARN_ID_443 }} \ | ||
--default-actions Type=forward,TargetGroupArn=$TARGET_GROUP_ARN | ||
run: | | ||
if [ "${{ env.CURRENT_VERSION }}" == "blue" ]; then | ||
TARGET_GROUP_ARN="arn:aws:elasticloadbalancing:ap-northeast-2:${{ secrets.AWS_ACCOUNT_ID }}:targetgroup/splanet-8081/${{ secrets.ARN_ID_8081 }}" | ||
else | ||
TARGET_GROUP_ARN="arn:aws:elasticloadbalancing:ap-northeast-2:${{ secrets.AWS_ACCOUNT_ID }}:targetgroup/splanet/${{ secrets.ARN_ID_8080 }}" | ||
fi | ||
aws elbv2 modify-listener --listener-arn arn:aws:elasticloadbalancing:ap-northeast-2:${{ secrets.AWS_ACCOUNT_ID }}:listener/app/splanet/${{ secrets.ARN_ID_443 }} \ | ||
--default-actions Type=forward,TargetGroupArn=$TARGET_GROUP_ARN | ||
- name: Remove Old Version | ||
uses: appleboy/[email protected] | ||
with: | ||
host: ${{ secrets.EC2_HOST }} | ||
username: ubuntu | ||
key: ${{ secrets.EC2_SSH_KEY }} | ||
envs: | | ||
CURRENT_VERSION=${{ steps.current_version.outputs.version }} | ||
script: | | ||
if [ "$CURRENT_VERSION" != "none" ]; then | ||
sudo docker stop splanet_$CURRENT_VERSION | ||
sudo docker rm splanet_$CURRENT_VERSION | ||
fi | ||
run: | | ||
if [ "${{ env.CURRENT_VERSION }}" != "none" ]; then | ||
ssh -i ${{ secrets.EC2_SSH_KEY }} ubuntu@${{ secrets.EC2_HOST }} " | ||
sudo docker stop splanet_${{ env.CURRENT_VERSION }} | ||
sudo docker rm splanet_${{ env.CURRENT_VERSION }} | ||
" | ||
fi | ||
- name: Clean up old Docker images | ||
uses: appleboy/[email protected] | ||
with: | ||
host: ${{ secrets.EC2_HOST }} | ||
username: ubuntu | ||
key: ${{ secrets.EC2_SSH_KEY }} | ||
script: | | ||
docker image ls --format "{{.ID}} {{.Repository}}:{{.Tag}}" | grep 'kimsongmok/splanet' | tail -n +4 | awk '{print $1}' | xargs docker rmi -f | ||
run: | | ||
ssh -i ${{ secrets.EC2_SSH_KEY }} ubuntu@${{ secrets.EC2_HOST }} " | ||
docker image ls --format \"{{.ID}} {{.Repository}}:{{.Tag}}\" | grep 'kimsongmok/splanet' | tail -n +4 | awk '{print \$1}' | xargs docker rmi -f | ||
sudo docker system prune -f | ||
" |