mirror of
https://github.com/mempool/mempool.git
synced 2026-08-13 12:33:11 +02:00
Merge pull request #6214 from mempool/knorrium/test_images
Refactor Docker workflow
This commit is contained in:
commit
33ea81baf9
2 changed files with 244 additions and 8 deletions
235
.github/workflows/docker.yml
vendored
235
.github/workflows/docker.yml
vendored
|
|
@ -17,11 +17,216 @@ permissions:
|
|||
contents: read
|
||||
|
||||
jobs:
|
||||
test-images:
|
||||
# Always run on tag pushes and all pull requests
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 30
|
||||
name: Test built Docker images
|
||||
steps:
|
||||
- name: Checkout project
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Add SHORT_SHA env property with commit short sha
|
||||
run: |
|
||||
if [ "${{ github.event_name }}" = "pull_request" ]; then
|
||||
SHA="${{ github.event.pull_request.head.sha }}"
|
||||
else
|
||||
SHA="${GITHUB_SHA}"
|
||||
fi
|
||||
echo "SHORT_SHA=${SHA:0:8}" >> $GITHUB_ENV
|
||||
|
||||
- name: Set TAG from pushed tag or package.json
|
||||
run: |
|
||||
if [ "${{ github.event_name }}" = "push" ]; then
|
||||
TAG="${GITHUB_REF/refs\/tags\//}"
|
||||
else
|
||||
FRONTEND_VERSION=$(jq -r '.version' frontend/package.json)
|
||||
BACKEND_VERSION=$(jq -r '.version' backend/package.json)
|
||||
if [ "$FRONTEND_VERSION" != "$BACKEND_VERSION" ]; then
|
||||
echo "Error: Frontend version ($FRONTEND_VERSION) and backend version ($BACKEND_VERSION) do not match"
|
||||
exit 1
|
||||
fi
|
||||
TAG="v${FRONTEND_VERSION}-${SHORT_SHA}"
|
||||
fi
|
||||
echo "TAG=${TAG}" >> $GITHUB_ENV
|
||||
|
||||
- name: Show set environment variables
|
||||
run: |
|
||||
printf " TAG: %s\n" "$TAG"
|
||||
printf " SHORT_SHA: %s\n" "$SHORT_SHA"
|
||||
|
||||
- name: Init repo for Dockerization
|
||||
run: docker/init.sh "$TAG"
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Build frontend image locally
|
||||
run: |
|
||||
docker buildx build \
|
||||
--tag test-frontend:$TAG \
|
||||
--build-arg commitHash=$SHORT_SHA \
|
||||
--load \
|
||||
--platform linux/amd64 \
|
||||
./frontend/
|
||||
|
||||
- name: Build backend image locally
|
||||
run: |
|
||||
docker buildx build \
|
||||
--tag test-backend:$TAG \
|
||||
--build-context rustgbt=./rust \
|
||||
--build-context backend=./backend \
|
||||
--build-arg commitHash=$SHORT_SHA \
|
||||
--load \
|
||||
--platform linux/amd64 \
|
||||
./backend/
|
||||
|
||||
- name: Prepare docker-compose test file
|
||||
run: |
|
||||
cat > /tmp/modify_compose.py << 'SCRIPT_END'
|
||||
import re
|
||||
import os
|
||||
import sys
|
||||
|
||||
# Read the base docker-compose file
|
||||
with open('docker/docker-compose.yml', 'r') as f:
|
||||
content = f.read()
|
||||
|
||||
# Get TAG from environment
|
||||
tag = os.environ.get('TAG', '')
|
||||
|
||||
# Replace image names with locally built test images
|
||||
content = content.replace('image: mempool/frontend:latest', f'image: test-frontend:{tag}')
|
||||
content = content.replace('image: mempool/backend:latest', f'image: test-backend:{tag}')
|
||||
|
||||
# Change web port mapping from 80:8080 to 8080:8080
|
||||
content = content.replace('- 80:8080', '- 8080:8080')
|
||||
|
||||
# Remove volumes from api service
|
||||
content = re.sub(r' volumes:\n - \.\/data:\/backend\/cache\n', '', content)
|
||||
|
||||
# For db service: remove user and volumes, add tmpfs and healthcheck
|
||||
# Remove user line from db service (only the one in db service)
|
||||
lines = content.split('\n')
|
||||
in_db_service = False
|
||||
new_lines = []
|
||||
for i, line in enumerate(lines):
|
||||
if line.strip().startswith('db:'):
|
||||
in_db_service = True
|
||||
elif line.strip() and not line.startswith(' ') and not line.startswith('\t'):
|
||||
in_db_service = False
|
||||
if in_db_service and line.strip() == 'user: "1000:1000"':
|
||||
continue
|
||||
new_lines.append(line)
|
||||
content = '\n'.join(new_lines)
|
||||
|
||||
# Remove volumes section from db service
|
||||
content = re.sub(r' volumes:\n - \.\/mysql\/data:\/var\/lib\/mysql\n', '', content)
|
||||
|
||||
# Add tmpfs after stop_grace_period in db service (healthcheck already exists in base file)
|
||||
db_stop_grace = ' stop_grace_period: 1m'
|
||||
db_additions = ' stop_grace_period: 1m\n tmpfs:\n - /var/lib/mysql'
|
||||
content = content.replace(db_stop_grace, db_additions, 1)
|
||||
|
||||
# Add depends_on to web service after ports
|
||||
web_ports = ' ports:\n - 8080:8080'
|
||||
web_with_depends = ' ports:\n - 8080:8080\n depends_on:\n - api\n - db'
|
||||
content = content.replace(web_ports, web_with_depends, 1)
|
||||
|
||||
# Add depends_on to api service after command
|
||||
api_command = ' command: "./wait-for-it.sh db:3306 --timeout=720 --strict -- ./start.sh"'
|
||||
api_with_depends = ' command: "./wait-for-it.sh db:3306 --timeout=720 --strict -- ./start.sh"\n depends_on:\n - db'
|
||||
content = content.replace(api_command, api_with_depends, 1)
|
||||
|
||||
# Write the modified content
|
||||
with open('docker-compose.test.yml', 'w') as f:
|
||||
f.write(content)
|
||||
|
||||
print("Generated docker-compose.test.yml")
|
||||
SCRIPT_END
|
||||
python3 /tmp/modify_compose.py
|
||||
cat docker-compose.test.yml
|
||||
|
||||
- name: Start containers
|
||||
run: |
|
||||
docker compose -f docker-compose.test.yml up -d
|
||||
|
||||
- name: Wait for services to be ready
|
||||
run: |
|
||||
echo "Waiting for all services (web, api, db) to be healthy..."
|
||||
timeout=120
|
||||
elapsed=0
|
||||
while [ $elapsed -lt $timeout ]; do
|
||||
# Check health status for all services
|
||||
PS_OUTPUT=$(docker compose -f docker-compose.test.yml ps)
|
||||
HEALTHY_COUNT=$(echo "$PS_OUTPUT" | grep -c "(healthy)" || true)
|
||||
if [ "$HEALTHY_COUNT" -ge 3 ]; then
|
||||
echo "All services are healthy!"
|
||||
echo "$PS_OUTPUT"
|
||||
break
|
||||
fi
|
||||
echo "Waiting for services to be healthy... (${elapsed}s/${timeout}s)"
|
||||
echo "$PS_OUTPUT"
|
||||
sleep 2
|
||||
elapsed=$((elapsed + 2))
|
||||
done
|
||||
if [ $elapsed -ge $timeout ]; then
|
||||
echo "Services did not become healthy in time"
|
||||
docker compose -f docker-compose.test.yml ps
|
||||
docker compose -f docker-compose.test.yml logs
|
||||
exit 1
|
||||
fi
|
||||
|
||||
- name: Verify containers are healthy
|
||||
run: |
|
||||
echo "Checking container health status..."
|
||||
PS_OUTPUT=$(docker compose -f docker-compose.test.yml ps)
|
||||
echo "$PS_OUTPUT"
|
||||
|
||||
# Check that all three services (web, api, db) are healthy
|
||||
HEALTHY_COUNT=$(echo "$PS_OUTPUT" | grep -c "(healthy)" || true)
|
||||
if [ "$HEALTHY_COUNT" -lt 3 ]; then
|
||||
echo "Not all containers are healthy. Expected 3 healthy services, found $HEALTHY_COUNT"
|
||||
docker compose -f docker-compose.test.yml logs
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Verify each service individually for better error messages
|
||||
if ! echo "$PS_OUTPUT" | grep -q "web.*(healthy)"; then
|
||||
echo "Web service is not healthy"
|
||||
docker compose -f docker-compose.test.yml logs web
|
||||
exit 1
|
||||
fi
|
||||
if ! echo "$PS_OUTPUT" | grep -q "api.*(healthy)"; then
|
||||
echo "API service is not healthy"
|
||||
docker compose -f docker-compose.test.yml logs api
|
||||
exit 1
|
||||
fi
|
||||
if ! echo "$PS_OUTPUT" | grep -q "db.*(healthy)"; then
|
||||
echo "Database service is not healthy"
|
||||
docker compose -f docker-compose.test.yml logs db
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "All containers are healthy!"
|
||||
|
||||
- name: Show container logs
|
||||
if: failure()
|
||||
run: |
|
||||
docker compose -f docker-compose.test.yml logs
|
||||
|
||||
- name: Clean up containers
|
||||
if: always()
|
||||
run: |
|
||||
docker compose -f docker-compose.test.yml down -v
|
||||
|
||||
build:
|
||||
# Run on tag pushes OR on PRs that have the "docker" label
|
||||
needs: test-images
|
||||
# Run on tag pushes OR on PRs with "docker-push" label (after test-images passes)
|
||||
if: |
|
||||
github.event_name == 'push' ||
|
||||
(github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'docker'))
|
||||
needs.test-images.result == 'success' &&
|
||||
(github.event_name == 'push' ||
|
||||
(github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'docker-push')))
|
||||
strategy:
|
||||
matrix:
|
||||
service:
|
||||
|
|
@ -33,6 +238,7 @@ jobs:
|
|||
outputs:
|
||||
image-digest-frontend: ${{ matrix.service == 'frontend' && steps.docker-build.outputs.digest || '' }}
|
||||
image-digest-backend: ${{ matrix.service == 'backend' && steps.docker-build.outputs.digest || '' }}
|
||||
tag: ${{ matrix.service == 'frontend' && (steps.set-tag-push.outputs.tag || steps.set-tag-pr.outputs.tag) || '' }}
|
||||
steps:
|
||||
- name: Replace the current swap file
|
||||
shell: bash
|
||||
|
|
@ -65,7 +271,11 @@ jobs:
|
|||
# Only for tag pushes: use the Git tag as TAG
|
||||
- name: Set TAG from pushed tag
|
||||
if: github.event_name == 'push'
|
||||
run: echo "TAG=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV
|
||||
id: set-tag-push
|
||||
run: |
|
||||
TAG="${GITHUB_REF/refs\/tags\//}"
|
||||
echo "TAG=${TAG}" >> $GITHUB_ENV
|
||||
echo "tag=${TAG}" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Add SHORT_SHA env property with commit short sha
|
||||
run: |
|
||||
|
|
@ -78,7 +288,10 @@ jobs:
|
|||
|
||||
|
||||
- name: Login to Docker for building
|
||||
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
|
||||
- name: Checkout project
|
||||
uses: actions/checkout@v4
|
||||
|
|
@ -86,13 +299,16 @@ jobs:
|
|||
# For PRs: use package.json version + short sha as TAG
|
||||
- name: Set TAG from service package.json for pull requests
|
||||
if: github.event_name == 'pull_request'
|
||||
id: set-tag-pr
|
||||
run: |
|
||||
if [ "${{ matrix.service }}" = "frontend" ]; then
|
||||
VERSION=$(jq -r '.version' frontend/package.json)
|
||||
else
|
||||
VERSION=$(jq -r '.version' backend/package.json)
|
||||
fi
|
||||
echo "TAG=v${VERSION}-${SHORT_SHA}" >> $GITHUB_ENV
|
||||
TAG="v${VERSION}-${SHORT_SHA}"
|
||||
echo "TAG=${TAG}" >> $GITHUB_ENV
|
||||
echo "tag=${TAG}" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Show set environment variables
|
||||
run: |
|
||||
|
|
@ -144,7 +360,7 @@ jobs:
|
|||
|
||||
tag-latest:
|
||||
needs: build
|
||||
# Only for successful *tag pushes* and only for "plain" versions (no '-')
|
||||
# Only for successful tag pushes (not PRs with docker-push label) and only for "plain" versions (no '-')
|
||||
if: ${{ needs.build.result == 'success' && github.event_name == 'push' && !contains(github.ref_name, '-') }}
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 30
|
||||
|
|
@ -171,7 +387,10 @@ jobs:
|
|||
network=host
|
||||
|
||||
- name: Login to Docker Hub
|
||||
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
|
||||
- name: Tag as latest for ${{ matrix.service }}
|
||||
run: |
|
||||
|
|
|
|||
|
|
@ -12,6 +12,12 @@ services:
|
|||
command: "./wait-for db:3306 --timeout=720 -- nginx -g 'daemon off;'"
|
||||
ports:
|
||||
- 80:8080
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "curl -f http://localhost:8080/ | grep -q '<html' || exit 1"]
|
||||
interval: 30s
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
start_period: 20s
|
||||
api:
|
||||
environment:
|
||||
MEMPOOL_BACKEND: "none"
|
||||
|
|
@ -32,6 +38,12 @@ services:
|
|||
command: "./wait-for-it.sh db:3306 --timeout=720 --strict -- ./start.sh"
|
||||
volumes:
|
||||
- ./data:/backend/cache
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "curl -f http://localhost:8999/api/v1/backend-info | grep -q . || exit 1"]
|
||||
interval: 30s
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
start_period: 40s
|
||||
db:
|
||||
environment:
|
||||
MYSQL_DATABASE: "mempool"
|
||||
|
|
@ -45,3 +57,8 @@ services:
|
|||
stop_grace_period: 1m
|
||||
volumes:
|
||||
- ./mysql/data:/var/lib/mysql
|
||||
healthcheck:
|
||||
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "mempool", "-pmempool"]
|
||||
interval: 5s
|
||||
timeout: 5s
|
||||
retries: 10
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue