Merge pull request #126 from nihalgonsalves/ng/input

Document how to use npm packages and env input
This commit is contained in:
Josh Gross 2021-03-29 17:35:07 -04:00 committed by GitHub
commit 8685086334
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -71,14 +71,14 @@ in case you need to use a non-default token.
By default, github-script will use the token provided to your workflow. By default, github-script will use the token provided to your workflow.
### Print the available attributes of context: ### Print the available attributes of context
```yaml ```yaml
- name: View context attributes - name: View context attributes
uses: actions/github-script@v3 uses: actions/github-script@v3
with: with:
script: console.log(context) script: console.log(context)
``` ```
### Comment on an issue ### Comment on an issue
@ -200,7 +200,6 @@ contain the actual diff text.
You can use the `github.graphql` object to run custom GraphQL queries against the GitHub API. You can use the `github.graphql` object to run custom GraphQL queries against the GitHub API.
```yaml ```yaml
jobs: jobs:
list-issues: list-issues:
runs-on: ubuntu-latest runs-on: ubuntu-latest
@ -225,7 +224,6 @@ jobs:
} }
const result = await github.graphql(query, variables) const result = await github.graphql(query, variables)
console.log(result) console.log(result)
``` ```
### Run a separate file ### Run a separate file
@ -268,3 +266,53 @@ external function.
Additionally, you'll want to use the [checkout Additionally, you'll want to use the [checkout
action](https://github.com/actions/checkout) to make sure your script file is action](https://github.com/actions/checkout) to make sure your script file is
available. available.
### Use npm packages
Like importing your own files above, you can also use installed modules:
```yaml
on: push
jobs:
echo-input:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 14
- run: npm ci
# or one-off:
- run: npm install execa
- uses: actions/github-script@v3
with:
script: |
const execa = require(`${process.env.GITHUB_WORKSPACE}/node_modules/execa`)
const { stdout } = await execa('echo', ['hello', 'world'])
console.log(stdout)
```
### Use env as input
You can set env vars to use them in your script:
```yaml
on: push
jobs:
echo-input:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v3
env:
FIRST_NAME: Mona
LAST_NAME: Octocat
with:
script: |
const { FIRST_NAME, LAST_NAME } = process.env
console.log(`Hello ${FIRST_NAME} ${LAST_NAME}`)
```