Skip to content

Commit dca9685

Browse files
meeroslavjoh-klein
andauthored
feat: switch to JS Action (#177)
* feat: switch to JS Action Refs: #158 * chore(docs): typos and misalignments * chore: remove value from outputs (schema validation error) * chore: token is defined twice * feat: revert file location change * feat: bump version --------- Co-authored-by: Johannes Klein <[email protected]>
1 parent e2e6dc8 commit dca9685

File tree

6 files changed

+72
-63
lines changed

6 files changed

+72
-63
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

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

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

7-
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.
7+
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.
88

99
Example of tags applied:
1010

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<p style="text-align: center;"><img src=".github/assets/nx.png"
22
width="100%" alt="Nx - Smart, Extensible Build Framework"></p>
33

4-
<h1 align="center">Set SHAs Action</h2>
4+
<h1 align="center">Set SHAs Action</h1>
55

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

88
- [Example Usage](#example-usage)
99
- [Configuration Options](#configuration-options)
@@ -141,7 +141,7 @@ This Action supports usage of your own self-hosted runners, but since it uses Gi
141141
jobs:
142142
myjob:
143143
runs-on: self-hosted
144-
container: my-org/my-amazing-image:v1.2.3-fresh
144+
container: my-org/my-amazing-image:v1.2.3-fresh
145145
name: My Job
146146
steps:
147147
- uses: actions/checkout@v4

action.yml

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ name: 'Nx set SHAs'
22
description: 'Derives SHAs for base and head for use in nx affected commands, optionally setting them as env variables for the current job'
33

44
inputs:
5+
token:
6+
description: 'The GitHub token used to perform git operations'
7+
default: ${{ github.token }}
58
main-branch-name:
69
description: 'The name of the main branch in your repo, used as the target of PRs. E.g. main, master etc'
710
default: 'main'
@@ -27,47 +30,15 @@ inputs:
2730
outputs:
2831
base:
2932
description: The value intended for use with --base or NX_BASE in all subsequent `nx affected` commands within the current workflow
30-
value: ${{ steps.setSHAs.outputs.base }}
3133
head:
3234
description: The value intended for use with --head or NX_HEAD in all subsequent `nx affected` commands within the current workflow
33-
value: ${{ steps.setSHAs.outputs.head }}
3435
noPreviousBuild:
3536
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"
36-
value: ${{ steps.setSHAs.outputs.noPreviousBuild }}
3737

3838
runs:
39-
using: 'composite'
40-
steps:
41-
- name: Set base and head SHAs used for nx affected
42-
id: setSHAs
43-
shell: bash
44-
env:
45-
gh_token: ${{ github.token }}
46-
main_branch_name: ${{ inputs.main-branch-name }}
47-
error_on_no_successful_workflow: ${{ inputs.error-on-no-successful-workflow }}
48-
last_successful_event: ${{ inputs.last-successful-event }}
49-
working_directory: ${{ inputs.working-directory }}
50-
working_id: ${{ inputs.workflow-id }}
51-
fallback_sha: ${{ inputs.fallback-sha }}
52-
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" "$fallback_sha"
39+
using: 'node20'
40+
main: 'dist/index.js'
5341

54-
- name: Log base and head SHAs used for nx affected
55-
shell: bash
56-
run: |
57-
echo "Base SHA"
58-
echo ${{ steps.setSHAs.outputs.base }}
59-
echo ""
60-
echo "Head SHA"
61-
echo ${{ steps.setSHAs.outputs.head }}
62-
echo ""
63-
64-
- name: Optionally set the derived SHAs as NX_BASE and NX_HEAD environment variables for the current job
65-
shell: bash
66-
if: ${{ inputs.set-environment-variables-for-job == 'true' }}
67-
run: |
68-
echo "NX_BASE=${{ steps.setSHAs.outputs.base }}" >> $GITHUB_ENV
69-
echo "NX_HEAD=${{ steps.setSHAs.outputs.head }}" >> $GITHUB_ENV
70-
echo "NX_BASE and NX_HEAD environment variables have been set for the current Job"
7142
branding:
7243
icon: 'terminal'
7344
color: 'blue'

dist/index.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64870,13 +64870,12 @@ const fs_1 = __nccwpck_require__(7147);
6487064870
const https_proxy_agent_1 = __nccwpck_require__(7219);
6487164871
const proxy_from_env_1 = __nccwpck_require__(3329);
6487264872
const { runId, repo: { repo, owner }, eventName, } = github.context;
64873-
process.env.GITHUB_TOKEN = process.argv[2];
64874-
const mainBranchName = process.argv[3];
64875-
const errorOnNoSuccessfulWorkflow = process.argv[4];
64876-
const lastSuccessfulEvent = process.argv[5];
64877-
const workingDirectory = process.argv[6];
64878-
const workflowId = process.argv[7];
64879-
const fallbackSHA = process.argv[8];
64873+
const mainBranchName = core.getInput('main-branch-name');
64874+
const errorOnNoSuccessfulWorkflow = core.getBooleanInput('error-on-no-successful-workflow');
64875+
const lastSuccessfulEvent = core.getInput('last-successful-event');
64876+
const workingDirectory = core.getInput('working-directory');
64877+
const workflowId = core.getInput('workflow-id');
64878+
const fallbackSHA = core.getInput('fallback-sha');
6488064879
const defaultWorkingDirectory = '.';
6488164880
const ProxifiedClient = action_1.Octokit.plugin(proxyPlugin);
6488264881
let BASE_SHA;
@@ -64894,7 +64893,7 @@ let BASE_SHA;
6489464893
const headResult = (0, child_process_1.spawnSync)('git', ['rev-parse', 'HEAD'], {
6489564894
encoding: 'utf-8',
6489664895
});
64897-
const HEAD_SHA = headResult.stdout;
64896+
let HEAD_SHA = headResult.stdout;
6489864897
if ((['pull_request', 'pull_request_target'].includes(eventName) &&
6489964898
!github.context.payload.pull_request.merged) ||
6490064899
eventName == 'merge_group') {
@@ -64917,7 +64916,7 @@ let BASE_SHA;
6491764916
return;
6491864917
}
6491964918
if (!BASE_SHA) {
64920-
if (errorOnNoSuccessfulWorkflow === 'true') {
64919+
if (errorOnNoSuccessfulWorkflow) {
6492164920
reportFailure(mainBranchName);
6492264921
return;
6492364922
}
@@ -64960,8 +64959,24 @@ let BASE_SHA;
6496064959
process.stdout.write(`Commit: ${BASE_SHA}\n`);
6496164960
}
6496264961
}
64963-
core.setOutput('base', stripNewLineEndings(BASE_SHA));
64964-
core.setOutput('head', stripNewLineEndings(HEAD_SHA));
64962+
BASE_SHA = stripNewLineEndings(BASE_SHA);
64963+
HEAD_SHA = stripNewLineEndings(HEAD_SHA);
64964+
// Log base and head SHAs used for nx affected
64965+
process.stdout.write('\n');
64966+
process.stdout.write('Base SHA');
64967+
process.stdout.write(BASE_SHA);
64968+
process.stdout.write('\n');
64969+
process.stdout.write('Head SHA');
64970+
process.stdout.write(HEAD_SHA);
64971+
process.stdout.write('\n');
64972+
// Optionally set the derived SHAs as NX_BASE and NX_HEAD environment variables for the current job
64973+
if (core.getBooleanInput('set-environment-variables-for-job')) {
64974+
core.exportVariable('NX_BASE', BASE_SHA);
64975+
core.exportVariable('NX_HEAD', HEAD_SHA);
64976+
process.stdout.write('NX_BASE and NX_HEAD environment variables have been set for the current Job');
64977+
}
64978+
core.setOutput('base', BASE_SHA);
64979+
core.setOutput('head', HEAD_SHA);
6496564980
}))();
6496664981
function reportFailure(branchName) {
6496764982
core.setFailed(`

find-successful-workflow.ts

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ const {
1111
repo: { repo, owner },
1212
eventName,
1313
} = github.context;
14-
process.env.GITHUB_TOKEN = process.argv[2];
15-
const mainBranchName = process.argv[3];
16-
const errorOnNoSuccessfulWorkflow = process.argv[4];
17-
const lastSuccessfulEvent = process.argv[5];
18-
const workingDirectory = process.argv[6];
19-
const workflowId = process.argv[7];
20-
const fallbackSHA = process.argv[8];
14+
const mainBranchName = core.getInput('main-branch-name');
15+
const errorOnNoSuccessfulWorkflow = core.getBooleanInput(
16+
'error-on-no-successful-workflow',
17+
);
18+
const lastSuccessfulEvent = core.getInput('last-successful-event');
19+
const workingDirectory = core.getInput('working-directory');
20+
const workflowId = core.getInput('workflow-id');
21+
const fallbackSHA = core.getInput('fallback-sha');
2122
const defaultWorkingDirectory = '.';
2223

2324
const ProxifiedClient = Octokit.plugin(proxyPlugin);
@@ -38,7 +39,7 @@ let BASE_SHA: string;
3839
const headResult = spawnSync('git', ['rev-parse', 'HEAD'], {
3940
encoding: 'utf-8',
4041
});
41-
const HEAD_SHA = headResult.stdout;
42+
let HEAD_SHA = headResult.stdout;
4243

4344
if (
4445
(['pull_request', 'pull_request_target'].includes(eventName) &&
@@ -73,7 +74,7 @@ let BASE_SHA: string;
7374
}
7475

7576
if (!BASE_SHA) {
76-
if (errorOnNoSuccessfulWorkflow === 'true') {
77+
if (errorOnNoSuccessfulWorkflow) {
7778
reportFailure(mainBranchName);
7879
return;
7980
} else {
@@ -130,8 +131,30 @@ let BASE_SHA: string;
130131
process.stdout.write(`Commit: ${BASE_SHA}\n`);
131132
}
132133
}
133-
core.setOutput('base', stripNewLineEndings(BASE_SHA));
134-
core.setOutput('head', stripNewLineEndings(HEAD_SHA));
134+
135+
BASE_SHA = stripNewLineEndings(BASE_SHA);
136+
HEAD_SHA = stripNewLineEndings(HEAD_SHA);
137+
138+
// Log base and head SHAs used for nx affected
139+
process.stdout.write('\n');
140+
process.stdout.write('Base SHA');
141+
process.stdout.write(BASE_SHA);
142+
process.stdout.write('\n');
143+
process.stdout.write('Head SHA');
144+
process.stdout.write(HEAD_SHA);
145+
process.stdout.write('\n');
146+
147+
// Optionally set the derived SHAs as NX_BASE and NX_HEAD environment variables for the current job
148+
if (core.getBooleanInput('set-environment-variables-for-job')) {
149+
core.exportVariable('NX_BASE', BASE_SHA);
150+
core.exportVariable('NX_HEAD', HEAD_SHA);
151+
process.stdout.write(
152+
'NX_BASE and NX_HEAD environment variables have been set for the current Job',
153+
);
154+
}
155+
156+
core.setOutput('base', BASE_SHA);
157+
core.setOutput('head', HEAD_SHA);
135158
})();
136159

137160
function reportFailure(branchName: string): void {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"private": true,
3-
"version": "4.1.2",
3+
"version": "4.2.0",
44
"license": "MIT",
55
"description": "This package.json is here purely to control the version of the Action, in combination with https://github.com/JamesHenry/publish-shell-action",
66
"scripts": {
7-
"build": "ncc build find-successful-workflow.ts --license licenses.txt",
7+
"build": "ncc build find-successful-workflow.ts -o dist --license licenses.txt",
88
"prepare": "is-ci || husky install",
99
"format:check": "prettier --check .",
1010
"format": "prettier --write ."

0 commit comments

Comments
 (0)