Repository: netlify/build
Pull request #3520 (2021-08-17 18:09:54)

feat(telemetry): report both build and deploy ids
**Which problem is this pull request solving?**

This PR addresses - https://github.com/netlify/segment-events/issues/48 - forwarding both `buildId` and `deployId` to our telemetry service.

**Checklist**

Please add a `x` inside each checkbox:

- [x] I have read the [contribution guidelines](../blob/main/CONTRIBUTING.md).
- [ ] The status checks are successful (continuous integration). Those can be seen below.



Patch:
diff --git a/packages/build/src/core/main.js b/packages/build/src/core/main.js
index 5bc4c4417..c8d86a713 100644
--- a/packages/build/src/core/main.js
+++ b/packages/build/src/core/main.js
@@ -52,8 +52,20 @@ const { getSeverity } = require('./severity')
  * @returns {string[]} buildResult.logs - When using the `buffer` option, all log messages
  */
 const build = async function (flags = {}) {
-  const { errorMonitor, framework, mode, logs, debug, testOpts, statsdOpts, dry, telemetry, ...flagsA } =
-    startBuild(flags)
+  const {
+    errorMonitor,
+    framework,
+    mode,
+    logs,
+    debug,
+    testOpts,
+    statsdOpts,
+    dry,
+    telemetry,
+    buildId,
+    deployId,
+    ...flagsA
+  } = startBuild(flags)
   const errorParams = { errorMonitor, mode, logs, debug, testOpts }
 
   try {
@@ -68,6 +80,8 @@ const build = async function (flags = {}) {
       configMutations,
     } = await execBuild({
       ...flagsA,
+      buildId,
+      deployId,
       dry,
       errorMonitor,
       mode,
@@ -86,6 +100,8 @@ const build = async function (flags = {}) {
     })
     const { success, severityCode, status } = getSeverity('success')
     await telemetryReport({
+      buildId,
+      deployId,
       status,
       commandsCount,
       pluginsOptions,
@@ -103,6 +119,8 @@ const build = async function (flags = {}) {
     const { pluginsOptions, siteInfo, userNodeVersion } = errorParams
     const { success, severityCode, status } = getSeverity(severity)
     await telemetryReport({
+      buildId,
+      deployId,
       status,
       pluginsOptions,
       siteInfo,
@@ -624,6 +642,8 @@ const handleBuildSuccess = async function ({ framework, dry, logs, timers, durat
 
 // Handles the calls and errors of telemetry reports
 const telemetryReport = async function ({
+  deployId,
+  buildId,
   status,
   commandsCount,
   pluginsOptions,
@@ -637,6 +657,8 @@ const telemetryReport = async function ({
 }) {
   try {
     await trackBuildComplete({
+      deployId,
+      buildId,
       status,
       commandsCount,
       pluginsOptions,
diff --git a/packages/build/src/core/normalize_flags.js b/packages/build/src/core/normalize_flags.js
index ba1e6764e..3022f3ebe 100644
--- a/packages/build/src/core/normalize_flags.js
+++ b/packages/build/src/core/normalize_flags.js
@@ -44,6 +44,7 @@ const getDefaultFlags = function ({ env: envOpt = {} }, combinedEnv) {
     functionsDistDir: DEFAULT_FUNCTIONS_DIST,
     cacheDir: DEFAULT_CACHE_DIR,
     deployId: combinedEnv.DEPLOY_ID,
+    buildId: combinedEnv.BUILD_ID,
     debug: Boolean(combinedEnv.NETLIFY_BUILD_DEBUG),
     bugsnagKey: combinedEnv.BUGSNAG_KEY,
     sendStatus: false,
diff --git a/packages/build/src/telemetry/main.js b/packages/build/src/telemetry/main.js
index cb99f9fb1..b8c24cc4e 100644
--- a/packages/build/src/telemetry/main.js
+++ b/packages/build/src/telemetry/main.js
@@ -18,6 +18,8 @@ const DEFAULT_TELEMETRY_CONFIG = {
 
 // Send telemetry request when build completes
 const trackBuildComplete = async function ({
+  deployId,
+  buildId,
   status,
   commandsCount,
   pluginsOptions,
@@ -34,6 +36,8 @@ const trackBuildComplete = async function ({
 
   try {
     const payload = getPayload({
+      deployId,
+      buildId,
       status,
       commandsCount,
       pluginsOptions,
@@ -67,6 +71,8 @@ const track = async function ({ payload, config: { origin, writeKey, timeout } }
 // Retrieve telemetry information
 // siteInfo can be empty if the build fails during the get config step
 const getPayload = function ({
+  deployId,
+  buildId,
   status,
   commandsCount,
   pluginsOptions,
@@ -80,6 +86,8 @@ const getPayload = function ({
     event: 'build:ci_build_process_completed',
     timestamp: Date.now(),
     properties: {
+      deployId,
+      buildId,
       status,
       steps: commandsCount,
       buildVersion,


Test patch:
diff --git a/packages/build/tests/telemetry/tests.js b/packages/build/tests/telemetry/tests.js
index 39c77458d..64158aff8 100644
--- a/packages/build/tests/telemetry/tests.js
+++ b/packages/build/tests/telemetry/tests.js
@@ -198,6 +198,27 @@ test('Telemetry reports no framework if none is provided', async (t) => {
   t.is(telemetryRequests[0].body.properties.framework, undefined)
 })
 
+test('Telemetry reports the build id if given via BUILD_ID', async (t) => {
+  const buildId = 'test-build-id'
+  const { telemetryRequests } = await runWithApiMock(t, 'success', { env: { BUILD_ID: buildId } })
+  t.is(telemetryRequests.length, 1)
+  t.is(telemetryRequests[0].body.properties.buildId, buildId)
+})
+
+test('Telemetry reports a deploy id if given via DEPLOY_ID', async (t) => {
+  const deployId = 'test-deploy-id'
+  const { telemetryRequests } = await runWithApiMock(t, 'success', { env: { DEPLOY_ID: deployId } })
+  t.is(telemetryRequests.length, 1)
+  t.is(telemetryRequests[0].body.properties.deployId, deployId)
+})
+
+test('Telemetry reports a deploy id if given via --deployId flag', async (t) => {
+  const deployId = 'test-deploy-id'
+  const { telemetryRequests } = await runWithApiMock(t, 'success', { deployId })
+  t.is(telemetryRequests.length, 1)
+  t.is(telemetryRequests[0].body.properties.deployId, deployId)
+})
+
 test('Telemetry calls timeout by default', async (t) => {
   const { telemetryRequests } = await runWithApiMock(t, 'success', {
     // We want to rely on the default timeout value