Skip to content

Commit 9acd8eb

Browse files
newbe36524claude
andcommitted
fix: resolve Docker build failures due to undefined template variables and version validation issues
- Add version prefix cleaning logic before validation to handle 'v' prefix from Git tags - Define BUILD_DATE, BASE_IMAGE, and CLEAN_VERSION template variables before Dockerfile generation - Add fallback logic for CLEAN_VERSION variable - Add Dockerfile validation step to check for valid FROM directive - Add debug logging for template variables and generated Dockerfile content Fixes issues where: - Git tags with 'v' prefix (e.g., v0.1.0-beta.16) failed version validation - Template variables CLEAN_VERSION, BUILD_DATE, and BASE_IMAGE were undefined causing Docker build failures Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0258ede commit 9acd8eb

1 file changed

Lines changed: 63 additions & 3 deletions

File tree

.github/workflows/docker-build.yml

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,30 @@ jobs:
128128
fi
129129
echo "version=${VERSION}" >> $GITHUB_OUTPUT
130130
131+
# Clean version by removing 'v' prefix (for Git tags and other sources)
132+
if [[ "${VERSION}" == v* ]]; then
133+
CLEAN_VERSION="${VERSION#v}"
134+
echo "Removed 'v' prefix from version: ${VERSION} -> ${CLEAN_VERSION}"
135+
else
136+
CLEAN_VERSION="${VERSION}"
137+
echo "Using version as-is (no 'v' prefix): ${VERSION}"
138+
fi
139+
131140
# Version format validation (skip for 'latest')
132141
if [ "${VERSION}" != "latest" ]; then
133142
# Version must start with a digit and contain only letters, numbers, dots, hyphens, or underscores
134-
if ! [[ "${VERSION}" =~ ^[0-9][0-9A-Za-z._-]*$ ]]; then
135-
echo "::error::Invalid version format '${VERSION}'"
143+
if ! [[ "${CLEAN_VERSION}" =~ ^[0-9][0-9A-Za-z._-]*$ ]]; then
144+
echo "::error::Invalid version format '${CLEAN_VERSION}'"
136145
echo "Version must start with a digit and only contain letters, numbers, dots, hyphens, or underscores."
137146
echo "Example: 1.2.3, 1.2.3-beta.1"
138147
exit 1
139148
fi
140-
echo "✓ Version format validated: ${VERSION}"
149+
echo "✓ Version format validated: ${CLEAN_VERSION}"
141150
fi
142151
152+
# Output CLEAN_VERSION for use in later steps
153+
echo "clean_version=${CLEAN_VERSION}" >> $GITHUB_OUTPUT
154+
143155
# Determine platform - default to 'all' if not specified
144156
if [ -z "${{ inputs.platform }}" ]; then
145157
# When platform is not specified or empty, default to 'all'
@@ -204,6 +216,37 @@ jobs:
204216
fi
205217
fi
206218
219+
- name: Define Template Variables
220+
if: steps.config.outputs.version != 'latest'
221+
run: |
222+
VERSION="${{ steps.config.outputs.version }}"
223+
CLEAN_VERSION="${{ steps.config.outputs.clean_version }}"
224+
IMAGE_NAME="hagicode"
225+
REGISTRY="${{ secrets.AZURE_ACR_REGISTRY }}"
226+
227+
# Define build date in ISO 8601 UTC format
228+
BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
229+
230+
# Define base image name
231+
BASE_IMAGE="${REGISTRY}/${IMAGE_NAME}:base"
232+
233+
# Ensure CLEAN_VERSION has a fallback value
234+
if [[ -z "${CLEAN_VERSION}" ]]; then
235+
CLEAN_VERSION="${VERSION}"
236+
echo "Using VERSION as CLEAN_VERSION (clean_version output was empty)"
237+
fi
238+
239+
# Output template variables for debugging
240+
echo "Template variables:"
241+
echo " CLEAN_VERSION: ${CLEAN_VERSION}"
242+
echo " BUILD_DATE: ${BUILD_DATE}"
243+
echo " BASE_IMAGE: ${BASE_IMAGE}"
244+
245+
# Set environment variables for the next step
246+
echo "CLEAN_VERSION=${CLEAN_VERSION}" >> $GITHUB_ENV
247+
echo "BUILD_DATE=${BUILD_DATE}" >> $GITHUB_ENV
248+
echo "BASE_IMAGE=${BASE_IMAGE}" >> $GITHUB_ENV
249+
207250
- name: Download and Extract Application
208251
if: steps.config.outputs.version != 'latest'
209252
env:
@@ -268,6 +311,23 @@ jobs:
268311
sed "s|{base_image_name}|${BASE_IMAGE}|g" \
269312
> output/docker-build-context/Dockerfile.app
270313
314+
# Verify generated Dockerfile contains valid FROM directive
315+
if ! grep -q "^FROM " output/docker-build-context/Dockerfile.app; then
316+
echo "::error::Generated Dockerfile is missing FROM directive"
317+
echo "This indicates that template variables were not properly substituted."
318+
echo "CLEAN_VERSION: ${CLEAN_VERSION}"
319+
echo "BUILD_DATE: ${BUILD_DATE}"
320+
echo "BASE_IMAGE: ${BASE_IMAGE}"
321+
exit 1
322+
fi
323+
324+
# Log generated Dockerfile for debugging
325+
echo "Generated Dockerfile:"
326+
echo "---"
327+
cat output/docker-build-context/Dockerfile.app
328+
echo "---"
329+
echo "✓ Dockerfile validated successfully"
330+
271331
# Copy application files from download to build context
272332
cp -r output/download/lib output/docker-build-context/
273333
# Copy entrypoint script

0 commit comments

Comments
 (0)