Skip to content

Instantly share code, notes, and snippets.

@ustun
Last active June 5, 2020 09:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ustun/b14dc73d03dca3cf1319a08ee1772120 to your computer and use it in GitHub Desktop.
Save ustun/b14dc73d03dca3cf1319a08ee1772120 to your computer and use it in GitHub Desktop.
Azure Devops Set Build Number for Master & PRs
#!/bin/bash
# Script to set build number in Azure Devops
# We want build number of the following form:
# "build master efab0 20190505 12:12"
# "build pr feature/foo efab0 20190505 12:12"
# Builtin Azure Devops build numbering is insufficient, so we override with this script.
date_str=$(date "+%Y%m%d %H%M")
if [[ -z "${SYSTEM_PULLREQUEST_SOURCEBRANCH}" ]]; then
# master
sha=${BUILD_SOURCEVERSION:0:5}
build_number="build of ${BUILD_SOURCEBRANCHNAME} ${sha} ${date_str}"
else
# pr
sha=${SYSTEM_PULLREQUEST_SOURCECOMMITID:0:5}
build_number="build of pr ${SYSTEM_PULLREQUEST_SOURCEBRANCH} ${sha} ${date_str}"
fi
# TF209010: The build number format build master ded3a 20190628 08:22 contains invalid character(s), is too long, or ends with '.'. The maximum length of a build number is 255 characters. Characters which are not allowed include '"', '/', ':', '<', '>', '\', '|', '?', '@', and '*'.
# get rid of forward slashes (some PR's have this, e.g. feature/foobar
cleaned_build_number=$(sed "s/\//_/g" <<<"$build_number")
# see: https://docs.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?view=azure-devops&tabs=bash
# see: https://github.com/microsoft/azure-pipelines-tasks/blob/master/docs/authoring/commands.md
echo "##vso[build.updatebuildnumber]${cleaned_build_number}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment