Skip to content

[LIME-159] 레디스 메시지 브로커 설정 #181

[LIME-159] 레디스 메시지 브로커 설정

[LIME-159] 레디스 메시지 브로커 설정 #181

Workflow file for this run

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]
outputs:
is_blue_env: ${{ steps.check-blue.outputs.is_blue_env }}
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 "::set-output name=is_blue_env::$IS_BLUE_ENV"
shell: bash
- name: Print if Environment is Blue
run: |
if [[ "${{ steps.check-blue.outputs.is_blue_env }}" == "true" ]]; then
echo "Environment is BLUE"
else
echo "Environment is NOT BLUE"
fi
shell: bash
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 ]
outputs:
passed: ${{ steps.test_response.outputs.passed }}
steps:
- name: docker pull
run: sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/lime-image
- name: Run Docker Container
run: |
PORT=${{ needs.set-variable.outputs.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 --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=${{ needs.set-variable.outputs.is_blue_env == 'true' && secrets.GREEN_PORT || secrets.BLUE_PORT }}
success=false
for i in {1..24}; do # 최대 1분(120초) 동안 5초 간격으로 반복
response=$(curl -s http://localhost:$PORT/actuator || true)
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 TEST Docker Container If Test Failed
run: |
TEST_PORT=${{ needs.set-variable.outputs.is_blue_env == 'true' && secrets.GREEN_PORT || secrets.BLUE_PORT }}
STOP_PORT=${{ needs.set-variable.outputs.is_blue_env == 'true' && secrets.BLUE_PORT || secrets.GREEN_PORT }}
# 테스트 결과에 따라 컨테이너 중지
if [[ ${{ steps.test_response.outputs.passed }} != 'true' ]]; then # 테스트 성공 시
echo "Test failed. Not stopping container on port $STOP_PORT."
container_id=$(sudo docker ps --filter "status=running" --format '{{.ID}} {{.Ports}}' | grep ":$TEST_PORT->" | awk '{print $1}')
if [ ! -z "$container_id" ]; then # 테스트 실패 시 테스트 포트에 실행 중인 컨테이너가 있다면 중지
sudo docker stop $container_id
echo "Container on port $TEST_PORT stopped."
else
echo "No container found running on port $TEST_PORT"
fi
fi
run-docker-image-on-server-2:
needs: [ build-docker-image, set-variable ]
runs-on: [ self-hosted, lime-was-server-2 ]
outputs:
passed: ${{ steps.test_response.outputs.passed }}
steps:
- name: docker pull
run: sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/lime-image
- name: Run Docker Container
run: |
PORT=${{ needs.set-variable.outputs.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 --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=${{ needs.set-variable.outputs.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 -s http://localhost:$PORT/actuator || true)
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 TEST Docker Container If Test Failed
run: |
TEST_PORT=${{ needs.set-variable.outputs.is_blue_env == 'true' && secrets.GREEN_PORT || secrets.BLUE_PORT }}
STOP_PORT=${{ needs.set-variable.outputs.is_blue_env == 'true' && secrets.BLUE_PORT || secrets.GREEN_PORT }}
# 테스트 결과에 따라 컨테이너 중지
if [[ ${{ steps.test_response.outputs.passed }} != 'true' ]]; then # 테스트 성공 시
echo "Test failed. Not stopping container on port $STOP_PORT."
container_id=$(sudo docker ps --filter "status=running" --format '{{.ID}} {{.Ports}}' | grep ":$TEST_PORT->" | awk '{print $1}')
if [ ! -z "$container_id" ]; then # 테스트 실패 시 테스트 포트에 실행 중인 컨테이너가 있다면 중지
sudo docker stop $container_id
echo "Container on port $TEST_PORT stopped."
else
echo "No container found running on port $TEST_PORT"
fi
fi
switch-environment:
needs: [ run-docker-image-on-server-1, run-docker-image-on-server-2]
runs-on: [ self-hosted, loadbalancer-server ]
outputs:
is_blue_env: ${{ steps.check-blue.outputs.is_blue_env }}
if: >
needs.run-docker-image-on-server-1.outputs.passed == 'true' &&
needs.run-docker-image-on-server-2.outputs.passed == 'true'
steps:
- name: Check Blue Environment
id: check-blue
run: |
IS_BLUE_ENV=$(sh /usr/local/bin/scripts/is_blue_env.sh)
echo "::set-output name=is_blue_env::$IS_BLUE_ENV"
shell: bash
- name: Switch to Green Environment
if: ${{ steps.check-blue.outputs.is_blue_env == 'true' }}
run: sh /usr/local/bin/scripts/switch_to_green_env.sh
- name: Switch to Blue Environment
if: ${{ steps.check-blue.outputs.is_blue_env != 'true' }}
run: sh /usr/local/bin/scripts/switch_to_blue_env.sh
stop-exist-container-in-was-1:
needs: [ run-docker-image-on-server-1, run-docker-image-on-server-2, switch-environment ]
runs-on: [ self-hosted, lime-was-server-1 ]
if: >
needs.run-docker-image-on-server-1.outputs.passed == 'true' &&
needs.run-docker-image-on-server-2.outputs.passed == 'true'
steps:
- name: Stop Existing Container in WAS-1
run: |
STOP_PORT=${{ needs.switch-environment.outputs.is_blue_env == 'true' && secrets.BLUE_PORT || secrets.GREEN_PORT }}
echo "Stopping Docker container on port $STOP_PORT..."
container_id=$(sudo docker ps --filter "status=running" --format '{{.ID}} {{.Ports}}' | grep ":$STOP_PORT->" | awk '{print $1}')
if [ ! -z "$container_id" ]; then # 테스트 성공 시 중지할 포트에 실행 중인 컨테이너가 있다면 중지
sudo docker stop $container_id
echo "Container on port $STOP_PORT stopped."
else
echo "No container found running on port $STOP_PORT"
fi
stop-exist-container-in-was-2:
needs: [ run-docker-image-on-server-1, run-docker-image-on-server-2, switch-environment ]
runs-on: [ self-hosted, lime-was-server-2 ]
if: >
needs.run-docker-image-on-server-1.outputs.passed == 'true' &&
needs.run-docker-image-on-server-2.outputs.passed == 'true'
steps:
- name: Stop Existing Container in WAS-2
run: |
STOP_PORT=${{ needs.switch-environment.outputs.is_blue_env == 'true' && secrets.BLUE_PORT || secrets.GREEN_PORT }}
echo "Stopping Docker container on port $STOP_PORT..."
container_id=$(sudo docker ps --filter "status=running" --format '{{.ID}} {{.Ports}}' | grep ":$STOP_PORT->" | awk '{print $1}')
if [ ! -z "$container_id" ]; then # 테스트 성공 시 중지할 포트에 실행 중인 컨테이너가 있다면 중지
sudo docker stop $container_id
echo "Container on port $STOP_PORT stopped."
else
echo "No container found running on port $STOP_PORT"
fi