(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>
This commit is contained in:
@@ -15,96 +15,23 @@ jobs:
|
|||||||
# Outputs the current version and whether it changed from the last commit
|
# Outputs the current version and whether it changed from the last commit
|
||||||
# This is used to determine if a new release/build is needed
|
# This is used to determine if a new release/build is needed
|
||||||
check_version:
|
check_version:
|
||||||
runs-on: ubuntu-latest
|
uses: ./.gitea/workflows/check_package_version.yml
|
||||||
outputs:
|
|
||||||
version_changed: ${{ steps.version-check.outputs.version_changed }}
|
|
||||||
version: ${{ steps.version-check.outputs.version }}
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Checkout code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
workspacePath: '.'
|
||||||
|
|
||||||
- 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
|
|
||||||
|
|
||||||
# Verifies if Docker image with current version already exists in registry
|
# Verifies if Docker image with current version already exists in registry
|
||||||
# This prevents rebuilding the same version but allows pulls and version changes
|
# This prevents rebuilding the same version but allows pulls and version changes
|
||||||
# to always trigger new builds. Uses lightweight manifest inspect (no download)
|
# to always trigger new builds. Uses lightweight manifest inspect (no download)
|
||||||
check_image_version:
|
check_image_version:
|
||||||
runs-on: ubuntu-latest
|
uses: ./.gitea/workflows/check_image_version.yml
|
||||||
outputs:
|
with:
|
||||||
image_exists: ${{ steps.manifest-check.outputs.image_exists }}
|
workspacePath: '.'
|
||||||
version: ${{ steps.version-read.outputs.version }}
|
imageName: 'gitea-actions-demo-project'
|
||||||
|
registryUrl: 'registry.budakova.org'
|
||||||
steps:
|
registryUsername: ${{ vars.PROFILE_REGISTRY_USERNAME }}
|
||||||
- name: Checkout code
|
registryNamespace: 'knee-cola'
|
||||||
uses: actions/checkout@v4
|
secrets:
|
||||||
|
registryToken: ${{ secrets.PROFILE_REGISTRY_TOKEN }}
|
||||||
- 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 }}"
|
|
||||||
|
|
||||||
# Builds and pushes Docker image to registry if conditions are met:
|
# Builds and pushes Docker image to registry if conditions are met:
|
||||||
# - Version changed in package.json, OR
|
# - Version changed in package.json, OR
|
||||||
|
|||||||
91
.gitea/workflows/check_image_version.yml
Normal file
91
.gitea/workflows/check_image_version.yml
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
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 "require('./${PACKAGE_JSON_PATH}').version")
|
||||||
|
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 }}"
|
||||||
75
.gitea/workflows/check_package_version.yml
Normal file
75
.gitea/workflows/check_package_version.yml
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
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
|
||||||
Reference in New Issue
Block a user