ci/cd: 정상적으로 요청이 안되는 원인을 찾기 위해 log 추가 #113
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
name: Java CICD with Gradle | |
on: | |
push: | |
branches: | |
- feature/CICD-test | |
pull_request: | |
branches: | |
- main | |
types: [closed] | |
permissions: | |
contents: read | |
jobs: | |
set-variable: | |
runs-on: [self-hosted, loadbalancer-server] | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Check Blue Environment | |
id: check-blue | |
run: | | |
IS_BLUE_ENV=$(sh /usr/local/bin/scripts/is_blue_env.sh) | |
echo "IS_BLUE_ENV=$IS_BLUE_ENV" >> $GITHUB_ENV | |
shell: bash | |
- name: Print Environment | |
run: | | |
echo "IS_BLUE_ENV is ${{ env.IS_BLUE_ENV }}" | |
build-docker-image: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Set YML | |
run: | | |
mkdir -p lime-api/src/main/resources | |
echo "${{ secrets.APPLICATION_YML }}" | base64 --decode > lime-api/src/main/resources/application.yml | |
echo "${{ secrets.APPLICATION_API_YML }}" | base64 --decode > lime-api/src/main/resources/application-api.yml | |
find lime-api/src | |
mkdir -p lime-domain/src/main/resources | |
echo "${{ secrets.APPLICATION_DOMAIN_YML }}" | base64 --decode > lime-domain/src/main/resources/application-domain.yml | |
find lime-domain/src | |
mkdir -p lime-infrastructure/src/main/resources | |
echo "${{ secrets.APPLICATION_INFRA_YML }}" | base64 --decode > lime-infrastructure/src/main/resources/application-infra.yml | |
find lime-infrastructure/src | |
- name: Build with Gradle | |
uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 | |
with: | |
arguments: clean bootJar | |
- name: docker image build | |
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/lime-image . | |
- name: docker login | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
- name: docker Hub push | |
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/lime-image | |
run-docker-image-on-server-1: | |
needs: [ build-docker-image, set-variable ] | |
runs-on: [ self-hosted, lime-was-server-1 ] | |
steps: | |
- name: docker pull | |
run: sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/lime-image | |
- name: Run Docker Container | |
run: | | |
PORT=${{ env.IS_BLUE_ENV == 'true' && secrets.GREEN_PORT || secrets.BLUE_PORT }} | |
# 해당 포트에서 실행 중인 컨테이너 확인 | |
RUNNING_CONTAINER=$(sudo docker ps --filter "status=running" --format '{{.ID}} {{.Ports}}' | grep ":$PORT->" | awk '{print $1}') | |
# 실행 중인 컨테이너가 있다면 중지 | |
if [ ! -z "$RUNNING_CONTAINER" ]; then | |
echo "Stopping existing container on port $PORT..." | |
sudo docker stop $RUNNING_CONTAINER | |
fi | |
echo "Running new container on port $PORT..." | |
sudo docker run --name lime-image --rm -d -p $PORT:8080 ${{ secrets.DOCKERHUB_USERNAME }}/lime-image | |
- name: Test Application Response from /actuator | |
id: test_response | |
continue-on-error: true | |
run: | | |
PORT=${{ env.IS_BLUE_ENV == 'true' && secrets.GREEN_PORT || secrets.BLUE_PORT }} | |
success=false | |
for i in {1..12}; do # 최대 1분(120초) 동안 5초 간격으로 반복 | |
response=$(curl -s http://localhost:$PORT/actuator) | |
if [[ -z "$response" ]]; then | |
echo "No response yet. Waiting for 5 seconds..." | |
sleep 5 | |
else | |
echo "Actuator response received: $response" | |
echo "::set-output name=passed::true" | |
success=true | |
break # 성공적으로 응답을 받으면 루프 종료 | |
fi | |
done | |
if [[ $success == false ]]; then | |
echo "::set-output name=passed::false" | |
fi | |
- name: Stop Docker Container After Test | |
run: | | |
PORT=${{ steps.test_response.outputs.passed == 'true' && secrets.BLUE_PORT || secrets.GREEN_PORT }} | |
echo "Stopping Docker container on port $PORT..." | |
container_id=$(sudo docker ps -q --filter "publish=$PORT") | |
if [ ! -z "$container_id" ]; then | |
sudo docker stop $container_id | |
echo "Container on port $PORT stopped." | |
else | |
echo "No container found running on port $PORT" | |
fi | |
run-docker-image-on-server-2: | |
needs: [ build-docker-image, set-variable ] | |
runs-on: [ self-hosted, lime-was-server-2 ] | |
steps: | |
- name: docker pull | |
run: sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/lime-image | |
- name: Run Docker Container | |
run: | | |
PORT=${{ env.IS_BLUE_ENV == 'true' && secrets.GREEN_PORT || secrets.BLUE_PORT }} | |
RUNNING_CONTAINER=$(sudo docker ps --filter "status=running" --format '{{.ID}} {{.Ports}}' | grep ":$PORT->" | awk '{print $1}') | |
# 실행 중인 컨테이너가 있다면 중지 | |
if [ ! -z "$RUNNING_CONTAINER" ]; then | |
echo "Stopping existing container on port $PORT..." | |
sudo docker stop $RUNNING_CONTAINER | |
fi | |
echo "Running new container on port $PORT..." | |
sudo docker run --name lime-image --rm -d -p $PORT:8080 ${{ secrets.DOCKERHUB_USERNAME }}/lime-image | |
- name: Test Application Response from /actuator | |
id: test_response | |
continue-on-error: true | |
run: | | |
PORT=${{ env.IS_BLUE_ENV == 'true' && secrets.GREEN_PORT || secrets.BLUE_PORT }} | |
success=false | |
echo "Testing application response from /actuator on port $PORT..." | |
for i in {1..24}; do # 최대 1분(120초) 동안 5초 간격으로 반복 | |
echo "Attempt1 $i..." | |
response=$(curl -v http://localhost:$PORT/actuator) | |
echo "Attempt2 $i..." | |
if [[ -z "$response" ]]; then | |
echo "No response yet. Waiting for 5 seconds..." | |
sleep 5 | |
else | |
echo "Actuator response received: $response" | |
echo "::set-output name=passed::true" | |
success=true | |
break # 성공적으로 응답을 받으면 루프 종료 | |
fi | |
done | |
if [[ $success == false ]]; then | |
echo "::set-output name=passed::false" | |
fi | |
- name: Stop Docker Container After Test | |
run: | | |
PORT=${{ steps.test_response.outputs.passed == 'true' && secrets.BLUE_PORT || secrets.GREEN_PORT }} | |
echo "Stopping Docker container on port $PORT..." | |
container_id=$(sudo docker ps -q --filter "publish=$PORT") | |
if [ ! -z "$container_id" ]; then | |
sudo docker stop $container_id | |
echo "Container on port $PORT stopped." | |
else | |
echo "No container found running on port $PORT" | |
fi | |
switch-environment: | |
needs: [ run-docker-image-on-server-1, run-docker-image-on-server-2 ] | |
runs-on: [ self-hosted, loadbalancer-server ] | |
if: > | |
needs.run-docker-image-on-server-1.outputs.passed == 'true' && | |
needs.run-docker-image-on-server-2.outputs.passed == 'true' | |
steps: | |
- name: Switch to Green Environment | |
if: ${{ env.IS_BLUE_ENV == 'true' }} | |
run: sh /usr/local/bin/scripts/switch_to_green_env.sh | |
- name: Switch to Blue Environment | |
if: ${{ env.IS_BLUE_ENV != 'true' }} | |
run: sh /usr/local/bin/scripts/switch_to_blue_env.sh |