(refactor) restructure workflow into two jobs with dependency
All checks were successful
Build and Push Docker Image / check_version (push) Successful in 8s
Build and Push Docker Image / build (push) Has been skipped

Split the workflow into separate check_version and build jobs using the needs keyword for job dependencies. This improves separation of concerns and makes the workflow structure more explicit.

Key changes:
- Created check_version job that outputs version_changed and version
- Build job now depends on check_version using needs keyword
- Build job uses version from check_version output (no re-reading)
- Added fetch-depth: 0 to ensure full git history for comparison
- Removed duplicate version reading step from build job
- Build conditional now uses needs.check_version.outputs.version_changed

Benefits:
- Clear separation between version checking and build logic
- Version is determined once and reused across jobs
- Better observability with separate job statuses in UI
- Enables potential reuse of version outputs by other jobs

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Nikola Derežić
2026-01-09 14:22:35 +01:00
parent 87fe823969
commit a249c599d9

View File

@@ -11,13 +11,17 @@ on:
- main
jobs:
build:
if: github.event_name == 'pull_request' || steps.version-check.outputs.version_changed == 'true'
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
@@ -25,6 +29,7 @@ jobs:
# 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
@@ -53,12 +58,14 @@ jobs:
echo "version_changed=false" >> $GITHUB_OUTPUT
fi
- name: Read package.json version
id: package-version
run: |
VERSION=$(node -p "require('./package.json').version")
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "Building version: $VERSION"
build:
needs: check_version
if: needs.check_version.outputs.version_changed == 'true' || github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
@@ -80,7 +87,7 @@ jobs:
context: .
push: true
tags: |
registry.budakova.org/knee-cola/gitea-actions-demo-project:${{ steps.package-version.outputs.version }}
registry.budakova.org/knee-cola/gitea-actions-demo-project:${{ needs.check_version.outputs.version }}
registry.budakova.org/knee-cola/gitea-actions-demo-project:latest
cache-from: type=registry,ref=registry.budakova.org/knee-cola/gitea-actions-demo-project:buildcache
cache-to: type=registry,ref=registry.budakova.org/knee-cola/gitea-actions-demo-project:buildcache,mode=max