ci/cd: needs 추가 #127
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] | |
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: Print if Environment is Blue | |
run: | | |
echo "IS_BLUE_ENV is ${{ needs.setup-environment.outputs.is_blue_env }}" | |
shell: bash | |
- name: Run Docker Container | |
run: | | |
PORT=${{ needs.setup-environment.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.setup-environment.outputs.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 || 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 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 ] | |
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.setup-environment.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.setup-environment.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 -v 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 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, set-variable ] | |
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: ${{ needs.setup-environment.outputs.is_blue_env == 'true' }} | |
run: sh /usr/local/bin/scripts/switch_to_green_env.sh | |
- name: Switch to Blue Environment | |
if: ${{ needs.setup-environment.outputs.is_blue_env != 'true' }} | |
run: sh /usr/local/bin/scripts/switch_to_blue_env.sh | |