Update readme with async example

This commit is contained in:
Léandre DA SILVA
2021-04-09 17:12:13 +02:00
committed by GitHub
parent f05a81df23
commit c72dc00003

View File

@@ -257,9 +257,6 @@ module.exports = ({github, context}) => {
}
```
You can also use async functions in this manner, as long as you `await` it in
the inline script.
Note that because you can't `require` things like the GitHub context or
Actions Toolkit libraries, you'll want to pass them as arguments to your
external function.
@@ -268,6 +265,44 @@ Additionally, you'll want to use the [checkout
action](https://github.com/actions/checkout) to make sure your script file is
available.
### Run a separate file with an async function
You can also use async functions in this manner, as long as you `await` it in
the inline script.
In your workflow:
```yaml
on: push
jobs:
echo-input:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/github-script@v3
env:
SHA: "${{env.parentSHA}}"
with:
script: |
const script = require(`${process.env.GITHUB_WORKSPACE}/path/to/script.js`)
await script({github, context, core})
```
And then export an async function from your module:
```javascript
module.exports = async ({ github, context, core }) => {
const { SHA } = process.env
const commit = await github.repos.getCommit({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `${SHA}`
})
core.exportVariable('author', commit.data.commit.author.email);
}
```
### Use npm packages
Like importing your own files above, you can also use installed modules: