Skip to content

sourceVersion override feature #62

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

Closed
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
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ This action offers three inputs that you can use to configure its behavior.
The only required input is `project-name`.

1. **project-name** (required) : The name of CodeBuild project you want to run.
1. **source-version-override** (optional) :
The source version you would like to show up in CodeBuild.
By default, the action uses the `GITHUB_SHA` from the GitHub Action run.
1. **buildspec-override** (optional) :
The location (in this repository) of the [buildspec file][codebuild buildspec]
that CodeBuild requires.
Expand Down Expand Up @@ -213,11 +216,11 @@ plus any environment variables that you specified in the `evn-passthrough` input
Regardless of the project configuration in CodeBuild or GitHub Actions,
we always pass the following parameters and values to CodeBuild in the StartBuild API call.

| CodeBuild value | GitHub value |
| ------------------------ | -------------------------------------- |
| `sourceVersion` | The commit that triggered the workflow |
| `sourceTypeOverride` | The string `'GITHUB'` |
| `sourceLocationOverride` | The `HTTPS` git url for `context.repo` |
| CodeBuild value | GitHub value |
| ------------------------ | --------------------------------------------------------------------- |
| `sourceVersion` | The commit that triggered the workflow or the source-version-override |
| `sourceTypeOverride` | The string `'GITHUB'` |
| `sourceLocationOverride` | The `HTTPS` git url for `context.repo` |

### What we did not do

Expand Down
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ inputs:
env-vars-for-codebuild:
description: 'Comma separated list of environment variables to send to CodeBuild'
required: false
source-version-override:
description: 'Source version, branch, commit'
required: false
outputs:
aws-build-id:
description: 'The AWS CodeBuild Build ID for this build.'
runs:
using: 'node12'
main: 'dist/index.js'
main: 'dist/index.js'
7 changes: 3 additions & 4 deletions code-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,16 @@ async function waitForBuildEndTime(sdk, { id, logs }, nextToken) {
function githubInputs() {
const projectName = core.getInput("project-name", { required: true });
const { owner, repo } = github.context.repo;
const { payload } = github.context;
// const { payload } = github.context;
// The github.context.sha is evaluated on import.
// This makes it hard to test.
// So I use the raw ENV.
// There is a complexity here because for pull request
// the GITHUB_SHA value is NOT the correct value.
// See: https://github.com/aws-actions/aws-codebuild-run-build/issues/36
const sourceVersion =
process.env[`GITHUB_EVENT_NAME`] === "pull_request"
? (((payload || {}).pull_request || {}).head || {}).sha
: process.env[`GITHUB_SHA`];
core.getInput("source-version-override", { required: false }) ||
process.env[`GITHUB_SHA`];

assert(sourceVersion, "No source version could be evaluated.");
const buildspecOverride =
Expand Down
150 changes: 129 additions & 21 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66968,6 +66968,51 @@ module.exports = /******/ (function (modules, runtime) {
/***/
},

/***/ 2102: /***/ function (__unusedmodule, exports, __webpack_require__) {
"use strict";

// For internal use, subject to change.
var __importStar =
(this && this.__importStar) ||
function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null)
for (var k in mod)
if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
// We use any as a valid input type
/* eslint-disable @typescript-eslint/no-explicit-any */
const fs = __importStar(__webpack_require__(5747));
const os = __importStar(__webpack_require__(2087));
const utils_1 = __webpack_require__(5082);
function issueCommand(command, message) {
const filePath = process.env[`GITHUB_${command}`];
if (!filePath) {
throw new Error(
`Unable to find environment variable for file command ${command}`
);
}
if (!fs.existsSync(filePath)) {
throw new Error(`Missing file at path: ${filePath}`);
}
fs.appendFileSync(
filePath,
`${utils_1.toCommandValue(message)}${os.EOL}`,
{
encoding: "utf8",
}
);
}
exports.issueCommand = issueCommand;
//# sourceMappingURL=file-command.js.map

/***/
},

/***/ 2106: /***/ function (module, __unusedexports, __webpack_require__) {
__webpack_require__(3234);
var AWS = __webpack_require__(395);
Expand Down Expand Up @@ -118516,6 +118561,7 @@ module.exports = /******/ (function (modules, runtime) {
};
Object.defineProperty(exports, "__esModule", { value: true });
const os = __importStar(__webpack_require__(2087));
const utils_1 = __webpack_require__(5082);
/**
* Commands
*
Expand Down Expand Up @@ -118569,13 +118615,15 @@ module.exports = /******/ (function (modules, runtime) {
}
}
function escapeData(s) {
return (s || "")
return utils_1
.toCommandValue(s)
.replace(/%/g, "%25")
.replace(/\r/g, "%0D")
.replace(/\n/g, "%0A");
}
function escapeProperty(s) {
return (s || "")
return utils_1
.toCommandValue(s)
.replace(/%/g, "%25")
.replace(/\r/g, "%0D")
.replace(/\n/g, "%0A")
Expand Down Expand Up @@ -123249,6 +123297,14 @@ module.exports = /******/ (function (modules, runtime) {
res = /<meta[\s]+?http-equiv=(['"])content-type\1[\s]+?content=(['"])(.+?)\2/i.exec(
str
);
if (!res) {
res = /<meta[\s]+?content=(['"])(.+?)\1[\s]+?http-equiv=(['"])content-type\3/i.exec(
str
);
if (res) {
res.pop(); // drop last quote
}
}

if (res) {
res = /charset=(.*)/i.exec(res.pop());
Expand Down Expand Up @@ -124366,7 +124422,7 @@ module.exports = /******/ (function (modules, runtime) {
case "error":
reject(
new FetchError(
`redirect mode is set to error: ${request.url}`,
`uri requested responds with a redirect, redirect mode is set to error: ${request.url}`,
"no-redirect"
)
);
Expand Down Expand Up @@ -124414,6 +124470,7 @@ module.exports = /******/ (function (modules, runtime) {
body: request.body,
signal: request.signal,
timeout: request.timeout,
size: request.size,
};

// HTTP-redirect fetch step 9
Expand Down Expand Up @@ -147209,6 +147266,30 @@ module.exports = /******/ (function (modules, runtime) {
/***/
},

/***/ 5082: /***/ function (__unusedmodule, exports) {
"use strict";

// We use any as a valid input type
/* eslint-disable @typescript-eslint/no-explicit-any */
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Sanitizes an input into a string so it can be passed into issueCommand safely
* @param input input to sanitize into a string
*/
function toCommandValue(input) {
if (input === null || input === undefined) {
return "";
} else if (typeof input === "string" || input instanceof String) {
return input;
}
return JSON.stringify(input);
}
exports.toCommandValue = toCommandValue;
//# sourceMappingURL=utils.js.map

/***/
},

/***/ 5089: /***/ function (module) {
module.exports = {
version: "2.0",
Expand Down Expand Up @@ -197787,6 +197868,8 @@ module.exports = /******/ (function (modules, runtime) {
};
Object.defineProperty(exports, "__esModule", { value: true });
const command_1 = __webpack_require__(4431);
const file_command_1 = __webpack_require__(2102);
const utils_1 = __webpack_require__(5082);
const os = __importStar(__webpack_require__(2087));
const path = __importStar(__webpack_require__(5622));
/**
Expand All @@ -197809,11 +197892,20 @@ module.exports = /******/ (function (modules, runtime) {
/**
* Sets env variable for this action and future actions in the job
* @param name the name of the variable to set
* @param val the value of the variable
* @param val the value of the variable. Non-string values will be converted to a string via JSON.stringify
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function exportVariable(name, val) {
process.env[name] = val;
command_1.issueCommand("set-env", { name }, val);
const convertedVal = utils_1.toCommandValue(val);
process.env[name] = convertedVal;
const filePath = process.env["GITHUB_ENV"] || "";
if (filePath) {
const delimiter = "_GitHubActionsFileCommandDelimeter_";
const commandValue = `${name}<<${delimiter}${os.EOL}${convertedVal}${os.EOL}${delimiter}`;
file_command_1.issueCommand("ENV", commandValue);
} else {
command_1.issueCommand("set-env", { name }, convertedVal);
}
}
exports.exportVariable = exportVariable;
/**
Expand All @@ -197829,7 +197921,12 @@ module.exports = /******/ (function (modules, runtime) {
* @param inputPath
*/
function addPath(inputPath) {
command_1.issueCommand("add-path", {}, inputPath);
const filePath = process.env["GITHUB_PATH"] || "";
if (filePath) {
file_command_1.issueCommand("PATH", inputPath);
} else {
command_1.issueCommand("add-path", {}, inputPath);
}
process.env[
"PATH"
] = `${inputPath}${path.delimiter}${process.env["PATH"]}`;
Expand All @@ -197855,12 +197952,22 @@ module.exports = /******/ (function (modules, runtime) {
* Sets the value of an output.
*
* @param name name of the output to set
* @param value value to store
* @param value value to store. Non-string values will be converted to a string via JSON.stringify
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function setOutput(name, value) {
command_1.issueCommand("set-output", { name }, value);
}
exports.setOutput = setOutput;
/**
* Enables or disables the echoing of commands into stdout for the rest of the step.
* Echoing is disabled by default if ACTIONS_STEP_DEBUG is not set.
*
*/
function setCommandEcho(enabled) {
command_1.issue("echo", enabled ? "on" : "off");
}
exports.setCommandEcho = setCommandEcho;
//-----------------------------------------------------------------------
// Results
//-----------------------------------------------------------------------
Expand Down Expand Up @@ -197894,18 +198001,24 @@ module.exports = /******/ (function (modules, runtime) {
exports.debug = debug;
/**
* Adds an error issue
* @param message error issue message
* @param message error issue message. Errors will be converted to string via toString()
*/
function error(message) {
command_1.issue("error", message);
command_1.issue(
"error",
message instanceof Error ? message.toString() : message
);
}
exports.error = error;
/**
* Adds an warning issue
* @param message warning issue message
* @param message warning issue message. Errors will be converted to string via toString()
*/
function warning(message) {
command_1.issue("warning", message);
command_1.issue(
"warning",
message instanceof Error ? message.toString() : message
);
}
exports.warning = warning;
/**
Expand Down Expand Up @@ -197962,8 +198075,9 @@ module.exports = /******/ (function (modules, runtime) {
* Saves state for current action, the state can only be retrieved by this action's post job execution.
*
* @param name name of the state to store
* @param value value to store
* @param value value to store. Non-string values will be converted to a string via JSON.stringify
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function saveState(name, value) {
command_1.issueCommand("save-state", { name }, value);
}
Expand Down Expand Up @@ -221066,11 +221180,6 @@ module.exports = /******/ (function (modules, runtime) {
bundlesize: [
{ path: "./dist/octokit-rest.min.js.gz", maxSize: "33 kB" },
],
_resolved:
"https://registry.npmjs.org/@octokit/rest/-/rest-16.43.1.tgz",
_integrity:
"sha512-gfFKwRT/wFxq5qlNjnW2dh+qh74XgTQ2B179UX5K1HYCluioWj8Ndbgqw2PVqa1NnVJkGHp2ovMpVn/DImlmkw==",
_from: "@octokit/[email protected]",
};

/***/
Expand Down Expand Up @@ -295215,9 +295324,8 @@ module.exports = /******/ (function (modules, runtime) {
// the GITHUB_SHA value is NOT the correct value.
// See: https://github.com/aws-actions/aws-codebuild-run-build/issues/36
const sourceVersion =
process.env[`GITHUB_EVENT_NAME`] === "pull_request"
? (((payload || {}).pull_request || {}).head || {}).sha
: process.env[`GITHUB_SHA`];
core.getInput("source-version-override", { required: false }) ||
process.env[`GITHUB_SHA`];

assert(sourceVersion, "No source version could be evaluated.");
const buildspecOverride =
Expand Down
15 changes: 13 additions & 2 deletions local.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ const cb = require("./code-build");
const assert = require("assert");
const yargs = require("yargs");

const { projectName, buildspecOverride, envPassthrough, remote } = yargs
const {
projectName,
sourceVersionOverride,
buildspecOverride,
envPassthrough,
remote,
} = yargs
.option("project-name", {
alias: "p",
describe: "AWS CodeBuild Project Name",
Expand All @@ -20,6 +26,11 @@ const { projectName, buildspecOverride, envPassthrough, remote } = yargs
describe: "Path to buildspec file",
type: "string",
})
.option("source-version-override", {
alias: "s",
describe: "Source version, branch, commit",
type: "string",
})
.option("env-vars-for-codebuild", {
alias: "e",
describe: "List of environment variables to send to CodeBuild",
Expand All @@ -32,7 +43,7 @@ const { projectName, buildspecOverride, envPassthrough, remote } = yargs
type: "string",
}).argv;

const BRANCH_NAME = uuid();
const BRANCH_NAME = sourceVersionOverride || uuid();

const params = cb.inputs2Parameters({
projectName,
Expand Down
Loading