Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ Check the [examples](https://github.com/mserranom/bbrun/tree/master/examples) an
Options
--template (-t), pipeline template, defaults to "bitbucket-pipelines.yml"
--env (-e), define environment variables for execution
--envfile (-ef), define environment file variables for execution
--dry-run (-d), performs dry run, printing the docker command
--interactive (-i), starts an interactive bash session in the container
--ignore-folder (-f), adds the folder as an empty volume (useful for forcing pipeline to install packages etc)
--keep-container (-k), does not remove the container after build (ignores --interactive)
--help, prints this very guide

Examples:
Expand Down
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ Options
--template (-t), build template, defaults to "bitbucket-pipelines.yml"
--pipeline (-p), pipeline to execute. "default" if not provided
--env (-e), define environment variables for execution
--envfile (-ef), define environment file variables for execution
--work-dir (-w), docker working directory, defaults to "ws"
--dry-run (-d), performs dry run, printing the docker command
--interactive (-i), starts an interactive bash session in the container
--ignore-folder (-if), maps the folder to an empty folder (useful for forcing package managers to reinstall)
--keep-container (-k), does not remove the container after build (ignores --interactive)
--help, prints this very guide

Examples:
Expand Down Expand Up @@ -47,6 +49,10 @@ Examples:
type: "string",
alias: "e"
},
envfile: {
type: "string",
alias: "ef"
},
"work-dir": {
type: "string",
alias: "w"
Expand All @@ -55,6 +61,10 @@ Examples:
type: "boolean",
alias: "i"
},
"keep-container": {
type: "boolean",
alias: "k"
},
"dry-run": {
type: "boolean",
alias: "d"
Expand Down
12 changes: 9 additions & 3 deletions src/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function checkExists() {
}
}

function run(commands, image, dryRun, interactive, workDir, ignoreFolder) {
function run(commands, image, dryRun, interactive, workDir, ignoreFolder, keepContainer) {
let ignore = '';
if (typeof ignoreFolder !== "undefined") {
if (typeof ignoreFolder === "string") {
Expand All @@ -48,9 +48,15 @@ function run(commands, image, dryRun, interactive, workDir, ignoreFolder) {
}).join(' ');
}

const rm = keepContainer ? "" : "--rm";
if (keepContainer) {
// keepContainer and interactive does not work together
interactive = false
}

const cmd = interactive
? `run --rm -P -it --entrypoint=/bin/bash -v ${pwd()}:${workDir} -w ${workDir} ${image}`
: `run --rm -P -v ${pwd()}:${workDir} -w ${workDir} ${image} bash ${BUILD_SCRIPT}`;
? `run ${rm} -P -it --entrypoint=/bin/bash -v ${pwd()}:${workDir} -w ${workDir} ${image}`
: `run ${rm} -P -v ${pwd()}:${workDir} -w ${workDir} ${image} bash ${BUILD_SCRIPT}`;

if (dryRun) {
console.log(`docker command:\n\tdocker ${cmd}`);
Expand Down
40 changes: 36 additions & 4 deletions src/environment.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";
const fs = require("fs");
const os = require("os");
const exec = require("shelljs").exec;

function save(env, location) {
//TODO: validate
Expand All @@ -16,13 +17,44 @@ function load(location) {
}
}

function loadBitbucketEnv() {
let envArs = [
'CI=true',
'BITBUCKET_SSH_KEY_FILE=~/.ssh/id_rsa'
];

// retrieve BITBUCKET_BRANCH
const commitHash = exec(`git branch --show-current`, { async: false, silent: true });
if (commitHash.code === 0 && commitHash.stdout) {
envArs = envArs.concat(`BITBUCKET_BRANCH="${commitHash.stdout.trim()}"`);
}

// retrieve BITBUCKET_COMMIT
const branchName = exec(`git rev-parse HEAD`, { async: false, silent: true });
if (branchName.code === 0 && branchName.stdout) {
envArs = envArs.concat(`BITBUCKET_COMMIT="${branchName.stdout.trim()}"`);
}

// BITBUCKET_BUILD_NUMBER
envArs = envArs.concat(`BITBUCKET_BUILD_NUMBER="${between(0,999)}"`);

return envArs;
}

function parseVars(envArg) {
return envArg
.trim()
.split(",")
.map(x => x.trim());
return envArg.match(/(?=\b[a-z])\w+=(?:(['"])(?:(?!\1).)*\1|[^,]*)/gi).map(x => x.trim());
}

/**
* Returns a random number between min (inclusive) and max (exclusive)
*/
function between(min, max) {
return Math.floor(
Math.random() * (max - min) + min
)
}

module.exports.save = save;
module.exports.load = load;
module.exports.parseVars = parseVars;
module.exports.loadBitbucketEnv = loadBitbucketEnv;
16 changes: 11 additions & 5 deletions src/exec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
const docker = require("./docker");
const { parseVars } = require("./util");
const { parseVars, load, loadBitbucketEnv } = require("./environment");

function exec(script, image, flags) {
const environmentVars = flags.env ? parseVars(flags.env) : [];
let environmentVars = flags.env ? parseVars(flags.env) : [];
if (flags.envfile) {
let loadEnvVars = load(flags.envfile);
environmentVars = environmentVars.concat(loadEnvVars);
}
const commands = [].concat(
environmentVars.map(x => `export ${x}`),
"set -e",
environmentVars.map((x) => { return x && (x.includes('export') ? x : `export ${x}`); }),
loadBitbucketEnv(),
'set -e',
script
);
docker.run(commands, image, flags.dryRun, flags.interactive, flags.workDir, flags.ignoreFolder);
console.log(commands);
docker.run(commands, image, flags.dryRun, flags.interactive, flags.workDir, flags.ignoreFolder, flags.keepContainer);
}

module.exports.exec = exec;
26 changes: 26 additions & 0 deletions test/__snapshots__/integration.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

exports[`environment variables miltiple variables are added to the script 1`] = `
"executing step in \\"ubuntu\\"
[
'export foo=bar',
'export var=env',
'set -e',
'echo \\"testing stuff\\"'
]
docker command:
docker run --rm -P -v PWD:/ws -w /ws ubuntu bash .bbrun.sh
build script:
Expand All @@ -14,6 +20,7 @@ build script:

exports[`environment variables single variable is added to the script 1`] = `
"executing step in \\"ubuntu\\"
[ 'export foo=bar', 'set -e', 'echo \\"testing stuff\\"' ]
docker command:
docker run --rm -P -v PWD:/ws -w /ws ubuntu bash .bbrun.sh
build script:
Expand All @@ -31,6 +38,7 @@ Malformed template, check https://confluence.atlassian.com/bitbucket/configure-b

exports[`no image template should use default atlassian image 1`] = `
"executing step in \\"atlassian/default-image:latest\\"
[ 'set -e', 'echo \\"testing stuff\\"' ]
docker command:
docker run --rm -P -v PWD:/ws -w /ws atlassian/default-image:latest bash .bbrun.sh
build script:
Expand All @@ -41,24 +49,28 @@ build script:

exports[`parallel steps executes all steps when step name is not provided 1`] = `
"executing step in \\"atlassian/default-image:latest\\"
[ 'set -e', 'echo \\"default step\\"' ]
docker command:
docker run --rm -P -v PWD:/ws -w /ws atlassian/default-image:latest bash .bbrun.sh
build script:
set -e
echo \\"default step\\"
executing step in \\"atlassian/default-image:latest\\"
[ 'set -e', 'echo \\"parallel step2\\"' ]
docker command:
docker run --rm -P -v PWD:/ws -w /ws atlassian/default-image:latest bash .bbrun.sh
build script:
set -e
echo \\"parallel step2\\"
executing step in \\"atlassian/default-image:latest\\"
[ 'set -e', 'echo \\"parallel step3\\"' ]
docker command:
docker run --rm -P -v PWD:/ws -w /ws atlassian/default-image:latest bash .bbrun.sh
build script:
set -e
echo \\"parallel step3\\"
executing step in \\"atlassian/default-image:latest\\"
[ 'set -e', 'echo \\"step4\\"' ]
docker command:
docker run --rm -P -v PWD:/ws -w /ws atlassian/default-image:latest bash .bbrun.sh
build script:
Expand All @@ -69,6 +81,7 @@ build script:

exports[`parallel steps individual steps from a parallel section can be executed 1`] = `
"executing step \\"parallel_step_2\\" in \\"atlassian/default-image:latest\\"
[ 'set -e', 'echo \\"parallel step2\\"' ]
docker command:
docker run --rm -P -v PWD:/test_wd -w /test_wd atlassian/default-image:latest bash .bbrun.sh
build script:
Expand All @@ -79,6 +92,7 @@ build script:

exports[`should resolve private image names 1`] = `
"executing step in \\"account-name/openjdk:8\\"
[ 'set -e', 'echo \\"testing stuff\\"' ]
docker command:
docker run --rm -P -v PWD:/ws -w /ws account-name/openjdk:8 bash .bbrun.sh
build script:
Expand All @@ -89,6 +103,7 @@ build script:

exports[`single step pipeline executes the default step with no arguments 1`] = `
"executing step in \\"ubuntu\\"
[ 'set -e', 'echo \\"testing stuff\\"' ]
docker command:
docker run --rm -P -v PWD:/ws -w /ws ubuntu bash .bbrun.sh
build script:
Expand All @@ -99,6 +114,7 @@ build script:

exports[`single step pipeline executes the step by name 1`] = `
"executing step \\"test\\" in \\"ubuntu\\"
[ 'set -e', 'echo \\"testing stuff\\"' ]
docker command:
docker run --rm -P -v PWD:/ws -w /ws ubuntu bash .bbrun.sh
build script:
Expand All @@ -109,6 +125,7 @@ build script:

exports[`template with multiple steps in the default pipeline should execute a named step in a branch 1`] = `
"executing step \\"master_step_2\\" in \\"atlassian/default-image:latest\\"
[ 'set -e', 'echo \\"master step 2\\"' ]
docker command:
docker run --rm -P -v PWD:/ws -w /ws atlassian/default-image:latest bash .bbrun.sh
build script:
Expand All @@ -119,6 +136,7 @@ build script:

exports[`template with multiple steps in the default pipeline should execute a single step by name 1`] = `
"executing step \\"step2\\" in \\"atlassian/default-image:latest\\"
[ 'set -e', 'echo \\"step2\\"' ]
docker command:
docker run --rm -P -v PWD:/ws -w /ws atlassian/default-image:latest bash .bbrun.sh
build script:
Expand All @@ -129,6 +147,7 @@ build script:

exports[`template with multiple steps in the default pipeline should execute a single step by name with spaces in the name 1`] = `
"executing step \\"Step Three\\" in \\"ubuntu\\"
[ 'set -e', 'echo \\"step3\\"' ]
docker command:
docker run --rm -P -v PWD:/ws -w /ws ubuntu bash .bbrun.sh
build script:
Expand All @@ -139,18 +158,21 @@ build script:

exports[`template with multiple steps in the default pipeline should execute all the default steps when no argument is provided 1`] = `
"executing step in \\"atlassian/default-image:latest\\"
[ 'set -e', 'echo \\"testing stuff\\"' ]
docker command:
docker run --rm -P -v PWD:/ws -w /ws atlassian/default-image:latest bash .bbrun.sh
build script:
set -e
echo \\"testing stuff\\"
executing step in \\"atlassian/default-image:latest\\"
[ 'set -e', 'echo \\"step2\\"' ]
docker command:
docker run --rm -P -v PWD:/ws -w /ws atlassian/default-image:latest bash .bbrun.sh
build script:
set -e
echo \\"step2\\"
executing step in \\"ubuntu\\"
[ 'set -e', 'echo \\"step3\\"' ]
docker command:
docker run --rm -P -v PWD:/ws -w /ws ubuntu bash .bbrun.sh
build script:
Expand All @@ -161,12 +183,14 @@ build script:

exports[`template with multiple steps in the default pipeline should execute all the steps in a branch if no step provided 1`] = `
"executing step in \\"atlassian/default-image:latest\\"
[ 'set -e', 'echo \\"testing stuff in master branch\\"' ]
docker command:
docker run --rm -P -v PWD:/ws -w /ws atlassian/default-image:latest bash .bbrun.sh
build script:
set -e
echo \\"testing stuff in master branch\\"
executing step in \\"atlassian/default-image:latest\\"
[ 'set -e', 'echo \\"master step 2\\"' ]
docker command:
docker run --rm -P -v PWD:/ws -w /ws atlassian/default-image:latest bash .bbrun.sh
build script:
Expand All @@ -177,6 +201,7 @@ build script:

exports[`template with multiple steps in the default pipeline should understand asterisk branches 1`] = `
"executing step \\"branch step 2\\" in \\"atlassian/default-image:latest\\"
[ 'set -e', 'echo \\"testing stuff in any branch\\"' ]
docker command:
docker run --rm -P -v PWD:/ws -w /ws atlassian/default-image:latest bash .bbrun.sh
build script:
Expand All @@ -187,6 +212,7 @@ build script:

exports[`working directory is overriden 1`] = `
"executing step in \\"ubuntu\\"
[ 'set -e', 'echo \\"testing stuff\\"' ]
docker command:
docker run --rm -P -v PWD:/test_wd -w /test_wd ubuntu bash .bbrun.sh
build script:
Expand Down