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>
83 lines
2.9 KiB
YAML
83 lines
2.9 KiB
YAML
name: Check Package Version
|
|
|
|
on:
|
|
workflow_call:
|
|
inputs:
|
|
workspacePath:
|
|
description: 'Path relative to repo root where package.json is located'
|
|
required: false
|
|
type: string
|
|
default: '.'
|
|
outputs:
|
|
version_changed:
|
|
description: 'Whether the version changed from the previous commit'
|
|
value: ${{ jobs.check_version.outputs.version_changed }}
|
|
version:
|
|
description: 'Current version from package.json'
|
|
value: ${{ jobs.check_version.outputs.version }}
|
|
|
|
jobs:
|
|
check_version:
|
|
runs-on: ubuntu-latest
|
|
outputs:
|
|
version_changed: ${{ steps.version-check.outputs.version_changed }}
|
|
version: ${{ steps.version-check.outputs.version }}
|
|
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
with:
|
|
fetch-depth: 0
|
|
|
|
- name: Check if version changed
|
|
id: version-check
|
|
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
|
|
|
|
# Get current version
|
|
CURRENT_VERSION=$(node -p "require('./${PACKAGE_JSON_PATH}').version")
|
|
echo "Current version: $CURRENT_VERSION"
|
|
echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
|
|
|
|
# Check if HEAD~1 exists (handle first commit)
|
|
if ! git rev-parse HEAD~1 &>/dev/null; then
|
|
echo "First commit detected, running workflow"
|
|
echo "version_changed=true" >> $GITHUB_OUTPUT
|
|
exit 0
|
|
fi
|
|
|
|
# Check if package.json exists in previous commit
|
|
if ! git show HEAD~1:${PACKAGE_JSON_PATH} &>/dev/null; then
|
|
echo "package.json doesn't exist in previous commit"
|
|
echo "version_changed=true" >> $GITHUB_OUTPUT
|
|
exit 0
|
|
fi
|
|
|
|
# Extract previous version using grep/sed (safer than node for old file)
|
|
PREVIOUS_VERSION=$(git show HEAD~1:${PACKAGE_JSON_PATH} | grep '"version"' | head -1 | sed -E 's/.*"version"\s*:\s*"([^"]+)".*/\1/')
|
|
echo "Previous version: $PREVIOUS_VERSION"
|
|
|
|
# Validate extraction
|
|
if [ -z "$PREVIOUS_VERSION" ]; then
|
|
echo "Warning: Could not extract previous version, assuming changed"
|
|
echo "version_changed=true" >> $GITHUB_OUTPUT
|
|
exit 0
|
|
fi
|
|
|
|
# Compare versions
|
|
if [ "$CURRENT_VERSION" != "$PREVIOUS_VERSION" ]; then
|
|
echo "Version changed: $PREVIOUS_VERSION -> $CURRENT_VERSION"
|
|
echo "version_changed=true" >> $GITHUB_OUTPUT
|
|
else
|
|
echo "Version unchanged: $CURRENT_VERSION"
|
|
echo "version_changed=false" >> $GITHUB_OUTPUT
|
|
fi
|