Skip to content

chore: be cd 스크립트 수정 #44

chore: be cd 스크립트 수정

chore: be cd 스크립트 수정 #44

name: backend-cd-prod
on:
pull_request:
branches:
- main
types:
- closed
env:
DOCKERHUB_REPOSITORY: ody-official
DOCKER_CONTAINER_NAME: ody-prod-app
jobs:
build-and-push:
if: github.event.pull_request.merged == true && startsWith(github.event.pull_request.head.ref, 'release-be/')
runs-on: ubuntu-latest
env:
TZ: 'Asia/Seoul'
defaults:
run:
working-directory: backend
steps:
- uses: actions/checkout@v4
with:
ref: main
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
- name: Check system timezone
run: |
echo "Current date and time: $(date)"
echo "TZ environment variable: $TZ"
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
- name: Clean Build With Gradle Wrapper
run: ./gradlew clean build
- name: Publish Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
if: always()
with:
files: ${{ github.workspace }}/backend/build/test-results/**/*.xml
- name: JUnit Report Action
uses: mikepenz/action-junit-report@v4
if: always()
with:
report_paths: ${{ github.workspace }}/backend/build/test-results/**/*.xml
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Back Up Image For Rollback
run: |
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:latest || true
docker tag ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:latest ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:previous || true
docker push ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:previous || true
- name: Docker Image Build And Push
run: docker build --platform linux/arm64 -t ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:${{ github.sha }}-prod -f Dockerfile . --build-arg JASYPT_ENCRYPTOR_PASSWORD=${{ secrets.JASYPT_PASSWORD }} -push
pull-and-deploy:
needs: build-and-push
runs-on: ${{ matrix.environment }}
strategy:
max-parallel: 1 # 직렬처리
matrix:
environment: [ prod-a, prod-b ]
steps:
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Clean Up Legacy Image And Pull
run: |
docker image prune -a -f
docker pull --platform linux/arm64 ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:${{ github.sha }}-prod
- name: Set up Container And Run Docker Image
run: |
docker stop $DOCKER_CONTAINER_NAME || true
docker rm $DOCKER_CONTAINER_NAME || true
docker run -d --platform linux/arm64 --name $DOCKER_CONTAINER_NAME -v /var/logs/ody-prod-logs:/ody-prod-logs -p 80:8080 -e SPRING_PROFILES_ACTIVE=prod -e JASYPT_ENCRYPTOR_PASSWORD=${{ secrets.JASYPT_PASSWORD }} ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:${{ github.sha }}-prod
- name: Health Check with Retry
uses: nick-invision/retry@v2
with:
timeout_minutes: 1
max_attempts: 5
retry_wait_seconds: 6
command: |
response=$(curl -s https://prod.oody.site/actuator/health)
status=$(echo $response | jq -r '.status')
if [ "$status" = "UP" ]; then
echo "Status is UP. Continuing..."
exit 0
else
echo "Status is NOT UP."
exit 1
fi
- name: Rollback if Health Check fails
if: failure()
run: |
docker stop $DOCKER_CONTAINER_NAME || true
docker rm $DOCKER_CONTAINER_NAME || true
docker run -d --platform linux/arm64 --name $DOCKER_CONTAINER_NAME -v /var/logs/ody-prod-logs:/ody-prod-logs -p 80:8080 -e SPRING_PROFILES_ACTIVE=prod -e JASYPT_ENCRYPTOR_PASSWORD=${{ secrets.JASYPT_PASSWORD }} ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:previous
- name: Tag successful deployment as latest
if: success()
run: |
docker tag ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:${{ github.sha }}-prod ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:latest
docker push ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:latest
- name: Check Docker Process
if: always()
run: docker ps