Skip to content

Docker Build and Push #1

Docker Build and Push

Docker Build and Push #1

Workflow file for this run

name: Docker Build and Push
on:
workflow_dispatch:
workflow_call:
inputs:
push-image:
description: 'Whether to push the Docker image'
required: false
default: true
type: boolean
outputs:
image-tag:
description: "Docker image tag"
value: ${{ jobs.docker-build.outputs.image-tag }}
env:
MONGO_URI: ${{ secrets.MONGO_URI }}
MONGO_USERNAME: ${{ secrets.MONGO_USERNAME }}
MONGO_PASSWORD: ${{ secrets.MONGO_PASSWORD }}
jobs:
docker-build:
name: Docker Build and Test
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
image-tag: ${{ github.sha }}
steps:
- name: Checkout Repository
uses: actions/checkout@v5
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: GHCR Login
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build Docker Image
uses: docker/build-push-action@v6
with:
push: false
tags: |
ghcr.io/${{ secrets.DOCKER_USERNAME }}/solar-system:${{ github.sha }}
- name: Test Docker Image
run: |
docker images
# Start the container
docker run --name solar-system-app -d \
-p 3000:3000 \
-e MONGO_URI=${{ secrets.MONGO_URI }} \
-e MONGO_USERNAME=${{ secrets.MONGO_USERNAME }} \
-e MONGO_PASSWORD=${{ secrets.MONGO_PASSWORD }} \
ghcr.io/${{ secrets.DOCKER_USERNAME }}/solar-system:${{ github.sha }}
# Wait for container to start
sleep 15
# Get container IP
export IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' solar-system-app)
echo "Container IP: $IP"
# Check if container is running
docker ps
# Check container logs
echo "Container logs:"
docker logs solar-system-app
echo "Testing the application..."
# Test the health endpoint
response=$(curl -s http://127.0.0.1:3000/live)
echo "Response: $response"
if [[ $response == *"live"* ]]; then
echo "Health check passed!"
else
echo "Health check failed!"
docker logs solar-system-app
exit 1
fi
- name: Push Docker Image
if: ${{ inputs.push-image != false }}
uses: docker/build-push-action@v6
with:
push: true
tags: |
docker.io/${{ secrets.DOCKER_USERNAME }}/solar-system:${{ github.sha }}
ghcr.io/${{ secrets.DOCKER_USERNAME }}/solar-system:${{ github.sha }}