mirror of
https://github.com/actions/github-script.git
synced 2025-12-22 07:52:29 +00:00
Update documentation for relative require support
This commit is contained in:
43
README.md
43
README.md
@@ -18,6 +18,18 @@ arguments will be provided:
|
|||||||
- `core` A reference to the [@actions/core](https://github.com/actions/toolkit/tree/main/packages/core) package
|
- `core` A reference to the [@actions/core](https://github.com/actions/toolkit/tree/main/packages/core) package
|
||||||
- `glob` A reference to the [@actions/glob](https://github.com/actions/toolkit/tree/main/packages/glob) package
|
- `glob` A reference to the [@actions/glob](https://github.com/actions/toolkit/tree/main/packages/glob) package
|
||||||
- `io` A reference to the [@actions/io](https://github.com/actions/toolkit/tree/main/packages/io) package
|
- `io` A reference to the [@actions/io](https://github.com/actions/toolkit/tree/main/packages/io) package
|
||||||
|
- `require` Is available, with some caveats:
|
||||||
|
- The location of the module that runs this action is where the Actions
|
||||||
|
runner downloads the github-script action to, so the `require` passed to
|
||||||
|
your script is actually a proxy that intercepts calls to require relative
|
||||||
|
paths and transforms them into an absolute path in the working directory,
|
||||||
|
instead.
|
||||||
|
- If you want to require an npm module in your working directory, you still
|
||||||
|
need to specify the relative path, including `node_modules`, such as
|
||||||
|
`./node_modules/lodash`.
|
||||||
|
- If for some reason you need the non-wrapped `require`, there is an escape
|
||||||
|
hatch available: `__original_require__` is the original value of `require`
|
||||||
|
without our wrapping applied.
|
||||||
|
|
||||||
Since the `script` is just a function body, these values will already be
|
Since the `script` is just a function body, these values will already be
|
||||||
defined, so you don't have to (see examples below).
|
defined, so you don't have to (see examples below).
|
||||||
@@ -243,12 +255,10 @@ jobs:
|
|||||||
- uses: actions/github-script@v3
|
- uses: actions/github-script@v3
|
||||||
with:
|
with:
|
||||||
script: |
|
script: |
|
||||||
const script = require(`${process.env.GITHUB_WORKSPACE}/path/to/script.js`)
|
const script = require(`./path/to/script.js`)
|
||||||
console.log(script({github, context}))
|
console.log(script({github, context}))
|
||||||
```
|
```
|
||||||
|
|
||||||
_Note that the script path given to `require()` must be an **absolute path** in this case, hence using [`GITHUB_WORKSPACE`](https://docs.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables)._
|
|
||||||
|
|
||||||
And then export a function from your module:
|
And then export a function from your module:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
@@ -282,24 +292,24 @@ jobs:
|
|||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
- uses: actions/github-script@v3
|
- uses: actions/github-script@v3
|
||||||
env:
|
env:
|
||||||
SHA: "${{env.parentSHA}}"
|
SHA: '${{env.parentSHA}}'
|
||||||
with:
|
with:
|
||||||
script: |
|
script: |
|
||||||
const script = require(`${process.env.GITHUB_WORKSPACE}/path/to/script.js`)
|
const script = require(`./path/to/script.js`)
|
||||||
await script({github, context, core})
|
await script({github, context, core})
|
||||||
```
|
```
|
||||||
|
|
||||||
And then export an async function from your module:
|
And then export an async function from your module:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
module.exports = async ({ github, context, core }) => {
|
module.exports = async ({github, context, core}) => {
|
||||||
const { SHA } = process.env
|
const {SHA} = process.env
|
||||||
const commit = await github.repos.getCommit({
|
const commit = await github.repos.getCommit({
|
||||||
owner: context.repo.owner,
|
owner: context.repo.owner,
|
||||||
repo: context.repo.repo,
|
repo: context.repo.repo,
|
||||||
ref: `${SHA}`
|
ref: `${SHA}`
|
||||||
})
|
})
|
||||||
core.exportVariable('author', commit.data.commit.author.email);
|
core.exportVariable('author', commit.data.commit.author.email)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -324,13 +334,18 @@ jobs:
|
|||||||
- uses: actions/github-script@v3
|
- uses: actions/github-script@v3
|
||||||
with:
|
with:
|
||||||
script: |
|
script: |
|
||||||
const execa = require(`${process.env.GITHUB_WORKSPACE}/node_modules/execa`)
|
const execa = require(`./node_modules/execa`)
|
||||||
|
|
||||||
const { stdout } = await execa('echo', ['hello', 'world'])
|
const { stdout } = await execa('echo', ['hello', 'world'])
|
||||||
|
|
||||||
console.log(stdout)
|
console.log(stdout)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
_(Note that at this time, one still has to specify `node_modules` in the
|
||||||
|
require path for modules in the working directory of the step that is
|
||||||
|
running. Hopefully we will have a solution for this in the future, but not
|
||||||
|
quite, yet.)_
|
||||||
|
|
||||||
### Use env as input
|
### Use env as input
|
||||||
|
|
||||||
You can set env vars to use them in your script:
|
You can set env vars to use them in your script:
|
||||||
|
|||||||
Reference in New Issue
Block a user