Created
November 13, 2024 21:52
-
-
Save rjchicago/c26a14f54fe36f5f33106436084afeca to your computer and use it in GitHub Desktop.
Example script to generate CI variables for a build process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -Eeou pipefail | |
# args | |
MAIN_BRANCH=${MAIN_BRANCH:-main} | |
BUILD_NUMBER=${BUILD_NUMBER:-0} | |
docker_tag_parse_string() { | |
KEY=$(echo $1 | awk '{print tolower($0)}') | |
re='(.*)[^a-z0-9.-]+(.*)' | |
while [[ $KEY =~ $re ]]; do | |
KEY=${BASH_REMATCH[1]}-${BASH_REMATCH[2]} | |
done | |
echo $KEY | |
} | |
# set variables | |
BRANCH=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p') | |
COMMIT_HASH=$(git rev-parse HEAD) | |
GENERATED_AT="`date -u +%Y-%m-%dT%H:%M:%SZ`"; | |
if [ "$BRANCH" = "$MAIN_BRANCH" ]; then | |
VERSION="`date -u +%Y%m%d`-$BUILD_NUMBER"; | |
else | |
VERSION=$(docker_tag_parse_string $BRANCH) | |
fi | |
# writing to file so Bamboo can read it | |
echo "VERSION=$VERSION | |
BRANCH=$BRANCH | |
BUILD_NUMBER=$BUILD_NUMBER | |
GENERATED_AT=$GENERATED_AT | |
COMMIT_HASH=$COMMIT_HASH" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment