-
Notifications
You must be signed in to change notification settings - Fork 5
/
version.sh
executable file
·37 lines (34 loc) · 1.18 KB
/
version.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/bash
SRCDIR=$(dirname $0)
if [ -n "$GITHUB_SHA" ]; then
# We are probably running in a Github workflow.
VERSION_SOURCE_URL="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY"
VERSION_COMMIT_HASH="$GITHUB_SHA"
VERSION_BUILD_URL="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"
if [[ "$GITHUB_REF" =~ ^refs/tags/ ]]; then
VERSION_TAG_NAME="$GITHUB_REF_NAME"
fi
elif [ -n "$CIRCLE_SHA1" ]; then
# We are running in a CircleCI job.
VERSION_SOURCE_URL="https://github.com/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME"
VERSION_COMMIT_HASH="$CIRCLE_SHA1"
VERSION_BUILD_URL="$CIRCLE_BUILD_URL"
VERSION_TAG_NAME="$CIRCLE_TAG"
elif [ -d ${SRCDIR}/.git ]; then
# Otherwise, try to grab version information from the git repository.
VERSION_COMMIT_HASH=$(git -C ${SRCDIR} rev-parse HEAD)
VERSION_SOURCE_URL=$(git -C ${SRCDIR} remote get-url origin)
VERSION_TAG_NAME=$(git -C ${SRCDIR} describe --tags --always)
fi
# Redirect to a file if provided as an argument.
if [ $# -ge 1 ]; then
exec > $1
fi
cat << EOF
{
"source": "${VERSION_SOURCE_URL}",
"commit": "${VERSION_COMMIT_HASH}",
"version": "${VERSION_TAG_NAME}",
"build": "${VERSION_BUILD_URL}"
}
EOF