Add API tests and update CI wf to start containers #9
Workflow file for this run
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 App | |
on: | |
pull_request: | |
branches: [ "main" ] | |
env: | |
SHA: ${{ github.sha }} | |
jobs: | |
build-docker-image: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Login to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Build and push todo-api | |
uses: docker/build-push-action@v6 | |
with: | |
push: true | |
tags: ${{ secrets.DOCKERHUB_USERNAME }}/todo-api:${{ env.SHA }} | |
- name: Build and push todo-db | |
uses: docker/build-push-action@v6 | |
with: | |
file: Dockerfile.db | |
push: true | |
tags: ${{ secrets.DOCKERHUB_USERNAME }}/todo-db:${{ env.SHA }} | |
api-tests: | |
runs-on: ubuntu-latest | |
needs: build-docker-image | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Login to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Run API tests | |
run: | | |
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/todo-db:${{ env.SHA }} | |
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/todo-api:${{ env.SHA }} | |
- name: Start containers | |
uses: hoverkraft-tech/[email protected] | |
env: | |
SHA: ${{ env.SHA }} | |
- name: Run API tests | |
run: | | |
docker-compose up -d | |
max_attempts=3 | |
attempt=1 | |
while [ $attempt -le $max_attempts ]; do | |
if curl -s http://localhost:8080/health | grep -q '"status":"OK"'; then | |
echo "Container is up and healthy!" | |
break | |
else | |
echo "Attempt $attempt/$max_attempts: Waiting for container..." | |
attempt=$((attempt + 1)) | |
if [ $attempt -le $max_attempts ]; then | |
sleep 60 # Wait for 60 seconds before the next attempt | |
fi | |
fi | |
done | |
if [ $attempt -gt $max_attempts ]; then | |
echo "Container failed to become healthy after $max_attempts attempts." | |
fi | |
- name: Test | |
run: go test -v ./... |