Skip to content

Docker Multi-Stage Pipeline #125

Docker Multi-Stage Pipeline

Docker Multi-Stage Pipeline #125

Workflow file for this run

name: Docker Multi-Stage Pipeline
on:
push:
branches:
- main
workflow_dispatch: # Allow manual runs from the Actions tab
schedule:
# Daily at 11:00 IST => 05:30 UTC
- cron: "30 5 * * *"
# Prevent overlapping runs (especially useful with schedules)
concurrency:
group: docker-multistage
cancel-in-progress: false
permissions:
contents: read
jobs:
docker-build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
# Optional: Enable Buildx for better caching/multi-arch
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build Docker image
run: |
docker build -t docker:siva .
- name: Save Docker image as artifact
run: |
docker save docker:siva > image.tar
- name: Upload image artifact
uses: actions/upload-artifact@v4
with:
name: docker-image
path: image.tar
retention-days: 1
docker-test:
runs-on: ubuntu-latest
needs: docker-build
steps:
- name: Download image artifact
uses: actions/download-artifact@v4
with:
name: docker-image
- name: Load Docker image
run: docker load < image.tar
- name: Run and Test Docker image
env:
CONTAINER_NAME: docker-ci-test
run: |
set -e
docker run -d --name "$CONTAINER_NAME" -p 8080:3000 docker:siva
# Wait for port to be ready (basic retry loop)
for i in {1..10}; do
if curl -sSf http://localhost:8080/ > /dev/null; then
echo "App responded OK"
break
fi
echo "Waiting for app to be ready..."
sleep 3
done
# Final assertion (fail the job if not 200 or 204)
curl -sSf -o /dev/null -w "%{http_code}\n" http://localhost:8080/ | grep -qE "200|204"
# Cleanup
docker logs "$CONTAINER_NAME" || true
docker rm -f "$CONTAINER_NAME"
docker-push:
runs-on: ubuntu-latest
needs: docker-test
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Download image artifact
uses: actions/download-artifact@v4
with:
name: docker-image
- name: Load Docker image
run: docker load < image.tar
- name: Tag and Push Docker image
env:
IMAGE_NAME: golisivaprasad/local
run: |
set -e
# Tag both siva and the commit SHA for traceability
docker tag docker:siva $IMAGE_NAME:siva
# docker tag docker:siva $IMAGE_NAME:${{ github.sha }}
docker push $IMAGE_NAME:siva
# docker push $IMAGE_NAME:${{ github.sha }}