From 3af55072c2ee20daf782fbf0407a6482a52c9dae Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Sun, 7 Feb 2021 02:31:39 +0000 Subject: [PATCH 1/4] chore(NA): move bazel workspace status into nodejs executable --- .bazelrc | 2 +- src/dev/bazel_workspace_status.js | 76 +++++++++++++++++++++++++++++++ src/dev/bazel_workspace_status.sh | 57 ----------------------- 3 files changed, 77 insertions(+), 58 deletions(-) create mode 100644 src/dev/bazel_workspace_status.js delete mode 100755 src/dev/bazel_workspace_status.sh diff --git a/.bazelrc b/.bazelrc index 158338ec5f093..5fa6ef245fcea 100644 --- a/.bazelrc +++ b/.bazelrc @@ -11,7 +11,7 @@ import %workspace%/.bazelrc.common # BuildBuddy ## Metadata settings -build --workspace_status_command=$(pwd)/src/dev/bazel_workspace_status.sh +build --workspace_status_command="node ./src/dev/bazel_workspace_status.js" # Enable this in case you want to share your build info # build --build_metadata=VISIBILITY=PUBLIC build --build_metadata=TEST_GROUPS=//packages diff --git a/src/dev/bazel_workspace_status.js b/src/dev/bazel_workspace_status.js new file mode 100644 index 0000000000000..0160ce2922e89 --- /dev/null +++ b/src/dev/bazel_workspace_status.js @@ -0,0 +1,76 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +// Inspired on https://github.com/buildbuddy-io/buildbuddy/blob/master/workspace_status.sh +// This script will be run bazel when building process starts to +// generate key-value information that represents the status of the +// workspace. The output should be like +// +// KEY1 VALUE1 +// KEY2 VALUE2 +// +// If the script exits with non-zero code, it's considered as a failure +// and the output will be discarded. + +(async () => { + const execa = require('execa'); + const os = require('os'); + + async function runCmd(cmd, args) { + try { + return await execa(cmd, args); + } catch (e) { + console.log(e); + return { exitCode: 1 }; + } + } + + // Git repo + const repoUrlCmdResult = await runCmd('git', ['config', '--get', 'remote.origin.url']); + if (repoUrlCmdResult.exitCode !== 0) { + process.exit(1); + } + console.log(`REPO_URL ${repoUrlCmdResult.stdout}`); + + // Commit SHA + const commitSHACmdResult = await runCmd('git', ['rev-parse', 'HEAD']); + if (commitSHACmdResult.exitCode !== 0) { + process.exit(1); + } + console.log(`COMMIT_SHA ${commitSHACmdResult.stdout}`); + + // Git branch + const gitBranchCmdResult = await runCmd('git', ['rev-parse', '--abbrev-ref', 'HEAD']); + if (gitBranchCmdResult.exitCode !== 0) { + process.exit(1); + } + console.log(`GIT_BRANCH ${gitBranchCmdResult.stdout}`); + + // Tree status + const treeStatusCmdResult = await runCmd('git', ['diff-index', '--quiet', 'HEAD', '--']); + const treeStatusVarStr = 'GIT_TREE_STATUS'; + if (treeStatusCmdResult.exitCode === 0) { + console.log(`${treeStatusVarStr} Clean`); + } else { + console.log(`${treeStatusVarStr} Modified`); + } + + // Host + if (process.env.CI) { + const hostCmdResult = await runCmd('hostname'); + const hostStr = hostCmdResult.stdout.split('-').slice(0, -1).join('-'); + const coresStr = os.cpus().filter((cpu, index) => { + return !cpu.model.includes('Intel') || index % 2 === 1; + }).length; + + if (hostCmdResult.exitCode !== 0) { + process.exit(1); + } + console.log(`HOST ${hostStr}-${coresStr}`); + } +})(); diff --git a/src/dev/bazel_workspace_status.sh b/src/dev/bazel_workspace_status.sh deleted file mode 100755 index efaca4bb98849..0000000000000 --- a/src/dev/bazel_workspace_status.sh +++ /dev/null @@ -1,57 +0,0 @@ -#!/bin/bash - -# Inspired on https://github.com/buildbuddy-io/buildbuddy/blob/master/workspace_status.sh -# This script will be run bazel when building process starts to -# generate key-value information that represents the status of the -# workspace. The output should be like -# -# KEY1 VALUE1 -# KEY2 VALUE2 -# -# If the script exits with non-zero code, it's considered as a failure -# and the output will be discarded. - -# Git repo -repo_url=$(git config --get remote.origin.url) -if [[ $? != 0 ]]; -then - exit 1 -fi -echo "REPO_URL ${repo_url}" - -# Commit SHA -commit_sha=$(git rev-parse HEAD) -if [[ $? != 0 ]]; -then - exit 1 -fi -echo "COMMIT_SHA ${commit_sha}" - -# Git branch -repo_url=$(git rev-parse --abbrev-ref HEAD) -if [[ $? != 0 ]]; -then - exit 1 -fi -echo "GIT_BRANCH ${repo_url}" - -# Tree status -git diff-index --quiet HEAD -- -if [[ $? == 0 ]]; -then - tree_status="Clean" -else - tree_status="Modified" -fi -echo "GIT_TREE_STATUS ${tree_status}" - -# Host -if [ "$CI" = "true" ]; then - host=$(hostname | sed 's|\(.*\)-.*|\1|') - cores=$(grep ^cpu\\scores /proc/cpuinfo | uniq | awk '{print $4}' ) - if [[ $? != 0 ]]; - then - exit 1 - fi - echo "HOST ${host}-${cores}" -fi From 43eb8b25daebec2f9d10b05f92708b08103ff57e Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Sun, 7 Feb 2021 02:38:06 +0000 Subject: [PATCH 2/4] chore(NA): removed unused console.log on error --- src/dev/bazel_workspace_status.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/dev/bazel_workspace_status.js b/src/dev/bazel_workspace_status.js index 0160ce2922e89..e25f7fd161c1d 100644 --- a/src/dev/bazel_workspace_status.js +++ b/src/dev/bazel_workspace_status.js @@ -25,7 +25,6 @@ try { return await execa(cmd, args); } catch (e) { - console.log(e); return { exitCode: 1 }; } } From 230258264c6f36cf281078ddf0785d3ee727ec7c Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Mon, 8 Feb 2021 18:11:44 +0000 Subject: [PATCH 3/4] chore(NA): ability to setup different name for origin remote on workspace status command --- src/dev/bazel_workspace_status.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/dev/bazel_workspace_status.js b/src/dev/bazel_workspace_status.js index e25f7fd161c1d..8275eee33465d 100644 --- a/src/dev/bazel_workspace_status.js +++ b/src/dev/bazel_workspace_status.js @@ -30,7 +30,12 @@ } // Git repo - const repoUrlCmdResult = await runCmd('git', ['config', '--get', 'remote.origin.url']); + const kbnGitOriginName = process.env.KBN_GIT_ORIGIN_NAME || 'origin'; + const repoUrlCmdResult = await runCmd('git', [ + 'config', + '--get', + `remote.${kbnGitOriginName}.url`, + ]); if (repoUrlCmdResult.exitCode !== 0) { process.exit(1); } From 546434d032aa0e07cf04a7e957294bfda1a8097c Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Mon, 8 Feb 2021 18:55:24 +0000 Subject: [PATCH 4/4] chore(NA): do not fail if cant collect repo url --- src/dev/bazel_workspace_status.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/dev/bazel_workspace_status.js b/src/dev/bazel_workspace_status.js index 8275eee33465d..fe60f9176d243 100644 --- a/src/dev/bazel_workspace_status.js +++ b/src/dev/bazel_workspace_status.js @@ -36,10 +36,10 @@ '--get', `remote.${kbnGitOriginName}.url`, ]); - if (repoUrlCmdResult.exitCode !== 0) { - process.exit(1); + if (repoUrlCmdResult.exitCode === 0) { + // Only output REPO_URL when found it + console.log(`REPO_URL ${repoUrlCmdResult.stdout}`); } - console.log(`REPO_URL ${repoUrlCmdResult.stdout}`); // Commit SHA const commitSHACmdResult = await runCmd('git', ['rev-parse', 'HEAD']);