update test ci to use local image #2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build and Test Docker Image | |
on: | |
push: | |
branches: | |
- '**' # Trigger the action on every push to any branch | |
tags: | |
- 'v*' # Trigger the action on tags that start with 'v' | |
env: | |
REGISTRY: ghcr.io | |
IMAGE_NAME: ${{ github.repository }} | |
jobs: | |
build-and-test-image: | |
name: Build and Test Docker image | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@v3 | |
with: | |
images: test-image # Use a local image name, not involving any registry | |
- name: Build Docker image (single platform) | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
platforms: linux/amd64 # Specify a single platform for building | |
load: true # Load the image into Docker's local cache | |
tags: test-image:latest # Use a local tag | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
- name: Run Docker container for testing | |
run: | | |
docker run -d -p 8080:8080 --name test_container test-image:latest # Run with the locally built image | |
sleep 2 # Wait for 2 seconds to allow the server to start | |
- name: Test health endpoint | |
run: | | |
status_code=$(curl -o /dev/null -s -w "%{http_code}" http://localhost:8080/health) | |
if [ "$status_code" -ne 200 ]; then | |
echo "Health check failed with status code $status_code" | |
exit 1 | |
fi | |
echo "Health check passed with status code $status_code" | |
- name: Stop and remove Docker container | |
run: | | |
docker stop test_container | |
docker rm test_container |