Skip to content

Calculate Docker Image Size #3

Calculate Docker Image Size

Calculate Docker Image Size #3

name: Calculate Docker Image Size
on:
workflow_dispatch:
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
env:
PYTHON_VERSION: '3.13'
TASKFILE_VERSION: 'v3.44.0'
TASKFILE_PATH: '/home/runner/go/bin'
services:
registry:
image: registry:3
ports:
- 5000:5000
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Get PR Information
id: pr_info
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
echo "Searching for PR from branch ${GITHUB_REF_NAME}..."
PR_NUMBER=$(gh pr list --state open --head "${GITHUB_REF_NAME}" --json number --jq '.[0].number // empty')
if [ -z "$PR_NUMBER" ]; then
echo "Not found in current repo, searching across forks..."
PR_NUMBER=$(gh pr list --state open --json number,headRefName,headRepositoryOwner --jq ".[] | select(.headRefName == \"${{ github.ref_name }}\") | .number" | head -n1)
fi
if [ -z "$PR_NUMBER" ]; then
echo "Could not find an associated open pull request."
else
echo "Found PR #$PR_NUMBER"
fi
echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
cache: 'pip'
cache-dependency-path: 'pyproject.toml'
- name: Install and build library
run: |
which task || curl -sSfL https://taskfile.dev/install.sh | sh -s -- -b ${{ env.TASKFILE_PATH }} ${{ env.TASKFILE_VERSION }}
export PATH="${{ env.TASKFILE_PATH }}:$PATH"
task init:ci
task build-dev
cp ./dist/arduino*.whl ./containers/python-apps-base/
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Setup Docker buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: network=host
- name: Build the base Docker image
uses: docker/build-push-action@v6
with:
context: ./containers/python-base
file: ./containers/python-base/Dockerfile
tags: localhost:5000/app-bricks/python-base:latest
platforms: linux/arm64
push: true
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Build the apps base Docker image
uses: docker/build-push-action@v6
with:
context: ./containers/python-apps-base
file: ./containers/python-apps-base/Dockerfile
tags: localhost:5000/app-bricks/python-apps-base:latest
platforms: linux/arm64
push: true
build-args: |
REGISTRY=localhost:5000/
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Pull images for inspection
run: |
docker image pull --platform linux/arm64 localhost:5000/app-bricks/python-base:latest
docker image pull --platform linux/arm64 localhost:5000/app-bricks/python-apps-base:latest
- name: Calculate image sizes
id: sizes
run: |
SIZE1=$(docker images 'localhost:5000/app-bricks/python-base:latest' --format '{{.Size}}')
SIZE2=$(docker images 'localhost:5000/app-bricks/python-apps-base:latest' --format '{{.Size}}')
echo "python_base_size=$SIZE1" >> $GITHUB_OUTPUT
echo "python_apps_base_size=$SIZE2" >> $GITHUB_OUTPUT
- name: Add image sizes to Job Summary
run: |
echo "## Docker Image Sizes" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "| Image | Size |" >> $GITHUB_STEP_SUMMARY
echo "|-------|------|" >> $GITHUB_STEP_SUMMARY
echo "| app-bricks/python-base | ${{ steps.sizes.outputs.python_base_size }} |" >> $GITHUB_STEP_SUMMARY
echo "| app-bricks/python-apps-base | ${{ steps.sizes.outputs.python_apps_base_size }} |" >> $GITHUB_STEP_SUMMARY
outputs:
python_base_size: ${{ steps.sizes.outputs.python_base_size }}
python_apps_base_size: ${{ steps.sizes.outputs.python_apps_base_size }}
pr_number: ${{ steps.pr_info.outputs.pr_number }}
comment-results:
runs-on: ubuntu-latest
needs: build
if: needs.build.outputs.pr_number != ''
permissions:
pull-requests: write
steps:
- name: Comment on PR with image sizes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh pr comment ${{ needs.build.outputs.pr_number }} --repo ${{ github.repository }} --body-file - <<EOF
## Docker Image Sizes
| Image | Size |
|-------|------|
| app-bricks/python-base | ${{ needs.build.outputs.python_base_size }} |
| app-bricks/python-apps-base | ${{ needs.build.outputs.python_apps_base_size }} |
EOF
notify-no-pr:
runs-on: ubuntu-latest
needs: build
if: needs.build.outputs.pr_number == ''
permissions: {}
steps:
- name: Notify to check Job Summary
run: |
echo "ℹ️ No PR found for this workflow run."
echo "📊 Image sizes are available in the Job Summary of the 'build' job."
echo ""
echo "To view:"
echo "1. Go to the 'build' job above"
echo "2. Scroll to the bottom to see the Job Summary"