From 797d9889727f0eb11722083e1be7ef19ec7d9cd1 Mon Sep 17 00:00:00 2001 From: Kawika Avilla Date: Thu, 24 Mar 2022 10:19:56 -0700 Subject: [PATCH] [CI][Build] Use BUILD_NUMBER for building bundles (#1371) When running a release build for example: ``` yarn build-platform --linux --skip-os-packages --release ``` The build task runs through get_build_number and checks how many commits you have locally and determines the build number. From there, and this is the value that is used to as a cache busting mechanism. However, in the release build repo https://github.com/opensearch-project/opensearch-build When this gets packaged and verified it actually pulls from the specified branch and only retrieves the HEAD commit. Thus making the count of commits locally equal to `1` and get_build_number always return `1` for releases essentially breaking the cache buster. The build repo however, sets an env variable of `BUILD_NUMBER` so if this value is available it will use it instead of commit count. The CI runs the unit tests and only gets the latest commit as well so instead of setting a env build number and basically creating the same unit test only check this locally. Issues resolved: * https://github.com/opensearch-project/opensearch-build/issues/1769 * https://github.com/opensearch-project/OpenSearch-Dashboards/issues/1363 Signed-off-by: Kawika Avilla --- src/dev/build/lib/get_build_number.test.ts | 41 ++++++++++++++++++++++ src/dev/build/lib/get_build_number.ts | 4 +++ 2 files changed, 45 insertions(+) create mode 100644 src/dev/build/lib/get_build_number.test.ts diff --git a/src/dev/build/lib/get_build_number.test.ts b/src/dev/build/lib/get_build_number.test.ts new file mode 100644 index 000000000000..438235b89362 --- /dev/null +++ b/src/dev/build/lib/get_build_number.test.ts @@ -0,0 +1,41 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +import { getBuildNumber } from './get_build_number'; + +const BUILD_NUMBER_ENV_KEY = 'BUILD_NUMBER'; +const itif = process.env.CI === '1' ? it.skip : it; + +describe('getBuildNumber', () => { + let previousBuildNumber: string; + + beforeEach(() => { + if (BUILD_NUMBER_ENV_KEY in process.env) { + previousBuildNumber = process.env[BUILD_NUMBER_ENV_KEY] as string; + delete process.env[BUILD_NUMBER_ENV_KEY]; + } + }); + + afterEach(() => { + if (BUILD_NUMBER_ENV_KEY in process.env) { + process.env[BUILD_NUMBER_ENV_KEY] = previousBuildNumber; + } + }); + + it('returns env BUILD_NUMBER count', async () => { + process.env.BUILD_NUMBER = '123'; + const buildNumber = await getBuildNumber(); + expect(buildNumber).toBe(123); + }); + + // If test is ran on the CI, it only gets 1 commit + itif('returns git commit count', async () => { + const buildNumber = await getBuildNumber(); + expect(buildNumber).toBeGreaterThan(1000); + }); +}); diff --git a/src/dev/build/lib/get_build_number.ts b/src/dev/build/lib/get_build_number.ts index 333adcf3bd00..99b846b3cc5f 100644 --- a/src/dev/build/lib/get_build_number.ts +++ b/src/dev/build/lib/get_build_number.ts @@ -29,6 +29,10 @@ import os from 'os'; import execa from 'execa'; export async function getBuildNumber() { + if ('BUILD_NUMBER' in process.env) { + return parseFloat(process.env.BUILD_NUMBER as string); + } + if (/^win/.test(os.platform())) { // Windows does not have the wc process and `find /C /V ""` does not consistently work const log = await execa('git', ['log', '--format="%h"']);