Movatterモバイル変換


[0]ホーム

URL:


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

Answeredbyyhakbar
rdhar asked this question inQ&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.jsconst{data:list_comments}=awaitgithub.rest.issues.listComments({issue_number:context.issue.number,owner:context.repo.owner,per_page:100,repo:context.repo.repo,});constget_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 theawait, then I get: "ReferenceError: github is not defined."

I'm sure I'm missing something obvious withmodule.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 scripthere that got@rdhar on the right path.

The crux of the solution is that:

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

Replies: 1 comment 1 reply

Comment options

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

The crux of the solution is that:

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

Thank you for sharing,@yhakbar!

That script setup andmodule.exports = async... is exactly what was needed -- the only tweak was to replace thereturn { ... } withcore.setOutput(), like so:

// Beforereturn{body:get_comment.body,id:get_comment.id,};// Aftercore.setOutput("body",get_comment.body);core.setOutput("id",get_comment.id);

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

# BeforefromJSON(steps.comment.outputs.result)['body']fromJSON(steps.comment.outputs.result)['id']# Aftersteps.comment.outputs.bodysteps.comment.outputs.id
Answer selected byrdhar
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@rdhar@yhakbar

[8]ページ先頭

©2009-2025 Movatter.jp