Skip to content

Commit 1fd3cbb

Browse files
committed
CI: Test that images start successfully after they are built.
1 parent ab9bbf5 commit 1fd3cbb

File tree

3 files changed

+201
-13
lines changed

3 files changed

+201
-13
lines changed

.github/workflows/build-brev-tutorial-docker-images.yml

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- name: Find tutorial directories
1818
id: find-tutorials
1919
run: |
20-
tutorials=$(find tutorials -mindepth 1 -maxdepth 1 -type d | jq -R -s -c 'split("\n") | map(select(length > 0))')
20+
tutorials=$(brev/discover-tutorials.bash | jq -R -s -c 'split("\n") | map(select(length > 0))')
2121
echo "tutorials=${tutorials}" >> $GITHUB_OUTPUT
2222
echo "Found tutorials: ${tutorials}"
2323
@@ -36,26 +36,18 @@ jobs:
3636
- name: Checkout repository
3737
uses: actions/checkout@v4
3838

39-
# - name: Free up disk space
40-
# run: |
41-
# # Delete large pre-installed software in parallel for speed
42-
# sudo rm -rf /usr/local/lib/android &
43-
# sudo rm -rf /opt/hostedtoolcache/CodeQL &
44-
# sudo rm -rf /usr/share/dotnet &
45-
# wait
46-
# echo "Disk space after cleanup:"
47-
# df -h /
48-
4939
- name: Get branch name and short SHA
5040
run: |
5141
echo "GIT_BRANCH_NAME=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV
5242
echo "GIT_SHORT_SHA=${GITHUB_SHA::7}" >> $GITHUB_ENV
5343
54-
- name: Set image name
44+
- name: Set image name and tutorial name
5545
id: set-image
5646
run: |
57-
image_name="ghcr.io/${{ github.repository_owner }}/$(basename ${{ matrix.tutorial }})-tutorial"
47+
tutorial_name=$(basename ${{ matrix.tutorial }})
48+
image_name="ghcr.io/${{ github.repository_owner }}/${tutorial_name}-tutorial"
5849
echo "image_name=${image_name,,}" >> $GITHUB_OUTPUT
50+
echo "tutorial_name=${tutorial_name}" >> $GITHUB_OUTPUT
5951
6052
- name: Check for HPCCM recipe
6153
id: check-hpccm
@@ -115,6 +107,19 @@ jobs:
115107
-f docker-compose.yml \
116108
base
117109
110+
- name: Mark build as successful
111+
run: |
112+
mkdir -p build-success
113+
echo "${{ steps.set-image.outputs.tutorial_name }}" > build-success/${{ steps.set-image.outputs.tutorial_name }}.txt
114+
echo "Build successful for ${{ steps.set-image.outputs.tutorial_name }}"
115+
116+
- name: Upload build success marker
117+
uses: actions/upload-artifact@v4
118+
with:
119+
name: build-success-${{ steps.set-image.outputs.tutorial_name }}
120+
path: build-success/
121+
retention-days: 1
122+
118123
- name: Check disk space after build
119124
if: always()
120125
run: |
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
name: Test Brev Tutorial Docker Images
2+
3+
on:
4+
workflow_run:
5+
workflows: ["Build and Push Brev Tutorial Docker Images"]
6+
types:
7+
- completed
8+
branches:
9+
- brev-reorg
10+
11+
jobs:
12+
discover-successful-builds:
13+
runs-on: ubuntu-latest
14+
# Only run if the build workflow completed (not cancelled)
15+
if: ${{ github.event.workflow_run.conclusion != 'cancelled' }}
16+
outputs:
17+
tutorials: ${{ steps.find-successful.outputs.tutorials }}
18+
steps:
19+
- name: Download all build success artifacts
20+
uses: dawidd6/action-download-artifact@v3
21+
with:
22+
workflow: build-brev-tutorial-docker-images.yml
23+
run_id: ${{ github.event.workflow_run.id }}
24+
path: build-success-artifacts
25+
name_is_regexp: true
26+
name: build-success-.*
27+
28+
- name: Find successful builds
29+
id: find-successful
30+
run: |
31+
# Collect all tutorial names from successful builds
32+
successful=()
33+
if [ -d "build-success-artifacts" ]; then
34+
# Find all .txt files containing tutorial names
35+
while IFS= read -r file; do
36+
if [ -f "$file" ]; then
37+
tutorial=$(cat "$file")
38+
successful+=("$tutorial")
39+
echo "Found successful build: $tutorial"
40+
fi
41+
done < <(find build-success-artifacts -name "*.txt" -type f)
42+
fi
43+
44+
# Convert to JSON array
45+
if [ ${#successful[@]} -eq 0 ]; then
46+
echo "tutorials=[]" >> $GITHUB_OUTPUT
47+
echo "No successful builds found"
48+
else
49+
tutorials_json=$(printf '%s\n' "${successful[@]}" | jq -R -s -c 'split("\n") | map(select(length > 0))')
50+
echo "tutorials=${tutorials_json}" >> $GITHUB_OUTPUT
51+
echo "Successful builds to test: ${tutorials_json}"
52+
fi
53+
54+
test-images:
55+
needs: [discover-successful-builds]
56+
# Only run if there are successful builds to test
57+
if: fromJson(needs.discover-successful-builds.outputs.tutorials)[0] != null
58+
runs-on: ubuntu-latest
59+
strategy:
60+
matrix:
61+
tutorial: ${{ fromJson(needs.discover-successful-builds.outputs.tutorials) }}
62+
fail-fast: false
63+
64+
steps:
65+
- name: Checkout repository
66+
uses: actions/checkout@v4
67+
68+
- name: Log in to GitHub Container Registry
69+
uses: docker/login-action@v3
70+
with:
71+
registry: ghcr.io
72+
username: ${{ github.actor }}
73+
password: ${{ secrets.GITHUB_TOKEN }}
74+
75+
- name: Test Docker Compose
76+
run: ./brev/test-docker-compose.bash "tutorials/${{ matrix.tutorial }}/brev/docker-compose.yml"

brev/test-docker-compose.bash

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#! /bin/bash
2+
3+
# This script tests a single Docker Compose file by starting and stopping containers.
4+
#
5+
# Usage:
6+
# ./test-docker-compose.bash <docker-compose-file>
7+
#
8+
# Example:
9+
# ./test-docker-compose.bash tutorials/accelerated-python/brev/docker-compose.yml
10+
11+
set -euo pipefail
12+
13+
SCRIPT_PATH=$(cd $(dirname ${0}); pwd -P)
14+
REPO_ROOT=$(cd ${SCRIPT_PATH}/..; pwd -P)
15+
16+
# Check argument
17+
if [ $# -ne 1 ]; then
18+
echo "Error: Docker Compose file path is required"
19+
echo "Usage: $0 <docker-compose-file>"
20+
echo "Example: $0 tutorials/accelerated-python/brev/docker-compose.yml"
21+
exit 1
22+
fi
23+
24+
COMPOSE_FILE=$1
25+
26+
# Convert to absolute path if relative
27+
if [[ "${COMPOSE_FILE}" != /* ]]; then
28+
COMPOSE_FILE="${REPO_ROOT}/${COMPOSE_FILE}"
29+
fi
30+
31+
# Validate docker-compose file exists
32+
if [ ! -f "${COMPOSE_FILE}" ]; then
33+
echo "❌ Error: Docker Compose file not found: ${COMPOSE_FILE}"
34+
exit 1
35+
fi
36+
37+
echo "============================================"
38+
echo "Testing Docker Compose: ${COMPOSE_FILE}"
39+
echo "============================================"
40+
echo ""
41+
42+
# Start containers
43+
echo "📦 Starting containers..."
44+
echo ""
45+
if docker compose -f "${COMPOSE_FILE}" up -d; then
46+
echo ""
47+
echo "✅ Containers started successfully"
48+
echo ""
49+
50+
# Wait a moment for containers to initialize
51+
echo "⏳ Waiting for containers to initialize..."
52+
sleep 5
53+
echo ""
54+
55+
# Show container status
56+
echo "📊 Container status:"
57+
docker compose -f "${COMPOSE_FILE}" ps
58+
echo ""
59+
60+
# Capture and display logs
61+
echo "📋 Container logs:"
62+
echo "--------------------------------------------"
63+
docker compose -f "${COMPOSE_FILE}" logs
64+
echo "--------------------------------------------"
65+
echo ""
66+
67+
# Stop containers
68+
echo "🛑 Stopping containers..."
69+
if docker compose -f "${COMPOSE_FILE}" down; then
70+
echo "✅ Containers stopped successfully"
71+
echo ""
72+
return_code=0
73+
else
74+
echo "❌ Failed to stop containers"
75+
echo ""
76+
return_code=1
77+
fi
78+
else
79+
echo ""
80+
echo "❌ Failed to start containers"
81+
echo ""
82+
83+
# Try to capture any logs that might be available
84+
echo "📋 Attempting to capture logs from failed startup:"
85+
echo "--------------------------------------------"
86+
docker compose -f "${COMPOSE_FILE}" logs || true
87+
echo "--------------------------------------------"
88+
echo ""
89+
90+
# Try to clean up
91+
echo "🛑 Attempting cleanup..."
92+
docker compose -f "${COMPOSE_FILE}" down || true
93+
echo ""
94+
95+
return_code=1
96+
fi
97+
98+
echo "============================================"
99+
if [ ${return_code} -eq 0 ]; then
100+
echo "✅ TEST PASSED: ${COMPOSE_FILE}"
101+
else
102+
echo "❌ TEST FAILED: ${COMPOSE_FILE}"
103+
fi
104+
echo "============================================"
105+
echo ""
106+
107+
exit ${return_code}

0 commit comments

Comments
 (0)