create_build_info_env

This script will create a key=value file at the given path containing build and source control information.

The given path can either be relative to the base directory (-d) or an absolute path—intermediate directories will not be created; if not given .env will be used.

The following parameters are optional:

d

the base directory ($PWD if not given)

e

add export in front of each key/value pair; also changes the default file name to .envrc

f

overwrite the existing file

p

a prefix for the keys

The value of BUILD_ID is depending on where this script is run:

locally

the current timestamp

AppVeyor

the value of the APPVEYOR_BUILD_ID environment variable

Bitbucket

the value of the BITBUCKET_BUILD_NUMBER environment variable

CircleCI

the value of the CIRCLE_WORKFLOW_ID environment variable

GitHub

the value of the GITHUB_RUN_ID environment variable

GitLab

the value of the CI_PIPELINE_ID environment variable

Jenkins

the value of the BUILD_ID environment variable

TeamCity

the value of the BUILD_NUMBER environment variable

Travis

the value of the TRAVIS_BUILD_ID environment variable

The value of BUILD_TIME is either the value of the SOURCE_DATE_EPOCH environment variable or the current timestamp.

The following will give you the timestamp of the HEAD commit:

SOURCE_DATE_EPOCH="$(git log --max-count=1 --pretty=format:%ct)"

This script will add source control information if the base directory (-d) is under Git version control, i.e., either the top-level directory or one its subdirectories.

/tmp
├── git_repo
│   └── .git
│   └── src
└── other
-d /tmp

source control information will not be added

-d /tmp/git_repo

source control information will be added

-d /tmp/git_repo/src

source control information will be added

-d /tmp/other

source control information will not be added

The value of GIT_COMMIT_ID is the hash of the HEAD commit of the checked-out branch; the suffix -next will be appended if the working tree is dirty.

Usage

$ scripts/web/create_build_info_env.sh

$ scripts/web/create_build_info_env.sh -e
$ scripts/web/create_build_info_js.sh -f /tmp/.env.local
$ scripts/web/create_build_info_js.sh -d ~/git_repo
$ scripts/web/create_build_info_js.sh -p EX_
$ scripts/web/create_build_info_env.sh

.env
# WARNING: will be overwritten by the `scripts/web/create_build_info_env.sh` script
BUILD_ID="1747733549"
BUILD_TIME="2025-05-20T09:32:29Z"
GIT_BRANCH="main"
GIT_COMMIT_ID="a56244680c24bd8224a0121914720df1c101b93b"
GIT_COMMIT_TIME="2025-05-20T09:11:25Z"
$ scripts/web/create_build_info_env.sh -e

.envrc
# WARNING: will be overwritten by the `scripts/web/create_build_info_env.sh` script
export BUILD_ID="1747733591"
export BUILD_TIME="2025-05-20T09:33:11Z"
export GIT_BRANCH="main"
export GIT_COMMIT_ID="a56244680c24bd8224a0121914720df1c101b93b-next"
export GIT_COMMIT_TIME="2025-05-20T09:11:25Z"
$ scripts/web/create_build_info_env.sh -p SD_

.env
# WARNING: will be overwritten by the `scripts/web/create_build_info_env.sh` script
SD_BUILD_ID="1747733611"
SD_BUILD_TIME="2025-05-20T09:33:31Z"
SD_GIT_BRANCH="main"
SD_GIT_COMMIT_ID="a56244680c24bd8224a0121914720df1c101b93b-next"
SD_GIT_COMMIT_TIME="2025-05-20T09:11:25Z"
$ SOURCE_DATE_EPOCH=0 scripts/web/create_build_info_env.sh

.env
# WARNING: will be overwritten by the `scripts/web/create_build_info_env.sh` script
BUILD_ID="1747733640"
BUILD_TIME="1970-01-01T00:00:00Z"
GIT_BRANCH="main"
GIT_COMMIT_ID="a56244680c24bd8224a0121914720df1c101b93b-next"
GIT_COMMIT_TIME="2025-05-20T09:11:25Z"