ci/cd: 사전에 run하고자 하는 포트가 있다면 제거 #104
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 -q --filter "ancestor=${{ secrets.DOCKERHUB_USERNAME }}/lime-image" --filter "publish=$PORT") | |
sudo docker stop $RUNNING_CONTAINER | |
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 }} | |
response=$(curl -s http://localhost:$PORT/actuator) | |
if [[ -z "$response" ]]; then | |
echo "::set-output name=passed::false" | |
else | |
echo "Actuator response received: $response" | |
echo "::set-output name=passed::true" | |
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" | |
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 -q --filter "ancestor=${{ secrets.DOCKERHUB_USERNAME }}/lime-image" --filter "publish=$PORT") | |
sudo docker stop $RUNNING_CONTAINER | |
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 }} | |
response=$(curl -s http://localhost:$PORT/actuator) | |
if [[ -z "$response" ]]; then | |
echo "::set-output name=passed::false" | |
else | |
echo "Actuator response received: $response" | |
echo "::set-output name=passed::true" | |
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 | |