A GitHub Action that automatically generates a build matrix based on Docker base image versions. Perfect for maintaining Docker images that need to support multiple versions of a base image.
- 🔍 Automatic Version Discovery: Fetches available versions from Docker Hub
- 🎯 Flexible Version Selection: Support for latest, all, or semantic version ranges
- đź”§ Configurable Builds: Define multiple build configurations with different Dockerfiles and build arguments
- 📦 Matrix Output: Generates GitHub Actions matrix for parallel builds
- 🚀 Easy Integration: Simple YAML configuration
name: Build Docker Images
on:
push:
branches:
- main
jobs:
generate-matrix:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.generate.outputs.matrix }}
steps:
- uses: actions/checkout@v6
- name: Generate build matrix
id: generate
uses: iwf-web/docker-image-version-matrix-action@v1
with:
base-image: 'verdaccio/verdaccio'
build:
needs: generate-matrix
runs-on: ubuntu-latest
strategy:
matrix: ${{ fromJson(needs.generate-matrix.outputs.matrix) }}
steps:
- uses: actions/checkout@v6
- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v5
with:
context: ${{ matrix.contextPath }}
file: ${{ matrix.dockerfilePath }}
tags: ${{ join(matrix.tags, ',') }}| Input | Description | Required | Default |
|---|---|---|---|
base-image |
The base Docker image (e.g., verdaccio/verdaccio) |
Yes | - |
config-path |
Path to the configuration YAML file | No | .github/base-image-versioner.yml |
version-range |
Version range override (e.g., "latest", "all", "^1", "^1|^2"). |
No | - |
| Output | Description |
|---|---|
matrix |
JSON string containing the build matrix with all combinations |
Create a .github/base-image-versioner.yml file in your repository:
# Strategy: 'latest', 'all', or 'semver'
strategy: latest
# Semver range (when strategy is 'semver')
semverRange: '^1.0.0'
# Maximum number of versions to build
maxVersions: 5
# Build configurations
builds:
- name: default
dockerfilePath: Dockerfile
contextPath: .
buildArgs:
NODE_ENV: production
tags:
- '{version}'
- 'latest'
- name: alpine
dockerfilePath: Dockerfile.alpine
contextPath: .
buildArgs:
VARIANT: alpine
tags:
- '{version}-alpine'
- 'alpine'Build only the most recent version:
strategy: latestBuild all available versions:
strategy: all
maxVersions: 10 # Optional limitBuild versions matching a semver range:
strategy: semver
semverRange: '^1.0.0' # All 1.x.x versionsstrategy: semver
semverRange: '>=2.0.0 <3.0.0' # All 2.x.x versionsUse pipe-separated ranges:
strategy: semver
semverRange: '^1|^2' # All 1.x.x and 2.x.x versionsOr override via workflow input:
with:
base-image: 'node'
version-range: '^18|^20|^22'# .github/base-image-versioner.yml
strategy: latest
builds:
- name: default
dockerfilePath: Dockerfile
tags:
- '{version}'
- 'latest'# .github/base-image-versioner.yml
strategy: semver
semverRange: '^6'
builds:
- name: default
dockerfilePath: Dockerfile
tags:
- '{version}'
- name: alpine
dockerfilePath: Dockerfile.alpine
buildArgs:
VARIANT: alpine
tags:
- '{version}-alpine'
- name: slim
dockerfilePath: Dockerfile.slim
buildArgs:
VARIANT: slim
tags:
- '{version}-slim'name: Build Docker Images
on:
workflow_dispatch:
inputs:
version-range:
description: 'Version range (e.g., "latest", "all", "^1|^2")'
required: false
default: 'latest'
jobs:
generate-matrix:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.generate.outputs.matrix }}
steps:
- uses: actions/checkout@v6
- uses: iwf-web/docker-image-version-matrix-action@v1
id: generate
with:
base-image: 'verdaccio/verdaccio'
version-range: ${{ github.event.inputs.version-range }}The action outputs a JSON matrix in this format:
{
"include": [
{
"version": "1.2.3",
"name": "default",
"dockerfilePath": "Dockerfile",
"contextPath": ".",
"buildArgs": {
"BASE_IMAGE_VERSION": "1.2.3",
"BASE_IMAGE": "verdaccio/verdaccio",
"NODE_ENV": "production"
},
"tags": ["1.2.3", "latest"]
}
]
}-
Install dependencies:
pnpm install
-
Bundle the TypeScript:
pnpm run bundle
-
Run tests:
pnpm test
- Edit TypeScript files in
src/ - Run
pnpm run bundleto compile - Run
pnpm testto ensure tests pass - Commit both
src/anddist/changes
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.