---
title: Embed Git Information in Node.js Build Artifacts
description: >-
  Embed Git information in Node.js build artifacts for containers, serverless,
  and host deployments.
breadcrumbs: >-
  Docs > Source Code Integration > Service Mapping for Source Code Integration >
  Embed Git Information in Node.js Build Artifacts
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# Embed Git Information in Node.js Build Artifacts

## Overview{% #overview %}

To embed Git information in your Node.js build artifacts, follow the instructions for your deployment model: Containers, Serverless, or Host.

{% alert level="info" %}
For transpiled Node.js applications (for example, TypeScript), generate and publish source maps with the deployed application. Run Node.js with the [`--enable-source-maps`](https://nodejs.org/docs/latest/api/cli.html#--enable-source-maps) flag. Otherwise, code links and snippets do not work.
{% /alert %}

## Prerequisites{% #prerequisites %}

- [Datadog Agent](https://docs.datadoghq.com/agent.md) v7.35.0 or later is required.
- The Node.js client library version 3.21.0 or later is required.

## Containers{% #containers %}

If you are using Docker containers, you have the following options: using a bundler plugin, using Docker, or configuring your application with `DD_GIT_*` environment variables.

{% collapsible-section %}
### Bundler plugin

If you're bundling your application from a Git directory, you can use plugins to inject Git metadata into the runtime bundle.

###### Bundling with esbuild{% #bundling-with-esbuild %}

Use the `dd-trace/esbuild` plugin to automatically inject `DD_GIT_REPOSITORY_URL` and `DD_GIT_COMMIT_SHA` in your runtime bundle. See the plugin [documentation](https://docs.datadoghq.com/tracing/trace_collection/automatic_instrumentation/dd_libraries/nodejs.md#bundling-with-esbuild). Install `dd-trace` **v5.68.0 or later** for automatic Git tag injection.

```diff
const esbuild = require('esbuild');
+ const ddPlugin = require('dd-trace/esbuild');

esbuild.build({
  entryPoints: ['index.js'],
  bundle: true,
  platform: 'node',
  target: ['node20'],
  format: 'cjs',
  outfile: 'dist/bundle.js',
  packages: 'external',
  external: ['dd-trace', 'express']
+  plugins: [ddPlugin],
}).catch((e) => {
  console.error(e);
  process.exit(1);
});
```

###### Bundling with Rollup{% #bundling-with-rollup %}

Use [rollup-plugin-inject-process-env](https://www.npmjs.com/package/rollup-plugin-inject-process-env) to inject `DD_GIT_REPOSITORY_URL` and `DD_GIT_COMMIT_SHA` in your runtime bundle. Run the bundle step inside a Git repository so the script can read `.git/` information.

```diff
+ const injectProcessEnv = require('rollup-plugin-inject-process-env');

+ const { DD_GIT_REPOSITORY_URL, DD_GIT_COMMIT_SHA } = getGitInfo();

module.exports = {
  input: 'index.js',
  output: {
    file: 'dist/bundle.js',
    format: 'cjs'
  },
  external: [
    'express',
    'dd-trace'
  ],
  plugins: [
    nodeResolve(),
    commonjs()
+    injectProcessEnv({
+       DD_GIT_REPOSITORY_URL,
+       DD_GIT_COMMIT_SHA
+    })
  ]
};
```

The `getGitInfo()` function executes git commands to return `DD_GIT_REPOSITORY_URL` and `DD_GIT_COMMIT_SHA` variables. It needs to be executed in the git repository.

```js
const { execSync } = require('child_process');

function getGitInfo() {
  try {
    const commitSha = execSync('git rev-parse HEAD', { encoding: 'utf8' }).trim();
    const repositoryUrl = execSync('git config --get remote.origin.url', { encoding: 'utf8' }).trim();

    console.log('Build-time Git metadata:', {
      commitSha,
      repositoryUrl,
    });

    return {
      DD_GIT_REPOSITORY_URL: repositoryUrl,
      DD_GIT_COMMIT_SHA: commitSha,
    };
  } catch (error) {
    console.warn('Could not get Git metadata at build time:', error.message);
    return {
      DD_GIT_REPOSITORY_URL: '',
      DD_GIT_COMMIT_SHA: '',
    };
  }
}
```

###### Bundling with webpack{% #bundling-with-webpack %}

Use the [BannerPlugin](https://webpack.js.org/plugins/banner-plugin/) to inject `DD_GIT_REPOSITORY_URL` and `DD_GIT_COMMIT_SHA` in your runtime bundle. Run the bundle step inside a Git repository so the script can read `.git/` information.

```diff
+ const webpack = require('webpack');

+ const { DD_GIT_REPOSITORY_URL, DD_GIT_COMMIT_SHA } = getGitInfo();

module.exports = {
	target: 'node',
	entry: './index.js',
	mode: 'production',
	output: {
		path: path.resolve(__dirname, 'dist'),
		filename: 'bundle.js',
		libraryTarget: 'commonjs2'
	},
	externals: [
		'express',
		'dd-trace'
	],
	plugins: [
+		new webpack.BannerPlugin({
+			raw: true,
+			entryOnly: true,
+			banner:
+        `process.env.DD_GIT_REPOSITORY_URL=${JSON.stringify(DD_GIT_REPOSITORY_URL)};` +
+				 `process.env.DD_GIT_COMMIT_SHA=${JSON.stringify(DD_GIT_COMMIT_SHA)};`,
+		}),
	]
};
```

The `getGitInfo()` function executes git commands to return `DD_GIT_REPOSITORY_URL` and `DD_GIT_COMMIT_SHA` variables. It needs to be executed in the git repository.

```js
const { execSync } = require('child_process');

function getGitInfo() {
  try {
    const commitSha = execSync('git rev-parse HEAD', { encoding: 'utf8' }).trim();
    const repositoryUrl = execSync('git config --get remote.origin.url', { encoding: 'utf8' }).trim();

    console.log('Build-time Git metadata:', {
      commitSha,
      repositoryUrl,
    });

    return {
      DD_GIT_REPOSITORY_URL: repositoryUrl,
      DD_GIT_COMMIT_SHA: commitSha,
    };
  } catch (error) {
    console.warn('Could not get Git metadata at build time:', error.message);
    return {
      DD_GIT_REPOSITORY_URL: '',
      DD_GIT_COMMIT_SHA: '',
    };
  }
}
```

{% /collapsible-section %}

{% collapsible-section %}
### Docker

You can embed git information in your Docker image using either build arguments with environment variables, or image labels.

###### Using build arguments and environment variables{% #using-build-arguments-and-environment-variables %}

1. Add the following lines to your application's Dockerfile:

   ```dockerfile
   ARG DD_GIT_REPOSITORY_URL
   ARG DD_GIT_COMMIT_SHA
   ENV DD_GIT_REPOSITORY_URL=${DD_GIT_REPOSITORY_URL} 
   ENV DD_GIT_COMMIT_SHA=${DD_GIT_COMMIT_SHA}
   ```

1. Add the following arguments to your Docker build command:

   ```shell
   docker build . \
    -t my-application \
    --build-arg DD_GIT_REPOSITORY_URL=<git-provider.example/me/my-repo> \
    --build-arg DD_GIT_COMMIT_SHA=$(git rev-parse HEAD)
   ```

###### Using image labels{% #using-image-labels %}

This approach requires Docker, or containerd >= 1.5.6. It doesn't support containers running on AWS Fargate.

Datadog can extract source code information directly from your images' Docker labels. During build time, follow the [Open Containers standard](https://github.com/opencontainers/image-spec/blob/main/annotations.md#pre-defined-annotation-keys) to add the git commit SHA and repository URL as Docker labels:

```shell
docker build . \
  -t my-application \
  --label org.opencontainers.image.revision=$(git rev-parse HEAD) \
  --label org.opencontainers.image.source=$(git config --get remote.origin.url)
```

{% /collapsible-section %}

{% collapsible-section %}
### `DD_GIT_*` environment variables

Configure your application with the `DD_GIT_*` environment variables:

```go
export DD_GIT_COMMIT_SHA="<commitSha>"
export DD_GIT_REPOSITORY_URL="<git-provider.example/me/my-repo>"
```

Replace `<commitSha>` with the commit SHA used to build your application. You can retrieve this by running `git rev-parse HEAD` at build time, and it needs to be passed into the runtime environment variables. Replace `<git-provider.example/me/my-repo>` with your repository URL.
{% /collapsible-section %}

## Serverless{% #serverless %}

If you are using Serverless, you have the following options depending on your serverless application's setup.

{% collapsible-section %}
### Bundler plugin

If you're bundling your application from a Git directory, you can use plugins to inject Git metadata into the runtime bundle.

###### Bundling with esbuild{% #bundling-with-esbuild-1 %}

Use the `dd-trace/esbuild` plugin to automatically inject `DD_GIT_REPOSITORY_URL` and `DD_GIT_COMMIT_SHA` in your runtime bundle. See the plugin [documentation](https://docs.datadoghq.com/tracing/trace_collection/automatic_instrumentation/dd_libraries/nodejs.md#bundling-with-esbuild). Install `dd-trace` **v5.68.0 or later** for automatic Git tag injection.

```diff
const esbuild = require('esbuild');
+ const ddPlugin = require('dd-trace/esbuild');

esbuild.build({
  entryPoints: ['index.js'],
  bundle: true,
  platform: 'node',
  target: ['node20'],
  format: 'cjs',
  outfile: 'dist/bundle.js',
  packages: 'external',
  external: ['dd-trace', 'express']
+  plugins: [ddPlugin],
}).catch((e) => {
  console.error(e);
  process.exit(1);
});
```

###### Bundling with Rollup{% #bundling-with-rollup-1 %}

Use [rollup-plugin-inject-process-env](https://www.npmjs.com/package/rollup-plugin-inject-process-env) to inject `DD_GIT_REPOSITORY_URL` and `DD_GIT_COMMIT_SHA` in your runtime bundle. Run the bundle step inside a Git repository so the script can read `.git/` information.

```diff
+ const injectProcessEnv = require('rollup-plugin-inject-process-env');

+ const { DD_GIT_REPOSITORY_URL, DD_GIT_COMMIT_SHA } = getGitInfo();

module.exports = {
  input: 'index.js',
  output: {
    file: 'dist/bundle.js',
    format: 'cjs'
  },
  external: [
    'express',
    'dd-trace'
  ],
  plugins: [
    nodeResolve(),
    commonjs()
+    injectProcessEnv({
+       DD_GIT_REPOSITORY_URL,
+       DD_GIT_COMMIT_SHA
+    })
  ]
};
```

The `getGitInfo()` function executes git commands to return `DD_GIT_REPOSITORY_URL` and `DD_GIT_COMMIT_SHA` variables. It needs to be executed in the git repository.

```js
const { execSync } = require('child_process');

function getGitInfo() {
  try {
    const commitSha = execSync('git rev-parse HEAD', { encoding: 'utf8' }).trim();
    const repositoryUrl = execSync('git config --get remote.origin.url', { encoding: 'utf8' }).trim();

    console.log('Build-time Git metadata:', {
      commitSha,
      repositoryUrl,
    });

    return {
      DD_GIT_REPOSITORY_URL: repositoryUrl,
      DD_GIT_COMMIT_SHA: commitSha,
    };
  } catch (error) {
    console.warn('Could not get Git metadata at build time:', error.message);
    return {
      DD_GIT_REPOSITORY_URL: '',
      DD_GIT_COMMIT_SHA: '',
    };
  }
}
```

###### Bundling with webpack{% #bundling-with-webpack-1 %}

Use the [BannerPlugin](https://webpack.js.org/plugins/banner-plugin/) to inject `DD_GIT_REPOSITORY_URL` and `DD_GIT_COMMIT_SHA` in your runtime bundle. Run the bundle step inside a Git repository so the script can read `.git/` information.

```diff
+ const webpack = require('webpack');

+ const { DD_GIT_REPOSITORY_URL, DD_GIT_COMMIT_SHA } = getGitInfo();

module.exports = {
	target: 'node',
	entry: './index.js',
	mode: 'production',
	output: {
		path: path.resolve(__dirname, 'dist'),
		filename: 'bundle.js',
		libraryTarget: 'commonjs2'
	},
	externals: [
		'express',
		'dd-trace'
	],
	plugins: [
+		new webpack.BannerPlugin({
+			raw: true,
+			entryOnly: true,
+			banner:
+        `process.env.DD_GIT_REPOSITORY_URL=${JSON.stringify(DD_GIT_REPOSITORY_URL)};` +
+				 `process.env.DD_GIT_COMMIT_SHA=${JSON.stringify(DD_GIT_COMMIT_SHA)};`,
+		}),
	]
};
```

The `getGitInfo()` function executes git commands to return `DD_GIT_REPOSITORY_URL` and `DD_GIT_COMMIT_SHA` variables. It needs to be executed in the git repository.

```js
const { execSync } = require('child_process');

function getGitInfo() {
  try {
    const commitSha = execSync('git rev-parse HEAD', { encoding: 'utf8' }).trim();
    const repositoryUrl = execSync('git config --get remote.origin.url', { encoding: 'utf8' }).trim();

    console.log('Build-time Git metadata:', {
      commitSha,
      repositoryUrl,
    });

    return {
      DD_GIT_REPOSITORY_URL: repositoryUrl,
      DD_GIT_COMMIT_SHA: commitSha,
    };
  } catch (error) {
    console.warn('Could not get Git metadata at build time:', error.message);
    return {
      DD_GIT_REPOSITORY_URL: '',
      DD_GIT_COMMIT_SHA: '',
    };
  }
}
```

{% /collapsible-section %}

{% collapsible-section %}
### Datadog tooling

{% dl %}

{% dt %}
[Datadog CLI tool](https://www.npmjs.com/package/@datadog/datadog-ci)
{% /dt %}

{% dd %}
Use the `datadog-ci` client version 2.10.0 or later. You must run the CLI tool in the same directory as the code repository.
{% /dd %}

{% dt %}
[Datadog Serverless Plugin](https://docs.datadoghq.com/serverless/libraries_integrations/plugin.md)
{% /dt %}

{% dd %}
Use the plugin version 5.60.0 or later.
{% /dd %}

{% dt %}
[Datadog CDK Construct](https://github.com/DataDog/datadog-cdk-constructs)
{% /dt %}

{% dd %}
Use the `datadog-cdk-constructs` version 0.8.5 or later for AWS CDK v1. Use the `datadog-cdk-constructs` version 1.4.0 or later for AWS CDK v2.
{% /dd %}

{% /dl %}

{% /collapsible-section %}

{% collapsible-section %}
### `DD_GIT_*` environment variables

Configure your application with the `DD_GIT_*` environment variables:

```go
export DD_GIT_COMMIT_SHA="<commitSha>"
export DD_GIT_REPOSITORY_URL="<git-provider.example/me/my-repo>"
```

Replace `<commitSha>` with the commit SHA used to build your application. You can retrieve this by running `git rev-parse HEAD` at build time, and it needs to be passed into the runtime environment variables. Replace `<git-provider.example/me/my-repo>` with your repository URL.
{% /collapsible-section %}

## Host{% #host %}

For host-based environments, you have the following options based on your build and deploy configuration.

{% collapsible-section %}
### Bundler plugin

If you're bundling your application from a Git directory, you can use plugins to inject Git metadata into the runtime bundle.

###### Bundling with esbuild{% #bundling-with-esbuild-2 %}

Use the `dd-trace/esbuild` plugin to automatically inject `DD_GIT_REPOSITORY_URL` and `DD_GIT_COMMIT_SHA` in your runtime bundle. See the plugin [documentation](https://docs.datadoghq.com/tracing/trace_collection/automatic_instrumentation/dd_libraries/nodejs.md#bundling-with-esbuild). Install `dd-trace` **v5.68.0 or later** for automatic Git tag injection.

```diff
const esbuild = require('esbuild');
+ const ddPlugin = require('dd-trace/esbuild');

esbuild.build({
  entryPoints: ['index.js'],
  bundle: true,
  platform: 'node',
  target: ['node20'],
  format: 'cjs',
  outfile: 'dist/bundle.js',
  packages: 'external',
  external: ['dd-trace', 'express']
+  plugins: [ddPlugin],
}).catch((e) => {
  console.error(e);
  process.exit(1);
});
```

###### Bundling with Rollup{% #bundling-with-rollup-2 %}

Use [rollup-plugin-inject-process-env](https://www.npmjs.com/package/rollup-plugin-inject-process-env) to inject `DD_GIT_REPOSITORY_URL` and `DD_GIT_COMMIT_SHA` in your runtime bundle. Run the bundle step inside a Git repository so the script can read `.git/` information.

```diff
+ const injectProcessEnv = require('rollup-plugin-inject-process-env');

+ const { DD_GIT_REPOSITORY_URL, DD_GIT_COMMIT_SHA } = getGitInfo();

module.exports = {
  input: 'index.js',
  output: {
    file: 'dist/bundle.js',
    format: 'cjs'
  },
  external: [
    'express',
    'dd-trace'
  ],
  plugins: [
    nodeResolve(),
    commonjs()
+    injectProcessEnv({
+       DD_GIT_REPOSITORY_URL,
+       DD_GIT_COMMIT_SHA
+    })
  ]
};
```

The `getGitInfo()` function executes git commands to return `DD_GIT_REPOSITORY_URL` and `DD_GIT_COMMIT_SHA` variables. It needs to be executed in the git repository.

```js
const { execSync } = require('child_process');

function getGitInfo() {
  try {
    const commitSha = execSync('git rev-parse HEAD', { encoding: 'utf8' }).trim();
    const repositoryUrl = execSync('git config --get remote.origin.url', { encoding: 'utf8' }).trim();

    console.log('Build-time Git metadata:', {
      commitSha,
      repositoryUrl,
    });

    return {
      DD_GIT_REPOSITORY_URL: repositoryUrl,
      DD_GIT_COMMIT_SHA: commitSha,
    };
  } catch (error) {
    console.warn('Could not get Git metadata at build time:', error.message);
    return {
      DD_GIT_REPOSITORY_URL: '',
      DD_GIT_COMMIT_SHA: '',
    };
  }
}
```

###### Bundling with webpack{% #bundling-with-webpack-2 %}

Use the [BannerPlugin](https://webpack.js.org/plugins/banner-plugin/) to inject `DD_GIT_REPOSITORY_URL` and `DD_GIT_COMMIT_SHA` in your runtime bundle. Run the bundle step inside a Git repository so the script can read `.git/` information.

```diff
+ const webpack = require('webpack');

+ const { DD_GIT_REPOSITORY_URL, DD_GIT_COMMIT_SHA } = getGitInfo();

module.exports = {
	target: 'node',
	entry: './index.js',
	mode: 'production',
	output: {
		path: path.resolve(__dirname, 'dist'),
		filename: 'bundle.js',
		libraryTarget: 'commonjs2'
	},
	externals: [
		'express',
		'dd-trace'
	],
	plugins: [
+		new webpack.BannerPlugin({
+			raw: true,
+			entryOnly: true,
+			banner:
+        `process.env.DD_GIT_REPOSITORY_URL=${JSON.stringify(DD_GIT_REPOSITORY_URL)};` +
+				 `process.env.DD_GIT_COMMIT_SHA=${JSON.stringify(DD_GIT_COMMIT_SHA)};`,
+		}),
	]
};
```

The `getGitInfo()` function executes git commands to return `DD_GIT_REPOSITORY_URL` and `DD_GIT_COMMIT_SHA` variables. It needs to be executed in the git repository.

```js
const { execSync } = require('child_process');

function getGitInfo() {
  try {
    const commitSha = execSync('git rev-parse HEAD', { encoding: 'utf8' }).trim();
    const repositoryUrl = execSync('git config --get remote.origin.url', { encoding: 'utf8' }).trim();

    console.log('Build-time Git metadata:', {
      commitSha,
      repositoryUrl,
    });

    return {
      DD_GIT_REPOSITORY_URL: repositoryUrl,
      DD_GIT_COMMIT_SHA: commitSha,
    };
  } catch (error) {
    console.warn('Could not get Git metadata at build time:', error.message);
    return {
      DD_GIT_REPOSITORY_URL: '',
      DD_GIT_COMMIT_SHA: '',
    };
  }
}
```

{% /collapsible-section %}

{% collapsible-section %}
### `DD_GIT_*` environment variables

Configure your application with the `DD_GIT_*` environment variables:

```go
export DD_GIT_COMMIT_SHA="<commitSha>"
export DD_GIT_REPOSITORY_URL="<git-provider.example/me/my-repo>"
```

Replace `<commitSha>` with the commit SHA used to build your application. You can retrieve this by running `git rev-parse HEAD` at build time, and it needs to be passed into the runtime environment variables. Replace `<git-provider.example/me/my-repo>` with your repository URL.
{% /collapsible-section %}
