(refactor) extract version check workflows into reusable components
All checks were successful
Build and Push Docker Image / check_version (push) Successful in 8s
Build and Push Docker Image / check_image_version (push) Successful in 9s
Build and Push Docker Image / build (push) Has been skipped

- Create check_package_version.yml reusable workflow
  - Add workspacePath input for flexible package.json location
  - Fix path concatenation to handle root directory cleanly
  - Expose version_changed and version outputs

- Create check_image_version.yml reusable workflow
  - Add workspacePath, imageName, registryUrl, registryUsername, and registryNamespace inputs
  - Make fully generic and portable across different registries
  - Fix path handling for clean path generation
  - Rename secret to registryToken for clarity

- Update build.yml to use reusable workflows
  - Replace inline jobs with workflow_call references
  - Pass all required parameters explicitly
  - Maintain existing conditional logic in build job

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Nikola Derežić
2026-01-09 16:32:31 +01:00
parent 29b479dcaf
commit 4b84d265b6
3 changed files with 178 additions and 85 deletions

View File

@@ -15,96 +15,23 @@ jobs:
# Outputs the current version and whether it changed from the last commit
# This is used to determine if a new release/build is needed
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: |
# Get current version
CURRENT_VERSION=$(node -p "require('./package.json').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 &>/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 | grep '"version"' | head -1 | sed -E 's/.*"version"\s*:\s*"([^"]+)".*/\1/')
echo "Previous version: $PREVIOUS_VERSION"
# 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
uses: ./.gitea/workflows/check_package_version.yml
with:
workspacePath: '.'
# Verifies if Docker image with current version already exists in registry
# This prevents rebuilding the same version but allows pulls and version changes
# to always trigger new builds. Uses lightweight manifest inspect (no download)
check_image_version:
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: |
VERSION=$(node -p "require('./package.json').version")
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "Checking for image version: $VERSION"
- name: Login to Registry
run: |
echo "${{ secrets.PROFILE_REGISTRY_TOKEN }}" | docker login registry.budakova.org -u "${{ vars.PROFILE_REGISTRY_USERNAME }}" --password-stdin
- name: Check if image exists in registry
id: manifest-check
run: |
VERSION=${{ steps.version-read.outputs.version }}
IMAGE="registry.budakova.org/knee-cola/gitea-actions-demo-project:${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 }}"
uses: ./.gitea/workflows/check_image_version.yml
with:
workspacePath: '.'
imageName: 'gitea-actions-demo-project'
registryUrl: 'registry.budakova.org'
registryUsername: ${{ vars.PROFILE_REGISTRY_USERNAME }}
registryNamespace: 'knee-cola'
secrets:
registryToken: ${{ secrets.PROFILE_REGISTRY_TOKEN }}
# Builds and pushes Docker image to registry if conditions are met:
# - Version changed in package.json, OR