Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3ca4cd5

Browse filesBrowse files
committed
Update documentation for relative require support
1 parent 83c92d6 commit 3ca4cd5
Copy full SHA for 3ca4cd5

File tree

1 file changed

+29
-14
lines changed
Filter options

1 file changed

+29
-14
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+29-14Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ arguments will be provided:
1818
- `core` A reference to the [@actions/core](https://github.com/actions/toolkit/tree/main/packages/core) package
1919
- `glob` A reference to the [@actions/glob](https://github.com/actions/toolkit/tree/main/packages/glob) package
2020
- `io` A reference to the [@actions/io](https://github.com/actions/toolkit/tree/main/packages/io) package
21+
- `require` Is available, with some caveats:
22+
- The location of the module that runs this action is where the Actions
23+
runner downloads the github-script action to, so the `require` passed to
24+
your script is actually a proxy that intercepts calls to require relative
25+
paths and transforms them into an absolute path in the working directory,
26+
instead.
27+
- If you want to require an npm module in your working directory, you still
28+
need to specify the relative path, including `node_modules`, such as
29+
`./node_modules/lodash`.
30+
- If for some reason you need the non-wrapped `require`, there is an escape
31+
hatch available: `__original_require__` is the original value of `require`
32+
without our wrapping applied.
2133

2234
Since the `script` is just a function body, these values will already be
2335
defined, so you don't have to (see examples below).
@@ -243,12 +255,10 @@ jobs:
243255
- uses: actions/github-script@v3
244256
with:
245257
script: |
246-
const script = require(`${process.env.GITHUB_WORKSPACE}/path/to/script.js`)
258+
const script = require(`./path/to/script.js`)
247259
console.log(script({github, context}))
248260
```
249261
250-
_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)._
251-
252262
And then export a function from your module:
253263
254264
```javascript
@@ -282,24 +292,24 @@ jobs:
282292
- uses: actions/checkout@v2
283293
- uses: actions/github-script@v3
284294
env:
285-
SHA: "${{env.parentSHA}}"
295+
SHA: '${{env.parentSHA}}'
286296
with:
287297
script: |
288-
const script = require(`${process.env.GITHUB_WORKSPACE}/path/to/script.js`)
298+
const script = require(`./path/to/script.js`)
289299
await script({github, context, core})
290300
```
291301
292302
And then export an async function from your module:
293303
294304
```javascript
295-
module.exports = async ({ github, context, core }) => {
296-
const { SHA } = process.env
297-
const commit = await github.repos.getCommit({
298-
owner: context.repo.owner,
299-
repo: context.repo.repo,
300-
ref: `${SHA}`
301-
})
302-
core.exportVariable('author', commit.data.commit.author.email);
305+
module.exports = async ({github, context, core}) => {
306+
const {SHA} = process.env
307+
const commit = await github.repos.getCommit({
308+
owner: context.repo.owner,
309+
repo: context.repo.repo,
310+
ref: `${SHA}`
311+
})
312+
core.exportVariable('author', commit.data.commit.author.email)
303313
}
304314
```
305315

@@ -324,13 +334,18 @@ jobs:
324334
- uses: actions/github-script@v3
325335
with:
326336
script: |
327-
const execa = require(`${process.env.GITHUB_WORKSPACE}/node_modules/execa`)
337+
const execa = require(`./node_modules/execa`)
328338
329339
const { stdout } = await execa('echo', ['hello', 'world'])
330340
331341
console.log(stdout)
332342
```
333343
344+
_(Note that at this time, one still has to specify `node_modules` in the
345+
require path for modules in the working directory of the step that is
346+
running. Hopefully we will have a solution for this in the future, but not
347+
quite, yet.)_
348+
334349
### Use env as input
335350

336351
You can set env vars to use them in your script:

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.