Skip to content

Commit a1b027d

Browse files
fix(build): Enhance Docker tag preparation and add version validation action. (#27)
1 parent edf7e79 commit a1b027d

File tree

4 files changed

+207
-159
lines changed

4 files changed

+207
-159
lines changed

.github/actions/prepare-docker-tags/action.yml

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Prepare Docker Tags
22

33
description: >-
44
Prepares Docker image tags based on context
5-
(PR, main branch, or feature branch)
5+
(PR, main branch, feature branch, or release)
66
77
inputs:
88
base-name:
@@ -19,6 +19,13 @@ inputs:
1919
description: Flavor prefix.
2020
default: debian
2121
required: false
22+
is-release:
23+
description: Whether this is a release build.
24+
default: "false"
25+
required: false
26+
version:
27+
description: Release version (for releases only).
28+
required: false
2229

2330
outputs:
2431
tags:
@@ -40,12 +47,14 @@ runs:
4047
env:
4148
BASE_NAME: ${{ inputs.base-name }}
4249
BUILD_TYPE: ${{ inputs.build-type }}
43-
PHP_VERSION: ${{ inputs.php-version }}
44-
FLAVOR_PREFIX: ${{ inputs.flavor-prefix }}
4550
EVENT_NAME: ${{ github.event_name }}
51+
FLAVOR_PREFIX: ${{ inputs.flavor-prefix }}
52+
IS_RELEASE: ${{ inputs.is-release }}
53+
PHP_VERSION: ${{ inputs.php-version }}
4654
PR_NUMBER: ${{ github.event.number }}
4755
REF: ${{ github.ref }}
4856
SHA: ${{ github.sha }}
57+
VERSION: ${{ inputs.version }}
4958
run: |
5059
# Prepare flavor
5160
FLAVOR="${FLAVOR_PREFIX}-${BUILD_TYPE}"
@@ -55,7 +64,18 @@ runs:
5564
echo "flavor=${FLAVOR}" >> "$GITHUB_OUTPUT"
5665
5766
# Generate tags based on context
58-
if [[ "${EVENT_NAME}" == "pull_request" ]]; then
67+
if [[ "${IS_RELEASE}" == "true" && -n "${VERSION}" ]]; then
68+
# Release context: similar to regular builds but with SHA
69+
SHORT_SHA=$(echo "${SHA}" | cut -c1-7)
70+
TAG="${BASE_NAME}:${PHP_VERSION}-${FLAVOR}-v${VERSION}"
71+
TAG+=",${BASE_NAME}:${PHP_VERSION}-${FLAVOR}-sha-${SHORT_SHA}"
72+
73+
# Latest tag only for prod builds
74+
if [[ "$BUILD_TYPE" == "prod" ]]; then
75+
TAG+=",${BASE_NAME}:latest"
76+
fi
77+
78+
elif [[ "${EVENT_NAME}" == "pull_request" ]]; then
5979
# For PRs: use PR number
6080
TAG="${BASE_NAME}:${PHP_VERSION}-${FLAVOR}-pr-${PR_NUMBER}"
6181
elif [[ "${REF}" == "refs/heads/main" ]]; then
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
name: Validate Version
2+
3+
description: >-
4+
Validates and extracts version from git tags or workflow inputs.
5+
Supports semantic versioning validation.
6+
7+
inputs:
8+
allow-prerelease:
9+
description: Allow pre-release versions (for example, 1.0.0-beta.1).
10+
default: "false"
11+
required: false
12+
input-version:
13+
description: Manual version input (from workflow_dispatch).
14+
required: false
15+
16+
outputs:
17+
is_prerelease:
18+
description: Whether this is a pre-release version.
19+
value: ${{ steps.validate.outputs.is_prerelease }}
20+
should_build:
21+
description: Whether the build should proceed.
22+
value: ${{ steps.validate.outputs.should_build }}
23+
source:
24+
description: Source of the version (tag, manual, or none).
25+
value: ${{ steps.validate.outputs.source }}
26+
version:
27+
description: Extracted and validated version.
28+
value: ${{ steps.validate.outputs.version }}
29+
30+
runs:
31+
using: composite
32+
steps:
33+
- name: Extract and validate version
34+
id: validate
35+
shell: bash
36+
env:
37+
ALLOW_PRERELEASE: ${{ inputs.allow-prerelease }}
38+
EVENT_DISPATCH: ${{ github.event_name == 'workflow_dispatch' }}
39+
EVENT_NAME: ${{ github.event_name }}
40+
INPUT_VERSION: ${{ inputs.input-version }}
41+
REF_NAME: ${{ github.ref_name }}
42+
run: |
43+
set -Eeuo pipefail
44+
45+
VERSION=""
46+
SHOULD_BUILD="false"
47+
IS_PRERELEASE="false"
48+
SOURCE="none"
49+
50+
# Extract version based on trigger
51+
if [[ "${REF_NAME:-}" == v* ]]; then
52+
# From git tag
53+
VERSION="${REF_NAME#v}"
54+
SOURCE="tag"
55+
echo "📍 Version from tag: $VERSION"
56+
elif [[ "$EVENT_DISPATCH" == "true" && -n "$INPUT_VERSION" ]]; then
57+
# From manual input
58+
VERSION="$INPUT_VERSION"
59+
SOURCE="manual"
60+
echo "📍 Version from manual input: $VERSION"
61+
else
62+
echo "⚠️ No version source found"
63+
echo "version=" >> "$GITHUB_OUTPUT"
64+
echo "should_build=false" >> "$GITHUB_OUTPUT"
65+
echo "is_prerelease=false" >> "$GITHUB_OUTPUT"
66+
echo "source=none" >> "$GITHUB_OUTPUT"
67+
exit 0
68+
fi
69+
70+
# Validate semantic version patterns
71+
SEMVER_REGEX="^[0-9]+\.[0-9]+\.[0-9]+$"
72+
PRERELEASE_REGEX="^[0-9]+\.[0-9]+\.[0-9]+-.+$"
73+
74+
if [[ "$VERSION" =~ $SEMVER_REGEX ]]; then
75+
# Valid stable version
76+
SHOULD_BUILD="true"
77+
IS_PRERELEASE="false"
78+
echo "✅ Valid stable version: $VERSION"
79+
elif [[ "$ALLOW_PRERELEASE" == "true" ]] && \
80+
[[ "$VERSION" =~ $PRERELEASE_REGEX ]]; then
81+
# Valid pre-release version
82+
SHOULD_BUILD="true"
83+
IS_PRERELEASE="true"
84+
echo "✅ Valid pre-release version: $VERSION"
85+
else
86+
# Invalid version format
87+
SHOULD_BUILD="false"
88+
echo "❌ Invalid version format: $VERSION"
89+
echo "Expected: X.Y.Z or X.Y.Z-suffix (if prerelease allowed)"
90+
fi
91+
92+
if [[ "$IS_PRERELEASE" == "true" ]]; then
93+
TYPE="Pre-release"
94+
else
95+
TYPE="Stable"
96+
fi
97+
98+
if [[ "$SHOULD_BUILD" == "true" ]]; then
99+
BUILD_STATUS="✅ Yes"
100+
else
101+
BUILD_STATUS="❌ No"
102+
fi
103+
104+
# Output results
105+
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
106+
echo "should_build=$SHOULD_BUILD" >> "$GITHUB_OUTPUT"
107+
echo "is_prerelease=$IS_PRERELEASE" >> "$GITHUB_OUTPUT"
108+
echo "source=$SOURCE" >> "$GITHUB_OUTPUT"
109+
110+
# Summary for GitHub Actions UI
111+
{
112+
echo "### 🏷️ Version Validation"
113+
echo "- **Version:** \`$VERSION\`"
114+
echo "- **Source:** $SOURCE"
115+
echo "- **Type:** $TYPE"
116+
echo "- **Should Build:** $BUILD_STATUS"
117+
} >> "$GITHUB_STEP_SUMMARY"

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ jobs:
5555
base-name: "${{ env.REGISTRY }}/${{ github.repository_owner }}/apache"
5656
build-type: ${{ matrix.build_type }}
5757
flavor-prefix: "debian"
58+
is-release: "false"
5859
php-version: "${{ env.PHP_VERSION }}"
5960

6061
- name: Build Only.

0 commit comments

Comments
 (0)