Skip to content

Commit

Permalink
feat: switch to JS Action
Browse files Browse the repository at this point in the history
Refs: nrwl#158
  • Loading branch information
joh-klein committed Jun 26, 2024
1 parent 5ba8bac commit f0d6dad
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 57 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

In order to publish a new version of the action, simply update the "version" in the package.json and merge into the main branch.

If your changed files include `find-successful-workflow.js`, then you should also update the bundle in the `dist` folder using `npm run build`.
If your changed files include `src/main.ts`, then you should also update the bundle in the `dist` folder using `npm run build`.

The workflow at ./github/workflows/publish.yml will apply the new version in the form of tags, which is all that is needed to publish an Action.
The workflow at `./github/workflows/publish.yml` will apply the new version in the form of tags, which is all that is needed to publish an Action.

Example of tags applied:

Expand Down
35 changes: 5 additions & 30 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name: 'Nx set SHAs'
description: 'Derives SHAs for base and head for use in nx affected commands, optionally setting them as env variables for the current job'

inputs:
token:
description: 'The GitHub token used to perform git operations'
default: ${{ github.token }}
main-branch-name:
description: 'The name of the main branch in your repo, used as the target of PRs. E.g. main, master etc'
default: 'main'
Expand Down Expand Up @@ -36,37 +39,9 @@ outputs:
value: ${{ steps.setSHAs.outputs.noPreviousBuild }}

runs:
using: 'composite'
steps:
- name: Set base and head SHAs used for nx affected
id: setSHAs
shell: bash
env:
gh_token: ${{ github.token }}
main_branch_name: ${{ inputs.main-branch-name }}
error_on_no_successful_workflow: ${{ inputs.error-on-no-successful-workflow }}
last_successful_event: ${{ inputs.last-successful-event }}
working_directory: ${{ inputs.working-directory }}
working_id: ${{ inputs.workflow-id }}
run: node "$GITHUB_ACTION_PATH/dist/index.js" "$gh_token" "$main_branch_name" "$error_on_no_successful_workflow" "$last_successful_event" "$working_directory" "$working_id"
using: 'node20'
main: 'dist/index.js'

- name: Log base and head SHAs used for nx affected
shell: bash
run: |
echo "Base SHA"
echo ${{ steps.setSHAs.outputs.base }}
echo ""
echo "Head SHA"
echo ${{ steps.setSHAs.outputs.head }}
echo ""
- name: Optionally set the derived SHAs as NX_BASE and NX_HEAD environment variables for the current job
shell: bash
if: ${{ inputs.set-environment-variables-for-job == 'true' }}
run: |
echo "NX_BASE=${{ steps.setSHAs.outputs.base }}" >> $GITHUB_ENV
echo "NX_HEAD=${{ steps.setSHAs.outputs.head }}" >> $GITHUB_ENV
echo "NX_BASE and NX_HEAD environment variables have been set for the current Job"
branding:
icon: 'terminal'
color: 'blue'
42 changes: 29 additions & 13 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37838,7 +37838,7 @@ function wrappy (fn, cb) {

/***/ }),

/***/ 5468:
/***/ 399:
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {

"use strict";
Expand All @@ -37861,13 +37861,13 @@ const fs_1 = __nccwpck_require__(7147);
const https_proxy_agent_1 = __nccwpck_require__(7219);
const proxy_from_env_1 = __nccwpck_require__(3329);
const { runId, repo: { repo, owner }, eventName, } = github.context;
process.env.GITHUB_TOKEN = process.argv[2];
const mainBranchName = process.argv[3];
const errorOnNoSuccessfulWorkflow = process.argv[4];
const lastSuccessfulEvent = process.argv[5];
const workingDirectory = process.argv[6];
const workflowId = process.argv[7];
const fallbackSHA = process.argv[8];
process.env.GITHUB_TOKEN = core.getInput('token', { required: true });
const mainBranchName = core.getInput('main-branch-name');
const errorOnNoSuccessfulWorkflow = core.getBooleanInput('error-on-no-successful-workflow');
const lastSuccessfulEvent = core.getInput('last-successful-event');
const workingDirectory = core.getInput('working-directory');
const workflowId = core.getInput('workflow-id');
const fallbackSHA = core.getInput('fallback-sha');
const defaultWorkingDirectory = '.';
const ProxifiedClient = action_1.Octokit.plugin(proxyPlugin);
let BASE_SHA;
Expand All @@ -37884,7 +37884,7 @@ let BASE_SHA;
const headResult = (0, child_process_1.spawnSync)('git', ['rev-parse', 'HEAD'], {
encoding: 'utf-8',
});
const HEAD_SHA = headResult.stdout;
let HEAD_SHA = headResult.stdout;
if ((['pull_request', 'pull_request_target'].includes(eventName) &&
!github.context.payload.pull_request.merged) ||
eventName == 'merge_group') {
Expand All @@ -37907,7 +37907,7 @@ let BASE_SHA;
return;
}
if (!BASE_SHA) {
if (errorOnNoSuccessfulWorkflow === 'true') {
if (errorOnNoSuccessfulWorkflow) {
reportFailure(mainBranchName);
return;
}
Expand Down Expand Up @@ -37940,8 +37940,24 @@ let BASE_SHA;
process.stdout.write(`Commit: ${BASE_SHA}\n`);
}
}
core.setOutput('base', stripNewLineEndings(BASE_SHA));
core.setOutput('head', stripNewLineEndings(HEAD_SHA));
BASE_SHA = stripNewLineEndings(BASE_SHA);
HEAD_SHA = stripNewLineEndings(HEAD_SHA);
// Log base and head SHAs used for nx affected
process.stdout.write('\n');
process.stdout.write('Base SHA');
process.stdout.write(BASE_SHA);
process.stdout.write('\n');
process.stdout.write('Head SHA');
process.stdout.write(HEAD_SHA);
process.stdout.write('\n');
// Optionally set the derived SHAs as NX_BASE and NX_HEAD environment variables for the current job
if (core.getBooleanInput('set-environment-variables-for-job')) {
core.exportVariable('NX_BASE', BASE_SHA);
core.exportVariable('NX_HEAD', HEAD_SHA);
process.stdout.write('NX_BASE and NX_HEAD environment variables have been set for the current Job');
}
core.setOutput('base', BASE_SHA);
core.setOutput('head', HEAD_SHA);
}))();
function reportFailure(branchName) {
core.setFailed(`
Expand Down Expand Up @@ -38383,7 +38399,7 @@ module.exports = JSON.parse('[[[0,44],"disallowed_STD3_valid"],[[45,46],"valid"]
/******/ // startup
/******/ // Load entry module and return exports
/******/ // This entry module is referenced by other modules so it can't be inlined
/******/ var __webpack_exports__ = __nccwpck_require__(5468);
/******/ var __webpack_exports__ = __nccwpck_require__(399);
/******/ module.exports = __webpack_exports__;
/******/
/******/ })()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"description": "This package.json is here purely to control the version of the Action, in combination with https://github.com/JamesHenry/publish-shell-action",
"scripts": {
"build": "ncc build find-successful-workflow.ts --license licenses.txt",
"build": "ncc build src/main.ts -o dist --license licenses.txt",
"prepare": "is-ci || husky install",
"format:check": "prettier --check .",
"format": "prettier --write ."
Expand Down
46 changes: 35 additions & 11 deletions find-successful-workflow.ts → src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ const {
repo: { repo, owner },
eventName,
} = github.context;
process.env.GITHUB_TOKEN = process.argv[2];
const mainBranchName = process.argv[3];
const errorOnNoSuccessfulWorkflow = process.argv[4];
const lastSuccessfulEvent = process.argv[5];
const workingDirectory = process.argv[6];
const workflowId = process.argv[7];
const fallbackSHA = process.argv[8];
process.env.GITHUB_TOKEN = core.getInput('token', { required: true });
const mainBranchName = core.getInput('main-branch-name');
const errorOnNoSuccessfulWorkflow = core.getBooleanInput(
'error-on-no-successful-workflow',
);
const lastSuccessfulEvent = core.getInput('last-successful-event');
const workingDirectory = core.getInput('working-directory');
const workflowId = core.getInput('workflow-id');
const fallbackSHA = core.getInput('fallback-sha');
const defaultWorkingDirectory = '.';

const ProxifiedClient = Octokit.plugin(proxyPlugin);
Expand All @@ -38,7 +40,7 @@ let BASE_SHA: string;
const headResult = spawnSync('git', ['rev-parse', 'HEAD'], {
encoding: 'utf-8',
});
const HEAD_SHA = headResult.stdout;
let HEAD_SHA = headResult.stdout;

if (
(['pull_request', 'pull_request_target'].includes(eventName) &&
Expand Down Expand Up @@ -73,7 +75,7 @@ let BASE_SHA: string;
}

if (!BASE_SHA) {
if (errorOnNoSuccessfulWorkflow === 'true') {
if (errorOnNoSuccessfulWorkflow) {
reportFailure(mainBranchName);
return;
} else {
Expand Down Expand Up @@ -122,8 +124,30 @@ let BASE_SHA: string;
process.stdout.write(`Commit: ${BASE_SHA}\n`);
}
}
core.setOutput('base', stripNewLineEndings(BASE_SHA));
core.setOutput('head', stripNewLineEndings(HEAD_SHA));

BASE_SHA = stripNewLineEndings(BASE_SHA);
HEAD_SHA = stripNewLineEndings(HEAD_SHA);

// Log base and head SHAs used for nx affected
process.stdout.write('\n');
process.stdout.write('Base SHA');
process.stdout.write(BASE_SHA);
process.stdout.write('\n');
process.stdout.write('Head SHA');
process.stdout.write(HEAD_SHA);
process.stdout.write('\n');

// Optionally set the derived SHAs as NX_BASE and NX_HEAD environment variables for the current job
if (core.getBooleanInput('set-environment-variables-for-job')) {
core.exportVariable('NX_BASE', BASE_SHA);
core.exportVariable('NX_HEAD', HEAD_SHA);
process.stdout.write(
'NX_BASE and NX_HEAD environment variables have been set for the current Job',
);
}

core.setOutput('base', BASE_SHA);
core.setOutput('head', HEAD_SHA);
})();

function reportFailure(branchName: string): void {
Expand Down

0 comments on commit f0d6dad

Please sign in to comment.