Skip to content

Merge pull request #152 from PROTOTYNE/develop #71

Merge pull request #152 from PROTOTYNE/develop

Merge pull request #152 from PROTOTYNE/develop #71

Workflow file for this run

name: CICD
on:
push:
branches: [ "main" ]
# pull_request:
# branches: [ "main" ]
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Grant execution permission for gradlew
run: chmod +x ./gradlew
- name: Build with Gradle
run: ./gradlew clean build -x test
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build Docker
run: docker build --platform linux/amd64 --build-arg PROFILES=blue --build-arg ENV=blue -t ${{ secrets.DOCKERHUB_USERNAME }}/live_server .
- name: Push Docker
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/live_server:latest
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Set target IP
run: |
URL="http://${{ secrets.LIVE_SERVER_IP }}/env"
STATUS=$(curl -o /dev/null -w "%{http_code}" "$URL")
echo $STATUS
if [ "$STATUS" = "200" ]; then
CURRENT_UPSTREAM=$(curl -s "$URL")
echo "CURRENT_UPSTREAM from server: $CURRENT_UPSTREAM"
else
CURRENT_UPSTREAM=green
echo "CURRENT_UPSTREAM default: $CURRENT_UPSTREAM"
fi
echo CURRENT_UPSTREAM=$CURRENT_UPSTREAM >> $GITHUB_ENV
if [ "$CURRENT_UPSTREAM" = "blue" ]; then
echo "CURRENT_PORT=8080" >> $GITHUB_ENV
echo "STOPPED_PORT=8081" >> $GITHUB_ENV
echo "TARGET_UPSTREAM=green" >> $GITHUB_ENV
elif [ "$CURRENT_UPSTREAM" = "green" ]; then
echo "CURRENT_PORT=8081" >> $GITHUB_ENV
echo "STOPPED_PORT=8080" >> $GITHUB_ENV
echo "TARGET_UPSTREAM=blue" >> $GITHUB_ENV
else
echo "Unexpected CURRENT_UPSTREAM value: $CURRENT_UPSTREAM"
echo "CURRENT_UPSTREAM default: green"
CURRENT_UPSTREAM=green
echo "CURRENT_PORT=8081" >> $GITHUB_ENV
echo "STOPPED_PORT=8080" >> $GITHUB_ENV
echo "TARGET_UPSTREAM=blue" >> $GITHUB_ENV
fi
- name: Deploy new version
uses: appleboy/ssh-action@master
with:
username: ubuntu
host: ${{ secrets.LIVE_SERVER_IP }}
key: ${{ secrets.EC2_SSH_KEY }}
script_stop: true
script: |
sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/live_server:latest
sudo docker-compose -f docker-compose-${{env.TARGET_UPSTREAM}}.yml up -d --remove-orphans
- name: Wait for server to start
run: sleep 30 # 서버가 시작될 시간을 확보
- name: Check deploy server URL
uses: jtalk/url-health-check-action@v3
with:
url: http://${{ secrets.LIVE_SERVER_IP }}:${{env.STOPPED_PORT}}/env
max-attempts: 10 # 재시도 횟수를 10으로 증가
retry-delay: 20s # 지연 시간을 20초로 설정
- name: Check if nginxserver is running
run: |
if [ $(sudo docker ps -q -f name=nginxserver) ]; then
echo "nginxserver is already running";
elif [ $(sudo docker ps -a -q -f name=nginxserver) ]; then
echo "nginxserver container exists but is stopped. Starting it now.";
sudo docker start nginxserver;
else
echo "nginxserver container does not exist, creating and starting it";
sudo docker run -d --name nginxserver -p 80:80 nginx;
fi
- name: Change nginx upstream
uses: appleboy/ssh-action@master
with:
username: ubuntu
host: ${{ secrets.LIVE_SERVER_IP }}
key: ${{ secrets.EC2_SSH_KEY }}
script_stop: true
script: |
sudo docker exec -i nginxserver bash -c 'echo "set \$service_url ${{ env.TARGET_UPSTREAM }};" > /etc/nginx/conf.d/service-env.inc && nginx -s reload'
# - name: Stop current server
# uses: appleboy/ssh-action@master
# with:
# username: ubuntu
# host: ${{ secrets.LIVE_SERVER_IP }}
# key: ${{ secrets.EC2_SSH_KEY }}
# script_stop: true
# script: |
# if sudo docker ps -a --format '{{.Names}}' | grep -Eq "^${{env.CURRENT_UPSTREAM}}$"; then
# echo "Stopping and removing container: ${{env.CURRENT_UPSTREAM}}";
# sudo docker stop ${{env.CURRENT_UPSTREAM}} && sudo docker rm ${{env.CURRENT_UPSTREAM}};
# else
# echo "Container ${{env.CURRENT_UPSTREAM}} does not exist. Skipping stop and remove.";
# fi