Implements automated Docker image building and publishing to registry: - build.yml: Main workflow that builds and pushes Docker images to registry - Triggers on push to master branch - Only builds when image with current version doesn't exist - Uses Docker BuildKit with layer caching for faster builds - Tags images with both version number and 'latest' - check_image_version.yml: Reusable workflow to verify image existence - Reads version from package.json - Uses lightweight manifest inspection (no image download) - Returns image_exists and version as outputs - check_package_version.yml: Reusable workflow to detect version changes - Compares version between commits - Handles edge cases (first commit, missing package.json) - Includes validation for version extraction failures All workflows include proper error handling and clear logging. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
95 lines
3.2 KiB
YAML
95 lines
3.2 KiB
YAML
name: Check Image Version
|
|
|
|
on:
|
|
workflow_call:
|
|
inputs:
|
|
workspacePath:
|
|
description: 'Path relative to repo root where package.json is located'
|
|
required: false
|
|
type: string
|
|
default: '.'
|
|
imageName:
|
|
description: 'Docker image name without registry FQDN or username'
|
|
required: true
|
|
type: string
|
|
registryUrl:
|
|
description: 'Docker registry URL (e.g., registry.budakova.org)'
|
|
required: false
|
|
type: string
|
|
default: 'registry.budakova.org'
|
|
registryUsername:
|
|
description: 'Docker registry username'
|
|
required: true
|
|
type: string
|
|
registryNamespace:
|
|
description: 'Docker registry namespace/organization (e.g., knee-cola)'
|
|
required: true
|
|
type: string
|
|
secrets:
|
|
registryToken:
|
|
description: 'Registry access token'
|
|
required: true
|
|
outputs:
|
|
image_exists:
|
|
description: 'Whether the image exists in the registry'
|
|
value: ${{ jobs.check_image.outputs.image_exists }}
|
|
version:
|
|
description: 'Current version from package.json'
|
|
value: ${{ jobs.check_image.outputs.version }}
|
|
|
|
jobs:
|
|
check_image:
|
|
runs-on: ubuntu-latest
|
|
outputs:
|
|
image_exists: ${{ steps.manifest-check.outputs.image_exists }}
|
|
version: ${{ steps.version-read.outputs.version }}
|
|
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Read version from package.json
|
|
id: version-read
|
|
run: |
|
|
WORKSPACE_PATH="${{ inputs.workspacePath }}"
|
|
# Clean up path - remove trailing slash if present
|
|
WORKSPACE_PATH="${WORKSPACE_PATH%/}"
|
|
# Handle root directory case
|
|
if [ "$WORKSPACE_PATH" = "." ]; then
|
|
PACKAGE_JSON_PATH="package.json"
|
|
else
|
|
PACKAGE_JSON_PATH="${WORKSPACE_PATH}/package.json"
|
|
fi
|
|
|
|
VERSION=$(node -p "try { require('./${PACKAGE_JSON_PATH}').version } catch(e) { console.error('Error reading version:', e.message); process.exit(1) }") || {
|
|
echo "Error: Failed to read version from ${PACKAGE_JSON_PATH}"
|
|
exit 1
|
|
}
|
|
echo "version=$VERSION" >> $GITHUB_OUTPUT
|
|
echo "Checking for image version: $VERSION"
|
|
|
|
- name: Login to Registry
|
|
run: |
|
|
echo "${{ secrets.registryToken }}" | docker login ${{ inputs.registryUrl }} -u "${{ inputs.registryUsername }}" --password-stdin
|
|
|
|
- name: Check if image exists in registry
|
|
id: manifest-check
|
|
run: |
|
|
VERSION=${{ steps.version-read.outputs.version }}
|
|
IMAGE="${{ inputs.registryUrl }}/${{ inputs.registryNamespace }}/${{ inputs.imageName }}:${VERSION}"
|
|
|
|
echo "Checking manifest for image: $IMAGE"
|
|
|
|
if docker manifest inspect "$IMAGE" &>/dev/null; then
|
|
echo "Image exists in registry"
|
|
echo "image_exists=true" >> $GITHUB_OUTPUT
|
|
else
|
|
echo "Image does not exist in registry"
|
|
echo "image_exists=false" >> $GITHUB_OUTPUT
|
|
fi
|
|
|
|
- name: Summary
|
|
run: |
|
|
echo "Version: ${{ steps.version-read.outputs.version }}"
|
|
echo "Image exists: ${{ steps.manifest-check.outputs.image_exists }}"
|