Files
gitea-actions-demo-project/.gitea/workflows/check_package_version.yml
Nikola Derežić 4b84d265b6
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
(refactor) extract version check workflows into reusable components
- 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>
2026-01-09 16:32:31 +01:00

76 lines
2.7 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"
# 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