Skip to content
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

Switch to JS Action #163

Open
wants to merge 4 commits into
base: main
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
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<p style="text-align: center;"><img src=".github/assets/nx.png"
width="100%" alt="Nx - Smart, Extensible Build Framework"></p>

<h1 align="center">Set SHAs Action</h2>
<h1 align="center">Set SHAs Action</h1>

✨ A Github Action which sets the base and head SHAs required for the `nx affected` commands in CI
✨ A GitHub Action which sets the base and head SHAs required for the `nx affected` commands in CI

- [Example Usage](#example-usage)
- [Configuration Options](#configuration-options)
Expand Down Expand Up @@ -141,7 +141,7 @@ This Action supports usage of your own self-hosted runners, but since it uses Gi
jobs:
myjob:
runs-on: self-hosted
container: my-org/my-amazing-image:v1.2.3-fresh
container: my-org/my-amazing-image:v1.2.3-fresh
name: My Job
steps:
- uses: actions/checkout@v4
Expand Down
38 changes: 5 additions & 33 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 All @@ -27,46 +30,15 @@ inputs:
outputs:
base:
description: The value intended for use with --base or NX_BASE in all subsequent `nx affected` commands within the current workflow
value: ${{ steps.setSHAs.outputs.base }}
head:
description: The value intended for use with --head or NX_HEAD in all subsequent `nx affected` commands within the current workflow
value: ${{ steps.setSHAs.outputs.head }}
noPreviousBuild:
description: "Used to check if a previous run was found in order to perform additional logic later on in your workflow, the only possible values is the string 'true', otherwise it won't be set"
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'
41 changes: 28 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,12 @@ 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];
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 +37883,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 +37906,7 @@ let BASE_SHA;
return;
}
if (!BASE_SHA) {
if (errorOnNoSuccessfulWorkflow === 'true') {
if (errorOnNoSuccessfulWorkflow) {
reportFailure(mainBranchName);
return;
}
Expand Down Expand Up @@ -37940,8 +37939,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 +38398,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
45 changes: 34 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,14 @@ 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];
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 +39,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 +74,7 @@ let BASE_SHA: string;
}

if (!BASE_SHA) {
if (errorOnNoSuccessfulWorkflow === 'true') {
if (errorOnNoSuccessfulWorkflow) {
reportFailure(mainBranchName);
return;
} else {
Expand Down Expand Up @@ -122,8 +123,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