From f0d6dadc30d7c2d6c1397da4f57e45d66a7caa39 Mon Sep 17 00:00:00 2001 From: Johannes Klein Date: Wed, 26 Jun 2024 11:15:49 +0200 Subject: [PATCH 1/4] feat: switch to JS Action Refs: #158 --- CONTRIBUTING.md | 4 +- action.yml | 35 +++------------- dist/index.js | 42 ++++++++++++++------ package.json | 2 +- find-successful-workflow.ts => src/main.ts | 46 ++++++++++++++++------ 5 files changed, 72 insertions(+), 57 deletions(-) rename find-successful-workflow.ts => src/main.ts (85%) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 15be3f1..d0bafec 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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: diff --git a/action.yml b/action.yml index 2d10fc5..604ea78 100644 --- a/action.yml +++ b/action.yml @@ -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' @@ -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' diff --git a/dist/index.js b/dist/index.js index 01d3178..c6c2b1c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -37838,7 +37838,7 @@ function wrappy (fn, cb) { /***/ }), -/***/ 5468: +/***/ 399: /***/ (function(__unused_webpack_module, exports, __nccwpck_require__) { "use strict"; @@ -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; @@ -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') { @@ -37907,7 +37907,7 @@ let BASE_SHA; return; } if (!BASE_SHA) { - if (errorOnNoSuccessfulWorkflow === 'true') { + if (errorOnNoSuccessfulWorkflow) { reportFailure(mainBranchName); return; } @@ -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(` @@ -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__; /******/ /******/ })() diff --git a/package.json b/package.json index 7412322..3a705f6 100644 --- a/package.json +++ b/package.json @@ -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 ." diff --git a/find-successful-workflow.ts b/src/main.ts similarity index 85% rename from find-successful-workflow.ts rename to src/main.ts index c2c1324..d93dade 100644 --- a/find-successful-workflow.ts +++ b/src/main.ts @@ -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); @@ -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) && @@ -73,7 +75,7 @@ let BASE_SHA: string; } if (!BASE_SHA) { - if (errorOnNoSuccessfulWorkflow === 'true') { + if (errorOnNoSuccessfulWorkflow) { reportFailure(mainBranchName); return; } else { @@ -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 { From b88c894a23c97ba7dace622c87da72ae73e8e66c Mon Sep 17 00:00:00 2001 From: Johannes Klein Date: Wed, 26 Jun 2024 11:18:03 +0200 Subject: [PATCH 2/4] chore(docs): typos and misalignments --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a86b4cf..24f7d2f 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@

Nx - Smart, Extensible Build Framework

-

Set SHAs Action

+

Set SHAs Action

-✨ 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) @@ -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 From 7b5c9d6d4f83481dac114ecc44bbfd90068c0156 Mon Sep 17 00:00:00 2001 From: Johannes Klein Date: Wed, 26 Jun 2024 11:18:38 +0200 Subject: [PATCH 3/4] chore: remove value from outputs (schema validation error) --- action.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/action.yml b/action.yml index 604ea78..00a2f14 100644 --- a/action.yml +++ b/action.yml @@ -30,13 +30,10 @@ 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: 'node20' From 1556a4c93fea5912e0e1c21e5907e53553b31a6a Mon Sep 17 00:00:00 2001 From: Johannes Klein Date: Wed, 26 Jun 2024 11:33:13 +0200 Subject: [PATCH 4/4] chore: token is defined twice --- dist/index.js | 1 - src/main.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index c6c2b1c..07e7c72 100644 --- a/dist/index.js +++ b/dist/index.js @@ -37861,7 +37861,6 @@ 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 = 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'); diff --git a/src/main.ts b/src/main.ts index d93dade..a6b657c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,7 +11,6 @@ const { repo: { repo, owner }, eventName, } = github.context; -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',