Skip to content

Commit 955142b

Browse files
authored
feat: enhance git-version script to generate pseudo-versions with timestamp (#4553)
Signed-off-by: maksim.nabokikh <max.nabokih@gmail.com>
1 parent adec8b4 commit 955142b

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

.github/workflows/artifacts.yaml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ jobs:
5252
steps:
5353
- name: Checkout repository
5454
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
55+
with:
56+
fetch-tags: true
5557

5658
- name: Set up QEMU
5759
uses: docker/setup-qemu-action@c7c53464625b32c7a7e944ae62b3e17d2b600130 # v3.7.0
@@ -90,6 +92,12 @@ jobs:
9092
labels: |
9193
org.opencontainers.image.documentation=https://dexidp.io/docs/
9294
95+
# Multiple exporters are not supported yet
96+
# See https://github.com/moby/buildkit/pull/2760
97+
- name: Get version from git-version script
98+
id: version
99+
run: echo "value=$(bash ./scripts/git-version)" >> "$GITHUB_OUTPUT"
100+
93101
# Multiple exporters are not supported yet
94102
# See https://github.com/moby/buildkit/pull/2760
95103
- name: Determine build output
@@ -124,10 +132,11 @@ jobs:
124132
tags: ${{ steps.meta.outputs.tags }}
125133
build-args: |
126134
BASE_IMAGE=${{ matrix.variant }}
127-
VERSION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}
135+
VERSION=${{ steps.version.outputs.value }}
128136
COMMIT_HASH=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}
129137
BUILD_DATE=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}
130-
labels: ${{ steps.meta.outputs.labels }}
138+
labels: |
139+
${{ steps.meta.outputs.labels }}
131140
# cache-from: type=gha
132141
# cache-to: type=gha,mode=max
133142
outputs: ${{ steps.build-output.outputs.value }}

scripts/git-version

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,42 @@
11
#!/bin/sh -e
22

33
# parse the current git commit hash
4-
COMMIT=`git rev-parse HEAD`
4+
COMMIT=`git rev-parse --short=8 HEAD`
55

6-
# check if the current commit has a matching tag
7-
TAG=$(git describe --exact-match --abbrev=0 --tags ${COMMIT} 2> /dev/null || true)
6+
# check if the current commit has a matching tag (filter for v* tags, excluding api/)
7+
TAG=$(git describe --exact-match --abbrev=0 --tags --match="v[0-9]*" 2> /dev/null || true)
88

99
# use the matching tag as the version, if available
1010
if [ -z "$TAG" ]; then
11-
VERSION=$COMMIT
11+
# No exact tag on current commit, find the last version tag and bump minor version
12+
# Get all tags matching v[0-9]*, sort them, and take the last one
13+
LAST_TAG=$(git tag --list "v[0-9]*" --sort=-version:refname | head -1)
14+
15+
if [ -z "$LAST_TAG" ]; then
16+
# No tags found, use v0.1.0 as fallback
17+
BASE_VERSION="v0.1.0"
18+
else
19+
# Parse the last tag and bump minor version
20+
# Remove 'v' prefix
21+
TAG_WITHOUT_V="${LAST_TAG#v}"
22+
23+
# Split version into parts (major.minor.patch)
24+
MAJOR=$(echo "$TAG_WITHOUT_V" | cut -d. -f1)
25+
MINOR=$(echo "$TAG_WITHOUT_V" | cut -d. -f2)
26+
PATCH=$(echo "$TAG_WITHOUT_V" | cut -d. -f3)
27+
28+
# Bump minor version
29+
MINOR=$((MINOR + 1))
30+
31+
# Construct base version with bumped minor
32+
BASE_VERSION="v${MAJOR}.${MINOR}.0"
33+
fi
34+
35+
# Get commit timestamp in YYYYMMDDhhmmss format
36+
TIMESTAMP=$(git log -1 --format=%ci HEAD | sed 's/[-: ]//g' | cut -c1-14)
37+
38+
# Construct pseudo-version
39+
VERSION="${BASE_VERSION}-${TIMESTAMP}-${COMMIT}"
1240
else
1341
VERSION=$TAG
1442
fi

0 commit comments

Comments
 (0)