A NestJS module that exposes Git build information via an API endpoint. Automatically captures and exposes Git metadata during your build process which is useful for debugging which version of the code is running in live environments.
Once installed, this module:
-
Automatically captures Git metadata during your build process including:
- Git commit SHA
- Branch name
- Build timestamp
- Package version
- Custom environment variables you specify
-
Exposes this information via a REST endpoint (default:
/git-info
):
{
"version": "1.0.0",
"githubSHA": "a1b2c3d4e5f6...",
"githubBranch": "main",
"buildTime": "2024-01-01T00:00:00.000Z"
}
This is particularly valuable when:
- Debugging production issues and need to verify which code version is deployed
- Managing multiple environments and need to track deployments
The module assumes you are using GitHub Actions, automatically capturing their environment variables during the build process.
npm install nest-git-info
- Install module in your code:
import { GitInfoModule } from 'nest-git-info';
@Module({ imports: [GitInfoModule.register()] })
export class AppModule {}
- Add a prebuild script (or to your existing prebuild script) so that the git environment details can be added at build time. By running this at build time, we avoid issues with docker cache layers in the
npm install
.
{
"scripts": {
"prebuild": "nest-git-info",
"build": "nest build"
}
}
- Add the build info file to your
.gitignore
:
src/build-info.json
- Make sure you have the following in your
nest-cli.json
to ensure the build info is compiled into the build output:
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true,
"assets": [
"**/*.json"
]
}
}
The prebuild
script will automatically run before your build, generating the Git information file. This works in both local development and CI/CD environments. For local builds you will just see a development
default value since the github environment variables wont exist.
GitInfoModule.register({
// Controller configuration
routePath: 'version', // Default: 'git-info'
swaggerTag: 'Version Info', // Default: 'git-info'
// Feature flags
disableSwagger: false, // Default: false
disableController: false, // Default: false
});
GitInfoModule.registerAsync({
useFactory: (configService: ConfigService) => ({
disableController: configService.get('NODE_ENV') === 'production',
routePath: configService.get('GIT_INFO_PATH'),
}),
inject: [ConfigService],
});
The module uses environment variables for configuration:
- Git Information Variables (defaults):
GITHUB_SHA
- The commit SHAGITHUB_REF_NAME
- The branch name
- Override Default Variable Names:
GIT_INFO_SHA_VAR
- Override the SHA environment variable nameGIT_INFO_BRANCH_VAR
- Override the branch environment variable name
- Additional Custom Variables:
GIT_INFO_ADD_*
- Add custom environment variables to the output Example:GIT_INFO_ADD_ENVIRONMENT=NODE_ENV
will add the NODE_ENV value to the output
Basic setup with GitHub Actions (uses default environment variables):
@Module({
imports: [GitInfoModule.register()]
})
export class AppModule {}
While environment variables control the build information, you might want to configure how the API endpoint behaves at runtime. For this, use registerAsync
:
@Module({
imports: [
ConfigModule.forRoot(),
GitInfoModule.registerAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
// Disable the controller in production
disableController: configService.get('NODE_ENV') === 'production',
// Use a custom endpoint path from configuration
routePath: configService.get('GIT_INFO_PATH') || 'git-info',
// Disable Swagger in production
disableSwagger: configService.get('NODE_ENV') === 'production'
}),
inject: [ConfigService],
}),
]
})
export class AppModule {}
This allows you to:
- Configure the API endpoint path dynamically
- Enable/disable the controller based on environment
- Control Swagger documentation
- Integrate with your application's configuration system (ConfigService, etc.)
Custom environment variables examples:
# In your CI/CD environment or .env file
GIT_INFO_SHA_VAR=CUSTOM_SHA
GIT_INFO_BRANCH_VAR=CUSTOM_BRANCH
GIT_INFO_ADD_ENVIRONMENT=NODE_ENV
GIT_INFO_ADD_REGION=AWS_REGION
{
"version": "1.0.0",
"githubSHA": "a1b2c3d4e5f6...",
"githubBranch": "main",
"buildTime": "2024-01-01T00:00:00.000Z",
"ENVIRONMENT": "production",
"REGION": "us-east-1"
}
This package automatically works with GitHub Actions as it uses the default environment variables that GitHub Actions injects during builds:
GITHUB_SHA
: The commit SHA that triggered the workflowGITHUB_REF_NAME
: The branch or tag name that triggered the workflow
For local development, these values will default to "development" if the environment variables aren't present.
The module includes Swagger support out of the box if you have @nestjs/swagger
installed. The endpoint will be automatically documented under the 'git-info' tag.
MIT