Merge pull request #131 from esker-software/Update-readme-with-async-example

Update readme with async example
This commit is contained in:
Josh Gross
2021-04-15 12:18:40 -04:00
committed by GitHub

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 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 Actions Toolkit libraries, you'll want to pass them as arguments to your
external function. 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 action](https://github.com/actions/checkout) to make sure your script file is
available. 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 ### Use npm packages
Like importing your own files above, you can also use installed modules: Like importing your own files above, you can also use installed modules: