Skip to content

Commit

Permalink
chore: update Docker CI/CD workflow with retries, health checks, and …
Browse files Browse the repository at this point in the history
…improved logging
  • Loading branch information
LeonardoMeireles55 committed Jan 29, 2025
1 parent 930885b commit 2f925a8
Showing 1 changed file with 79 additions and 27 deletions.
106 changes: 79 additions & 27 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,69 @@ name: Docker Image CI/CD

on:
push:
branches: [ 'master' ]
# pull_request:
# branches: ['master']
branches: [ 'dev/workflows' ]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build:
runs-on: [ self-hosted, linux, x64, backend ]
timeout-minutes: 15

steps:
- uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
scan-type: 'fs'
scan-ref: '.'
format: 'table'
exit-code: '1'
ignore-unfixed: true
severity: 'CRITICAL,HIGH'

- name: Log in to Docker Hub
run: echo "${{ secrets.DOCKER_HUB_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_HUB_USERNAME }}" --password-stdin
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}

- name: Build and push Docker image
id: docker_build
run: |
docker compose -f docker-compose.build.yml build
docker compose -f docker-compose.build.yml push
for i in 1 2 3; do
if docker compose -f docker-compose.build.yml build && \
docker compose -f docker-compose.build.yml push; then
exit 0
fi
echo "Retry $i/3..."
sleep 10
done
exit 1
deploy:
runs-on: [ self-hosted, linux, x64, backend ]
needs: build
environment: Production
timeout-minutes: 10

steps:
- name: Checkout code
uses: actions/checkout@v4
- uses: actions/checkout@v4

- name: Deploy
env:
DB_DATABASE: ${{ secrets.DB_DATABASE }}
Expand All @@ -54,36 +83,59 @@ jobs:
SPRING_MAIL_PASSWORD: ${{ secrets.SPRING_MAIL_PASSWORD }}
EMAIL_TO_SEND_LIST: ${{ secrets.EMAIL_TO_SEND_LIST }}
run: |
docker compose pull
docker compose up -d
notify:
runs-on: [ self-hosted, linux, x64, backend ]
needs: deploy

steps:
- name: Notify deployment
run: echo "Deployment completed successfully"
for i in 1 2 3; do
if docker compose pull && docker compose up -d; then
exit 0
fi
echo "Retry $i/3..."
sleep 10
done
exit 1
health-check:
runs-on: [ self-hosted, linux, x64, backend ]
needs: deploy
timeout-minutes: 5

steps:
- name: Wait for server to be ready and healthy test
- name: Health check with timeout and retry
run: |
sleep 30
until curl -sSf http://localhost:${{ secrets.SERVER_LOCAL_PORT }}/actuator/health; do
echo "Waiting for server..."
sleep 5
max_attempts=12
attempt=1
while [ $attempt -le $max_attempts ]; do
if curl -sSf http://localhost:${{ secrets.SERVER_LOCAL_PORT }}/actuator/health; then
echo "Service is healthy!"
exit 0
fi
echo "Attempt $attempt/$max_attempts - Service not healthy yet..."
sleep 10
attempt=$((attempt + 1))
done
echo "Health check failed after $max_attempts attempts"
exit 1
cleanup:
runs-on: [ self-hosted, linux, x64, backend ]
needs: health-check
if: always()

steps:
- uses: actions/checkout@v4
- name: Cleanup Docker resources
run: |
chmod +x ./.github/scripts/cleanup_docker.sh
./.github/scripts/cleanup_docker.sh
notify:
runs-on: [ self-hosted, linux, x64, backend ]
needs: [deploy, health-check, cleanup]
if: always()

steps:
- name: Set execute permission for cleanup script
run: chmod +x ./.github/scripts/cleanup_docker.sh
- name: Running scripts
run: ./.github/scripts/cleanup_docker.sh
- name: Notify deployment status
run: |
if [ "${{ job.status }}" = "success" ]; then
echo "✅ Deployment completed successfully"
else
echo "❌ Deployment failed"
fi

0 comments on commit 2f925a8

Please sign in to comment.