Skip to content

Commit 383dfae

Browse files
authored
feat: make GIT_BRANCH and GIT_COMMIT_HASH available as build args (#219)
* feat: make GIT_BRANCH and GIT_COMMIT available as build args * Fix injection problems * Make actual action for it * Lint fix * Rename to commit_hash * Change workflow branch from 'mp/docker_branch' to 'main'
1 parent 350dcc0 commit 383dfae

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: get-branch
2+
description: extracts the branch in readable form
3+
author: datavisyn
4+
5+
inputs:
6+
branch:
7+
description: "branch override"
8+
required: false
9+
10+
outputs:
11+
branch:
12+
description: "extracted branch"
13+
value: ${{ steps.get-branch.outputs.branch }}
14+
commit_hash:
15+
description: "extracted commit hash"
16+
value: ${{ steps.get-branch.outputs.commit_hash }}
17+
18+
runs:
19+
using: "composite"
20+
steps:
21+
- name: Extract branch and commit hash
22+
shell: bash
23+
id: get-branch
24+
run: |
25+
# Use HEAD_REF for PRs, fallback to REF_NAME for pushes.
26+
BRANCH_NAME_RAW="${HEAD_REF:-"$REF_NAME"}"
27+
# Clean up the ref by removing 'refs/heads/' or 'refs/tags/'
28+
BRANCH_NAME="${BRANCH_NAME_RAW#refs/heads/}"
29+
BRANCH_NAME="${BRANCH_NAME#refs/tags/}"
30+
31+
# A manual input from workflow_dispatch will always override the detected branch name
32+
if [ -n "${MANUAL_BRANCH}" ]; then
33+
BRANCH_NAME="${MANUAL_BRANCH}"
34+
fi
35+
36+
echo "branch=${BRANCH_NAME}" >> $GITHUB_OUTPUT
37+
echo "commit_hash=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
38+
env:
39+
HEAD_REF: ${{ github.head_ref }}
40+
REF_NAME: ${{ github.ref_name }}
41+
MANUAL_BRANCH: ${{ inputs.branch }}

.github/workflows/build-docker-artifacts.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ jobs:
211211
id: login-ecr
212212
uses: aws-actions/[email protected]
213213

214+
- uses: ./tmp/github-workflows/.github/actions/get-branch
215+
id: get-branch
216+
with:
217+
branch: ${{ inputs.branch }}
218+
214219
- name: Build image
215220
uses: docker/build-push-action@v6
216221
with:
@@ -223,6 +228,8 @@ jobs:
223228
# Disable the cache to avoid outdated (base) images
224229
no-cache: true
225230
build-args: |
231+
GIT_BRANCH=${{ steps.get-branch.outputs.branch }}
232+
GIT_COMMIT_HASH=${{ steps.get-branch.outputs.commit_hash }}
226233
DOCKERFILE_DIRECTORY=${{ matrix.component.flavor_directory }}/${{ matrix.component.directory }}
227234
PYTHON_BASE_IMAGE=${{ env.PYTHON_BASE_IMAGE }}
228235
DATAVISYN_PYTHON_BASE_IMAGE=${{ env.DATAVISYN_PYTHON_BASE_IMAGE }}

0 commit comments

Comments
 (0)