Skip to content

feat: pass custom environment variables to shell command #460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ The plugin can be configured in the [**semantic-release** configuration file](ht
"@semantic-release/exec",
{
"verifyConditionsCmd": "./verify.sh",
"prepareCmd": {
"cmd": "./prepare.sh ${nextRelease.version} ${nextRelease.gitTag}",
"env": {
"NEXT_RELEASE_NOTES": "${nextRelease.notes}"
}
},
"publishCmd": "./publish.sh ${nextRelease.version} ${branch.name} ${commits.length} ${Date.now()}"
}
]
Expand Down Expand Up @@ -68,7 +74,14 @@ With this example:
| `shell` | The shell to use to run the command. See [execa#shell](https://github.com/sindresorhus/execa#shell). |
| `execCwd` | The path to use as current working directory when executing the shell commands. This path is relative to the path from which **semantic-release** is running. For example if **semantic-release** runs from `/my-project` and `execCwd` is set to `buildScripts` then the shell command will be executed from `/my-project/buildScripts` |

Each shell command is generated with [Lodash template](https://lodash.com/docs#template). All the [`context` object keys](https://github.com/semantic-release/semantic-release/blob/master/docs/developer-guide/plugin.md#context) passed to semantic-release plugins are available as template options.
Each shell command can be a string or an object with the following properties:

| Property | Description |
| -------- | ----------------------------------------------------- |
| `cmd` | The shell command to execute. |
| `env` | An object to pass as additional environment variables |

Each shell command and environment variable value are generated with [Lodash template](https://lodash.com/docs#template). All the [`context` object keys](https://github.com/semantic-release/semantic-release/blob/master/docs/developer-guide/plugin.md#context) passed to semantic-release plugins are available as template options.

## verifyConditionsCmd

Expand Down
24 changes: 21 additions & 3 deletions lib/exec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolve } from "path";
import { template } from "lodash-es";
import { template, isNil, isPlainObject, isString } from "lodash-es";
import { execa } from "execa";

export default async function exec(
Expand All @@ -8,18 +8,36 @@ export default async function exec(
{ cwd, env, stdout, stderr, logger, ...context },
) {
const cmd = config[cmdProp] ? cmdProp : "cmd";
const script = template(config[cmd])({ config, ...context });

const cmdParsed = parseCommand(config[cmd]);
const script = template(cmdParsed.cmd)({ config, ...context });

const envInterpolated = {
...env,
...Object.entries(cmdParsed.env).reduce((acc, [key, value]) => {
acc[key] = template(value)({ config, ...context });
return acc;
}, {}),
};

logger.log("Call script %s", script);

const result = execa(script, {
shell: shell || true,
cwd: execCwd ? resolve(cwd, execCwd) : cwd,
env,
env: envInterpolated,
});

result.stdout.pipe(stdout, { end: false });
result.stderr.pipe(stderr, { end: false });

return (await result).stdout.trim();
}

function parseCommand(cmd) {
if (isString(cmd)) {
return { cmd, env: {} };
} else if (isPlainObject(cmd) && !isNil(cmd.cmd)) {
return { cmd: cmd.cmd, env: cmd.env || {} };
}
}
13 changes: 11 additions & 2 deletions lib/verify-config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { isNil, isString } from "lodash-es";
import { isNil, isPlainObject, isString } from "lodash-es";
import AggregateError from "aggregate-error";
import getError from "./get-error.js";

const isNonEmptyString = (value) => isString(value) && value.trim();
const isOptional = (validator) => (value) => isNil(value) || validator(value);
const isCmdWithEnv = (value) =>
isPlainObject(value) &&
isNonEmptyString(value.cmd) &&
isOptional(isObjectOfStrings)(value.env);
const isStringOrCmdWithEnv = (value) =>
isNonEmptyString(value) || isCmdWithEnv(value);
const isObjectOfStrings = (value) =>
isPlainObject(value) &&
Object.values(value).every((element) => isString(element));

const VALIDATORS = {
cmd: isNonEmptyString,
cmd: isStringOrCmdWithEnv,
shell: isOptional((shell) => shell === true || isNonEmptyString(shell)),
execCwd: isOptional(isNonEmptyString),
};
Expand Down
22 changes: 22 additions & 0 deletions test/exec.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ test("Generate command with template", async (t) => {
t.is(result, "confValue 1.0.0");
});

test("Generate command with env variables", async (t) => {
const pluginConfig = {
publishCmd: {
cmd: `./test/fixtures/echo-args.sh \${config.conf} \${lastRelease.version}`,
env: {
NEXT_RELEASE_NOTES: "${nextRelease.notes}",
},
},
conf: "confValue",
};
const context = {
stdout: t.context.stdout,
stderr: t.context.stderr,
lastRelease: { version: "1.0.0" },
nextRelease: { notes: "notes &\"'" },
logger: t.context.logger,
};

const result = await exec("publishCmd", pluginConfig, context);
t.is(result, "confValue 1.0.0 notes &\"'");
});

test('Execute the script with the specified "shell"', async (t) => {
const context = {
stdout: t.context.stdout,
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/echo-args.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
echo "$@"
echo "$@" "$NEXT_RELEASE_NOTES"
Loading