create_build_info_ts

This script will create a TypeScript 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.

The following parameters are optional:

d

the base directory ($PWD if not given)

f

overwrite the existing file

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_ts.sh build-info.ts

$ scripts/web/create_build_info_ts.sh -f /tmp/build-info.ts
$ scripts/web/create_build_info_ts.sh -d ~/git_repo src/build-info.ts
$ scripts/web/create_build_info_ts.sh src/build-info.ts

src/build-info.ts
export type BuildInfo = {
// ...
};

export const buildInfo: BuildInfo = {
  build: {
    id: '1710116078',
    time: '2024-03-11T00:14:38Z',
  },
  git: {
    branch: 'main',
    commit: {
      id: '95189bb08fa918576f10339eb15303d152ade2aa',
      time: '2024-03-10T23:52:54Z',
    },
  },
};
$ SOURCE_DATE_EPOCH=0 scripts/web/create_build_info_ts.sh src/build-info.ts

src/build-info.ts
...
  build: {
...
    time: '1970-01-01T00:00:00Z',
  },
...