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 291af5c

Browse filesBrowse files
committed
Run script in GITHUB_WORKSPACE directory
Change to the GITHUB_WORKSPACE directory before running the script so that callers do not have to exlicitly use the GITHUB_WORKSPACE environment variable when requiring script files. Workflow run steps are run within the GITHUB_WORKSPACE so this seems like the expected default.
1 parent e86864d commit 291af5c
Copy full SHA for 291af5c

File tree

4 files changed

+74
-7
lines changed
Filter options

4 files changed

+74
-7
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ By default, github-script will use the token provided to your workflow.
7878
uses: actions/github-script@v3
7979
with:
8080
script: console.log(context)
81-
```
81+
```
8282

8383
### Comment on an issue
8484

@@ -244,11 +244,12 @@ jobs:
244244
- uses: actions/github-script@v3
245245
with:
246246
script: |
247-
const script = require(`${process.env.GITHUB_WORKSPACE}/path/to/script.js`)
247+
const script = require(`./path/to/script.js`)
248248
console.log(script({github, context}))
249249
```
250250
251-
_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+
The script will be run within the [`GITHUB_WORKSPACE`](https://docs.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables)
252+
directory.
252253

253254
And then export a function from your module:
254255

‎__test__/async-function.test.ts

Copy file name to clipboardExpand all lines: __test__/async-function.test.ts
+44Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,48 @@ describe('callAsyncFunction', () => {
2525
test('can access console', async () => {
2626
await callAsyncFunction({} as any, 'console')
2727
})
28+
29+
describe('change directory', () => {
30+
const MOCK_CWD = '/path/to/action'
31+
const MOCK_GITHUB_WORKSPACE = '/path/to/code'
32+
const PREV_GITHUB_WORKSPACE = process.env.GITHUB_WORKSPACE
33+
34+
let chdir: jest.SpyInstance
35+
36+
beforeEach(() => {
37+
jest.resetModules()
38+
jest.clearAllMocks()
39+
40+
let cwd = MOCK_CWD
41+
42+
chdir = jest.spyOn(process, 'chdir').mockImplementation(directory => {
43+
cwd = directory
44+
})
45+
jest.spyOn(process, 'cwd').mockImplementation(() => {
46+
return cwd
47+
})
48+
})
49+
50+
afterAll(() => {
51+
process.env.GITHUB_WORKSPACE = PREV_GITHUB_WORKSPACE
52+
})
53+
54+
test('changes to GITHUB_WORKSPACE if environment variable is set', async () => {
55+
process.env.GITHUB_WORKSPACE = MOCK_GITHUB_WORKSPACE
56+
57+
await callAsyncFunction({} as any, 'process')
58+
59+
expect(chdir.mock.calls.length).toBe(2)
60+
expect(chdir.mock.calls[0][0]).toBe(MOCK_GITHUB_WORKSPACE)
61+
expect(chdir.mock.calls[1][0]).toBe(MOCK_CWD)
62+
})
63+
64+
test('does not change directory if GITHUB_WORKSPACE is not set', async () => {
65+
delete process.env.GITHUB_WORKSPACE
66+
67+
await callAsyncFunction({} as any, 'process')
68+
69+
expect(chdir.mock.calls.length).toBe(0)
70+
})
71+
})
2872
})

‎dist/index.js

Copy file name to clipboardExpand all lines: dist/index.js
+13-2Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5365,8 +5365,19 @@ var io = __webpack_require__(436);
53655365
// CONCATENATED MODULE: ./src/async-function.ts
53665366
const AsyncFunction = Object.getPrototypeOf(async () => null).constructor;
53675367
function callAsyncFunction(args, source) {
5368-
const fn = new AsyncFunction(...Object.keys(args), source);
5369-
return fn(...Object.values(args));
5368+
const previousWorkingDirectory = process.cwd();
5369+
try {
5370+
if (process.env.GITHUB_WORKSPACE !== undefined) {
5371+
process.chdir(process.env.GITHUB_WORKSPACE);
5372+
}
5373+
const fn = new AsyncFunction(...Object.keys(args), source);
5374+
return fn(...Object.values(args));
5375+
}
5376+
finally {
5377+
if (previousWorkingDirectory !== process.cwd()) {
5378+
process.chdir(previousWorkingDirectory);
5379+
}
5380+
}
53705381
}
53715382

53725383
// CONCATENATED MODULE: ./src/main.ts

‎src/async-function.ts

Copy file name to clipboardExpand all lines: src/async-function.ts
+13-2Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ export function callAsyncFunction<T>(
1717
args: AsyncFunctionArguments,
1818
source: string
1919
): Promise<T> {
20-
const fn = new AsyncFunction(...Object.keys(args), source)
21-
return fn(...Object.values(args))
20+
const previousWorkingDirectory = process.cwd()
21+
try {
22+
if (process.env.GITHUB_WORKSPACE !== undefined) {
23+
process.chdir(process.env.GITHUB_WORKSPACE)
24+
}
25+
26+
const fn = new AsyncFunction(...Object.keys(args), source)
27+
return fn(...Object.values(args))
28+
} finally {
29+
if (previousWorkingDirectory !== process.cwd()) {
30+
process.chdir(previousWorkingDirectory)
31+
}
32+
}
2233
}

0 commit comments

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