8000 Document how to use npm packages and env input by nihalgonsalves · Pull Request #126 · actions/github-script · GitHub
[go: up one dir, main page]

Skip to content

Document how to use npm packages and env input #126

8000
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

Merged
merged 3 commits into from
Mar 29, 2021
Merged

Document how to use npm packages and env input #126

merged 3 commits into from
Mar 29, 2021

Conversation

nihalgonsalves
Copy link
Contributor
@nihalgonsalves nihalgonsalves commented Mar 29, 2021

ℹ️ Updated to document env and process.env as inputs.


We've been using actions/github-script to glue together different build tools. We've had cases where we want to passthrough outputs of other scripts to this action –

  • One way would be to just use an input value:

    - uses: actions/github-script@v3
      with:
        foo: 'bar'
        script: console.log(core.getInput('foo'))

    which works, but displays this warning:

    Warning: Unexpected input(s) 'foo', valid inputs are ['script', 'github-token', 'debug', 'user-agent', 'previews', 'result-encoding']
    
  • Another way would be to inline the value:

    - uses: actions/github-script@v3
      with:
        script: console.log('${{ 'foo' }}')

    but this can cause issues depending on what the value of the string is, and requires you to be aware of how your string will interact with the program:

    - uses: actions/github-script@v3
      with:
        script: console.log('${{ 'Single quotes can kill: '' a script' }}')
    
    # SyntaxError: missing ) after argument list

So this PR introduces a simple input variable that is exposed directly to the function:

- uses: actions/github-script@v3
  with:
    input: hello world
    script: console.log(input)

It can be combined with JSON.parse(input) in the script to pass through stringified JSON (from other jobs or CLI tools, for example).


I also made two other changes

  • Changed the PR comment workflow to check whether the base and head repos are the same, so that changes can be tested more easily on forks (for example, checking my changes here: nihalgonsalves/github-script#1). It still won't comment on PRs across forks (like this one).
  • In addition to the docs for this input variable, I also added a README section documenting how installed packages can be used.

@nihalgonsalves nihalgonsalves requested a review from a team March 29, 2021 15:34
@nihalgonsalves nihalgonsalves changed the title Add input params Add an input variable Mar 29, 2021
@joshmgross
Copy link
Contributor

Would step environment variables work here and avoid the quotes issue?

https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepsenv

A couple concerns with this approach:

  1. input is not descriptive name
  2. It only supports a single input.

If environment variables support your case, I'd prefer we document how to use those as inputs.

For example:

- uses: actions/github-script@v3
  env:
    FIRST_NAME: Mona
    LAST_NAME: Octocat
  with:
    script: console.log(`Hello ${process.env.FIRST_NAME} ${process.env.LAST_NAME}`)

@nihalgonsalves nihalgonsalves changed the title Add an input variable Document how to use npm packages and env input Mar 29, 2021
@nihalgonsalves
Copy link
Contributor Author
nihalgonsalves commented Mar 29, 2021

@joshmgross Thanks - not sure why I didn't think of that. It works perfectly. I updated my PR to simply update the README.

Copy link
Contributor
@joshmgross joshmgross left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for this!

@nihalgonsalves
Copy link
Contributor Author

I updated the npm command example to remove the -g flag.

Thanks for the quick review! :)

@joshmgross joshmgross merged commit 8685086 into actions:main Mar 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0