Skip to content

Commit

Permalink
fix(circleci-orb): improve packing of circleci inline commands
Browse files Browse the repository at this point in the history
Currently we use `EOF` for reading the script from stdin. It looks like
NodeJS on Windows does not properly deal with scripts through piping.

We continue using here-doc bash functionality but use the stdin
redirection operator which makes the stin node execution work.
  • Loading branch information
devversion committed Sep 20, 2022
1 parent 0f3a67f commit 5770ea7
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ jobs:

steps:
- checkout_and_rebase

- run:
shell: /usr/bin/env node -e
command: |
console.error("OK")
- *restore_cache
- setup_bazel_config
- yarn_install
Expand Down
13 changes: 12 additions & 1 deletion circleci-orb/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")

ORB_NAME = "angular/dev-infra"

ORB_VERSION = "0.0.7"
ORB_VERSION = "0.0.16"

nodejs_binary(
name = "pack_orb_script",
entry_point = "pack-orb-script.mjs",
templated_args = [
"--nobazel_run_linker",
],
visibility = ["//circleci-orb:__subpackages__"],
)

filegroup(
name = "orb_generated_files",
Expand Down
2 changes: 1 addition & 1 deletion circleci-orb/commands/rebase-pr-on-target-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ parameters:
default: 'main'
shell:
type: string
default: ""
default: ''
description: |
Shell to use for executing the command. Useful for Windows where a
non-bash shell is the default.
Expand Down
16 changes: 7 additions & 9 deletions circleci-orb/index.bzl
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
"""Helper rules for building CircleCI orbs with the help of Bazel."""

load("@build_bazel_rules_nodejs//:index.bzl", "npm_package_bin")

def nodejs_script_to_sh_script(name, output_file, bundle_file):
"""Rule that takes a NodeJS script and wraps it into a Bash script.
This is useful for inclusion in CircleCI `run` commands in Orbs because
there cannot be an external NodeJS script file, or direct NodeJS `run` commands.
"""
native.genrule(

npm_package_bin(
name = name,
srcs = [bundle_file],
tool = "//circleci-orb:pack_orb_script",
data = [bundle_file],
outs = [output_file],
cmd = """
touch $@
echo '(cat <<"EOF" ' >> $@
cat $< >> $@
echo 'EOF' >> $@
echo ') | node' >> $@
""",
args = ["$(location %s)" % bundle_file, "$@"],
)
29 changes: 29 additions & 0 deletions circleci-orb/pack-orb-script.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import fs from 'fs';

async function main(bundleExecpath, outputExecpath) {
const content = await fs.promises.readFile(bundleExecpath, 'utf8');
// We need to avoid passing it through command line arguments. See:
// https://stackoverflow.com/a/47443446.
const output = `
node <<"NGEOF"
${content}
NGEOF
`;
await fs.promises.writeFile(outputExecpath, output, 'utf8');
}

try {
const [bundleExecpath, outputExecpath] = process.argv.slice(2);
await main(bundleExecpath, outputExecpath);
} catch (e) {
console.error(e);
process.exitCode = 1;
}
7 changes: 5 additions & 2 deletions circleci-orb/scripts/rebase-pr-on-target-branch/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ package(default_visibility = ["//circleci-orb:__subpackages__"])
ts_library(
name = "rebase-pr-on-target-branch",
srcs = glob(["*.ts"]),
deps = ["@npm//@types/node"],
deps = [
"@npm//@types/node",
],
)

esbuild(
name = "bundle",
entry_point = "index.ts",
format = "iife",
sourcemap = "inline",
minify = True,
sourcemap = "",
deps = [":rebase-pr-on-target-branch"],
)

Expand Down

0 comments on commit 5770ea7

Please sign in to comment.