Skip to content

Commit

Permalink
feat: detects the best shell and allow to override entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mdubourg001 authored Dec 21, 2021
2 parents c05f4f3 + ea4cd78 commit bb9b08a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const {
GLOBAL_DEFAULT_KEY,
DEFAULT_STAGES,
} = require("./src/constants");
const {
getShellCommandLine,
} = require("./src/shell");

// ----- globals -----

Expand Down Expand Up @@ -67,7 +70,7 @@ async function execCommands({

try {
exec = await container.exec({
Cmd: ["sh", "-c", command],
Cmd: getShellCommandLine(command),
AttachStdout: true,
AttachStderr: true,
WorkingDir: workdir,
Expand Down Expand Up @@ -280,6 +283,9 @@ async function main() {
(job.image?.name ? job.image.name : job.image) ??
(DEFAULT.image?.name ? DEFAULT.image.name : DEFAULT.image) ??
"ruby:2.5";
const entrypoint =
(job.image?.entrypoint ? job.image.entrypoint : undefined) ??
(DEFAULT.image?.entrypoint ? DEFAULT.image.entrypoint : undefined);

let cache = {};
if ("cache" in job) {
Expand Down Expand Up @@ -454,7 +460,9 @@ async function main() {

const config = {
Image: preparedImageName,
Entrypoint: entrypoint,
Tty: true,
Cmd: getShellCommandLine(),
Env: Object.keys(variables).map((key) => `${key}=${variables[key]}`),
HostConfig: {
AutoRemove: true,
Expand Down
49 changes: 49 additions & 0 deletions src/shell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// References:
// * https://gitlab.com/gitlab-org/gitlab-runner/-/blob/master/shells/bash.go
// * https://gitlab.com/gitlab-org/gitlab-runner/-/blob/master/executors/shell/shell.go

/**
* get bash detect shell script
*
* @param isLoginShell {boolean} login shell
* @return {string} generated code
*/
function getBashDetectShellScript(isLoginShell) {
const bashDefaultArguments = isLoginShell ? '--login' : '';
return `if [ -x /usr/local/bin/bash ]; then
exec /usr/local/bin/bash ${bashDefaultArguments} "$@"
elif [ -x /usr/bin/bash ]; then
exec /usr/bin/bash ${bashDefaultArguments} "$@"
elif [ -x /bin/bash ]; then
exec /bin/bash ${bashDefaultArguments} "$@"
elif [ -x /usr/local/bin/sh ]; then
exec /usr/local/bin/sh "$@"
elif [ -x /usr/bin/sh ]; then
exec /usr/bin/sh "$@"
elif [ -x /bin/sh ]; then
exec /bin/sh "$@"
elif [ -x /busybox/sh ]; then
exec /busybox/sh "$@"
else
echo shell not found
exit 1
fi
`;
}

function getShellCommandLine(userCommand) {
const command = [
'sh', '-c', getBashDetectShellScript(true)
];
if (userCommand) {
command.push('--');
command.push('-c');
command.push(userCommand);
}
return command;
}


module.exports = {
getShellCommandLine
};

0 comments on commit bb9b08a

Please sign in to comment.