1+ name : Get version
2+ description : Detects if build is a snapshot and gets the release or snapshot version
3+
4+ runs :
5+ using : composite
6+ steps :
7+ # Checking if this particular build is a snapshot build
8+ - name : Detect if snapshot
9+ id : get-is-snapshot
10+ shell : bash
11+ run : |
12+ # Check if tagged with release or release candidate
13+ TAG=$(git describe --exact-match --tags HEAD 2>/dev/null || echo "")
14+ echo "Detected Git tag: '$TAG'"
15+
16+ # Define regex patterns
17+ RE_PROD='^v?[0-9]+\.[0-9]+\.[0-9]+$'
18+ RE_RC='^v?[0-9]+\.[0-9]+\.[0-9]+-rc[0-9]+$'
19+
20+ if [[ "$TAG" =~ $RE_PROD ]]; then
21+ echo "Production release tag detected: $TAG"
22+ echo "is-snapshot=false" >> $GITHUB_OUTPUT
23+ exit 0
24+ elif [[ "$TAG" =~ $RE_RC ]]; then
25+ echo "Release candidate tag detected: $TAG"
26+ echo "is-snapshot=false" >> $GITHUB_OUTPUT
27+ exit 0
28+ else
29+ echo "No matching tag found"
30+ echo "is-snapshot=true" >> $GITHUB_OUTPUT
31+ fi
32+
33+ # Checking if previous commit contains pom.xml. This should always return true
34+ COMMIT_REF="HEAD~1"
35+ if ! git show "${COMMIT_REF}:pom.xml" &>/dev/null; then
36+ echo "Error: pom.xml not found in commit ${COMMIT_REF}"
37+ echo "is-snapshot='true'" >> $GITHUB_OUTPUT
38+ exit 0
39+ fi
40+
41+ # Getting previous version
42+ OLD_VERSIONS=$(git show "${COMMIT_REF}:pom.xml" | sed -n '0,/<version>/ s/.*<version>\([^<]*\)<\/version>.*/\1/p')
43+
44+ # Getting current version
45+ NEW_VERSIONS=$(sed -n '0,/<version>/ s/.*<version>\([^<]*\)<\/version>.*/\1/p' pom.xml)
46+
47+ echo "old versions: ${OLD_VERSIONS}, new versions: ${NEW_VERSIONS}"
48+ # Compare the extracted versions. CI will not commit snapshot version.
49+ if [[ "${OLD_VERSIONS}" != "${NEW_VERSIONS}" ]]; then
50+ echo "is-snapshot=false" >> $GITHUB_OUTPUT
51+ else
52+ echo "is-snapshot=true" >> $GITHUB_OUTPUT
53+ fi
54+
55+ - name : Get release or snapshot-version
56+ id : get-release-version
57+ shell : bash
58+ run : |
59+ IS_SNAPSHOT=${{ steps.get-is-snapshot.outputs.is-snapshot }}
60+ if [ $IS_SNAPSHOT == "true" ];then
61+ echo release-version="$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)-SNAPSHOT_$GITHUB_SHA" >> $GITHUB_OUTPUT
62+ else
63+ echo release-version="$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)" >> $GITHUB_OUTPUT
64+ fi
65+
66+ outputs :
67+ is-snapshot :
68+ description : Whether this is a snapshot build
69+ value : ${{ steps.get-is-snapshot.outputs.is-snapshot }}
70+ release-version :
71+ description : The release or snapshot version
72+ value : ${{ steps.get-release-version.outputs.release-version }}
0 commit comments