Skip to content

Commit 1e72e4d

Browse files
fix(build): Add prepare-docker-tags action for dynamic Docker tag generation in build workflow. (#25)
1 parent 42fad8a commit 1e72e4d

File tree

2 files changed

+86
-30
lines changed

2 files changed

+86
-30
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
name: Prepare Docker Tags
2+
3+
description: >-
4+
Prepares Docker image tags based on context
5+
(PR, main branch, or feature branch)
6+
7+
inputs:
8+
base-name:
9+
description: Base name for the Docker image.
10+
required: true
11+
build-type:
12+
description: Build type (dev, full, prod).
13+
required: true
14+
php-version:
15+
description: PHP version.
16+
default: "8.4"
17+
required: false
18+
flavor-prefix:
19+
description: Flavor prefix.
20+
default: debian
21+
required: false
22+
23+
outputs:
24+
tags:
25+
description: Generated Docker tags.
26+
value: ${{ steps.generate.outputs.tags }}
27+
php-version:
28+
description: PHP version used.
29+
value: ${{ steps.generate.outputs.php-version }}
30+
flavor:
31+
description: Full flavor string.
32+
value: ${{ steps.generate.outputs.flavor }}
33+
34+
runs:
35+
using: composite
36+
steps:
37+
- name: Generate Tags.
38+
id: generate
39+
shell: bash
40+
env:
41+
BASE_NAME: ${{ inputs.base-name }}
42+
BUILD_TYPE: ${{ inputs.build-type }}
43+
PHP_VERSION: ${{ inputs.php-version }}
44+
FLAVOR_PREFIX: ${{ inputs.flavor-prefix }}
45+
EVENT_NAME: ${{ github.event_name }}
46+
PR_NUMBER: ${{ github.event.number }}
47+
REF: ${{ github.ref }}
48+
SHA: ${{ github.sha }}
49+
run: |
50+
# Prepare flavor
51+
FLAVOR="${FLAVOR_PREFIX}-${BUILD_TYPE}"
52+
53+
# Output common values
54+
echo "php-version=${PHP_VERSION}" >> "$GITHUB_OUTPUT"
55+
echo "flavor=${FLAVOR}" >> "$GITHUB_OUTPUT"
56+
57+
# Generate tags based on context
58+
if [[ "${EVENT_NAME}" == "pull_request" ]]; then
59+
# For PRs: use PR number
60+
TAG="${BASE_NAME}:${PHP_VERSION}-${FLAVOR}-pr-${PR_NUMBER}"
61+
elif [[ "${REF}" == "refs/heads/main" ]]; then
62+
# For main: stable tags + short SHA
63+
SHORT_SHA=$(echo "${SHA}" | cut -c1-7)
64+
TAG="${BASE_NAME}:${PHP_VERSION}-${FLAVOR}"
65+
TAG+=",${BASE_NAME}:${PHP_VERSION}-${FLAVOR}-sha-${SHORT_SHA}"
66+
else
67+
# Other branches: use branch name
68+
BRANCH_NAME="${REF#refs/heads/}"
69+
# Sanitize branch name for Docker tag
70+
BRANCH_NAME=$(echo "${BRANCH_NAME}" | \
71+
sed 's/[^a-zA-Z0-9._-]/-/g')
72+
TAG="${BASE_NAME}:${PHP_VERSION}-${FLAVOR}-${BRANCH_NAME}"
73+
fi
74+
75+
echo "tags=${TAG}" >> "$GITHUB_OUTPUT"

.github/workflows/build.yml

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ concurrency:
2525
name: build
2626

2727
env:
28+
PHP_VERSION: "8.4"
2829
REGISTRY: ghcr.io
2930

3031
jobs:
@@ -43,34 +44,14 @@ jobs:
4344
- name: Setup Docker Buildx.
4445
uses: docker/setup-buildx-action@v3
4546

46-
- name: Prepare tags.
47+
- name: Prepare Tags.
4748
id: tags
48-
env:
49-
BUILD_TYPE: ${{ matrix.build_type }}
50-
PHP_VERSION: "8.4"
51-
run: |
52-
BASE_NAME="${REGISTRY}/${{ github.repository_owner }}/apache"
53-
FLAVOR="debian-${BUILD_TYPE}"
54-
55-
echo "BASE_NAME=${BASE_NAME}" >> "$GITHUB_OUTPUT"
56-
echo "PHP_VERSION=${PHP_VERSION}" >> "$GITHUB_OUTPUT"
57-
58-
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
59-
# For PRs: use PR number
60-
PR_NUMBER=${{ github.event.number }}
61-
TAG="${BASE_NAME}:${PHP_VERSION}-${FLAVOR}-pr-${PR_NUMBER}"
62-
elif [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
63-
# For main: stable tags + short SHA
64-
SHORT_SHA=$(echo "${{ github.sha }}" | cut -c1-7)
65-
TAG="${BASE_NAME}:${PHP_VERSION}-${FLAVOR}"
66-
TAG="${TAG},${BASE_NAME}:${PHP_VERSION}-${FLAVOR}-sha-${SHORT_SHA}"
67-
else
68-
# Other branches: use branch name
69-
BRANCH_NAME="${GITHUB_REF#refs/heads/}"
70-
TAG="${BASE_NAME}:${PHP_VERSION}-${FLAVOR}-${BRANCH_NAME}"
71-
fi
72-
73-
echo "TAG=${TAG}" >> "$GITHUB_OUTPUT"
49+
uses: ./.github/actions/prepare-docker-tags
50+
with:
51+
base-name: "${{ env.REGISTRY }}/${{ github.repository_owner }}/apache"
52+
build-type: ${{ matrix.build_type }}
53+
flavor-prefix: "debian"
54+
php-version: "${{ env.PHP_VERSION }}"
7455

7556
- name: Build Only.
7657
uses: docker/build-push-action@v6
@@ -80,10 +61,10 @@ jobs:
8061
context: .
8162
file: ./src/flavor/apache/Dockerfile
8263
push: false
83-
tags: ${{ steps.tags.outputs.TAG }}
64+
tags: ${{ steps.tags.outputs.tags }}
8465
build-args: |
8566
BUILD_TYPE=${{ matrix.build_type }}
86-
PHP_VERSION=${{ steps.tags.outputs.PHP_VERSION }}
67+
PHP_VERSION=${{ env.PHP_VERSION }}
8768
cache-from: >-
8869
type=gha,scope=${{ env.SCOPE }}-${{ matrix.build_type }}
8970
cache-to: >-
@@ -98,7 +79,7 @@ jobs:
9879
run: |
9980
echo "🎉 Build completed for ${BUILD_TYPE}!"
10081
echo "Event: ${EVENT_NAME}"
101-
echo "Tags: ${{ steps.tags.outputs.TAG }}"
82+
echo "Tags: ${{ steps.tags.outputs.tags }}"
10283
if [[ "${EVENT_NAME}" == "push" ]]; then
10384
echo "✅ Image pushed to registry"
10485
else

0 commit comments

Comments
 (0)