Skip to content

Commit

Permalink
feat: Improve GitHub Actions workflows with better error handling and…
Browse files Browse the repository at this point in the history
… health checks
  • Loading branch information
haahauge committed Mar 20, 2025
1 parent 4e39b92 commit 0bd58bc
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 18 deletions.
56 changes: 47 additions & 9 deletions .github/workflows/deploy-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,78 @@ on:
jobs:
deploy:
runs-on: self-hosted
env:
DOCKER_BUILDKIT: 1

steps:
- name: Checkout Repository
uses: actions/checkout@v3
with:
token: ${{ secrets.ACTIONS }}

- name: Setup Environment
run: |
echo "Setting up environment..."
if [ ! -f .env ]; then
echo "Creating .env file..."
cp .env.example .env || echo "No .env.example found, skipping..."
fi
- name: Load Environment Variables
uses: falti/dotenv-action@v1.1.4
with:
export-variables: true
keys-case: bypass

- name: Setup Docker
run: |
echo "Current user and groups:"
id
echo "Docker info:"
docker info || true
echo "Ensuring docker group membership..."
if ! groups | grep -q docker; then
echo "::warning::Current user is not in docker group"
fi
- name: Build Docker Image for Frontend
run: |
echo "Building frontend image..."
cd frontend
sudo docker build -t secfit-frontend:latest .
docker build --progress=plain -t secfit-frontend:latest .
- name: Stop and Remove Existing Container
- name: Clean Up Existing Container
run: |
sudo docker rm -f secfit-frontend || true
echo "Cleaning up existing container..."
docker rm -f secfit-frontend || true
- name: Run New Container
- name: Deploy Container
run: |
sudo docker run -d \
echo "Deploying new container..."
docker run -d \
--name secfit-frontend \
-p 3000:3000 \
--restart unless-stopped \
-e NODE_ENV=production \
--health-cmd="curl -f http://localhost:3000 || exit 1" \
--health-interval=10s \
--health-timeout=5s \
--health-retries=3 \
secfit-frontend:latest
- name: Verify Deployment
run: |
sudo docker ps
echo "Waiting for container to be healthy..."
sleep 10
curl -f http://localhost:3000 || exit 1
echo "Verifying deployment..."
docker ps
echo "Waiting for container health check..."
for i in {1..12}; do
if docker inspect secfit-frontend --format='{{.State.Health.Status}}' 2>/dev/null | grep -q healthy; then
echo "Container is healthy!"
exit 0
fi
echo "Waiting for container to be healthy... (attempt $i/12)"
sleep 10
done
echo "Container health check failed"
docker logs secfit-frontend
exit 1
65 changes: 56 additions & 9 deletions .github/workflows/deploy_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,72 @@ jobs:
env:
REPO_NAME: ${{ github.event.repository.name }}
COMPOSE_FILE: ${{ github.event.inputs.config }}
DOCKER_BUILDKIT: 1

steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Dotenv Action
with:
token: ${{ secrets.ACTIONS }}

- name: Setup Environment
run: |
echo "Setting up environment..."
if [ ! -f .env ]; then
echo "Creating .env file..."
cp .env.example .env || echo "No .env.example found, skipping..."
fi
- name: Load Environment Variables
uses: falti/dotenv-action@v1.1.4
with:
export-variables: true
keys-case: bypass

- name: Check status of config file
- name: Setup Docker
run: |
env
echo "Current user and groups:"
id
echo "Docker info:"
docker info || true
echo "Ensuring docker group membership..."
if ! groups | grep -q docker; then
echo "::warning::Current user is not in docker group"
fi
- name: Check Docker Compose Config
run: |
echo "Environment variables:"
env | sort
echo "Docker compose version:"
docker compose version
cd $GITHUB_WORKSPACE
docker compose -f $COMPOSE_FILE --verbose config && printf "OK\n" || exit 1
echo "Validating compose file:"
docker compose -f $COMPOSE_FILE config && echo "Configuration valid!" || exit 1
- name: Build and run docker image
run: docker compose -f $COMPOSE_FILE up --force-recreate --build -d
- name: Deploy with Docker Compose
run: |
echo "Deploying with docker compose..."
docker compose -f $COMPOSE_FILE up --force-recreate --build -d
- name: Verify that gateway is available
run: docker ps
- name: Verify Deployment
run: |
echo "Checking running containers..."
docker ps
echo "Waiting for services to be healthy..."
for i in {1..12}; do
if docker compose -f $COMPOSE_FILE ps | grep -q "unhealthy\|exit"; then
echo "Some containers are unhealthy or have exited"
docker compose -f $COMPOSE_FILE logs
exit 1
elif docker compose -f $COMPOSE_FILE ps | grep -q "starting"; then
echo "Some containers are still starting... (attempt $i/12)"
sleep 10
continue
else
echo "All containers appear to be running!"
exit 0
fi
done
echo "Deployment verification timed out"
docker compose -f $COMPOSE_FILE logs
exit 1

0 comments on commit 0bd58bc

Please sign in to comment.