Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to reference a separate script file which processes and returns output? #433

Answered by yhakbar
rdhar asked this question in Q&A
Discussion options

Hi, I would like to strip out snippets of JS code into separate files outside of YAML for ease of legibility. However, I'm struggling to understand how best to achieve something this.

Before/Current
script: |
 const { data: list_comments } = await github.rest.issues.listComments({
 issue_number: context.issue.number,
 owner: context.repo.owner,
 per_page: 100,
 repo: context.repo.repo,
 });
 const get_comment = list_comments
 .sort((a, b) => b.id - a.id)
 .find((comment) => /^keyword/.test(comment.body));
 return {
 body: get_comment.body,
 id: get_comment.id,
 };
After/Proposed
script: |
 require(process.env.GITHUB_ACTION_PATH + '/comment.js');
// File: comment.js
const { data: list_comments } = await github.rest.issues.listComments({
 issue_number: context.issue.number,
 owner: context.repo.owner,
 per_page: 100,
 repo: context.repo.repo,
});
const get_comment = list_comments
 .sort((a, b) => b.id - a.id)
 .find((comment) => /^keyword/.test(comment.body));
return {
 body: get_comment.body,
 id: get_comment.id,
};

With this, I get: "SyntaxError: await is only valid in async functions and the top level bodies of modules."
If I drop the await, then I get: "ReferenceError: github is not defined."

I'm sure I'm missing something obvious with module.exports = ({ github, context }) => { ... }, but I'm not sure how best to address this particular script which: makes an API call, processes the response, and returns the output in that specific order.

Really appreciate any thoughts/inputs, thanks for your time.

You must be logged in to vote

I shared an example script here that got @rdhar on the right path.

The crux of the solution is that:

  1. The script to be referenced is requireed in the script step that needs re-usable code as an exported function.
  2. Async functions need to be awaited in the calling script step.

Replies: 1 comment 1 reply

Comment options

I shared an example script here that got @rdhar on the right path.

The crux of the solution is that:

  1. The script to be referenced is requireed in the script step that needs re-usable code as an exported function.
  2. Async functions need to be awaited in the calling script step.
You must be logged in to vote
1 reply
Comment options

Thank you for sharing, @yhakbar!

That script setup and module.exports = async... is exactly what was needed -- the only tweak was to replace the return { ... } with core.setOutput(), like so:

// Before
return {
 body: get_comment.body,
 id: get_comment.id,
};
// After
core.setOutput("body", get_comment.body);
core.setOutput("id", get_comment.id);

Similarly, I had to amend references to the output result within the action.yml workflow as well:

# Before
fromJSON(steps.comment.outputs.result)['body']
fromJSON(steps.comment.outputs.result)['id']
# After
steps.comment.outputs.body
steps.comment.outputs.id
Answer selected by rdhar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /