Skip to content

Commit f3721f0

Browse files
committed
2 parents b3ac081 + 8461fff commit f3721f0

File tree

6 files changed

+52
-24
lines changed

6 files changed

+52
-24
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/
7171
# Default: true
7272
persist-credentials: ''
7373

74-
# Relative path under $GITHUB_WORKSPACE to place the repository
74+
# Relative path under $GITHUB_WORKSPACE/working-directory to place the repository
7575
path: ''
7676

7777
# Whether to execute `git clean -ffdx && git reset --hard HEAD` before fetching
@@ -130,6 +130,10 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/
130130
# running from unless specified. Example URLs are https://github.com or
131131
# https://my-ghes-server.example.com
132132
github-server-url: ''
133+
134+
# Provide the working directory for the git commands to execute into, defaults to
135+
# $GITHUB_WORKSPACE
136+
working-directory: ''
133137
```
134138
<!-- end usage -->
135139

__test__/git-auth-helper.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ async function setup(testName: string): Promise<void> {
825825
sshUser: '',
826826
workflowOrganizationId: 123456,
827827
setSafeDirectory: true,
828-
githubServerUrl: githubServerUrl
828+
githubServerUrl: githubServerUrl,
829829
}
830830
}
831831

__test__/input-helper.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,25 @@ describe('input-helper tests', () => {
145145
const settings: IGitSourceSettings = await inputHelper.getInputs()
146146
expect(settings.workflowOrganizationId).toBe(123456)
147147
})
148+
149+
it('sets a different working directory', async() => {
150+
inputs['working-directory'] = '/home/user/test'
151+
inputs['path'] = 'path/to/repo'
152+
const settings: IGitSourceSettings = await inputHelper.getInputs()
153+
expect(settings.repositoryPath).toBe(path.resolve('/home/user/test/path/to/repo'))
154+
})
155+
156+
it('sets a working directory on root', async() => {
157+
inputs['working-directory'] = '/'
158+
inputs['path'] = 'path/to/repo'
159+
const settings: IGitSourceSettings = await inputHelper.getInputs()
160+
expect(settings.repositoryPath).toBe(path.resolve('/path/to/repo'))
161+
})
162+
163+
it('sets a working directory on root and repository path is set to empty', async() => {
164+
inputs['working-directory'] = '/'
165+
inputs['path'] = ''
166+
const settings: IGitSourceSettings = await inputHelper.getInputs()
167+
expect(settings.repositoryPath).toBe(path.resolve('/'))
168+
})
148169
})

action.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ inputs:
5353
description: 'Whether to configure the token or SSH key with the local git config'
5454
default: true
5555
path:
56-
description: 'Relative path under $GITHUB_WORKSPACE to place the repository'
56+
description: 'Relative path under $GITHUB_WORKSPACE/working-directory to place the repository'
5757
clean:
5858
description: 'Whether to execute `git clean -ffdx && git reset --hard HEAD` before fetching'
5959
default: true
@@ -101,6 +101,9 @@ inputs:
101101
github-server-url:
102102
description: The base URL for the GitHub instance that you are trying to clone from, will use environment defaults to fetch from the same instance that the workflow is running from unless specified. Example URLs are https://github.com or https://my-ghes-server.example.com
103103
required: false
104+
working-directory:
105+
description: Provide the working directory for the git commands to execute into, defaults to $GITHUB_WORKSPACE
106+
required: false
104107
runs:
105108
using: node20
106109
main: dist/index.js

dist/index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,14 +1724,14 @@ const workflowContextHelper = __importStar(__nccwpck_require__(9568));
17241724
function getInputs() {
17251725
return __awaiter(this, void 0, void 0, function* () {
17261726
const result = {};
1727-
// GitHub workspace
1728-
let githubWorkspacePath = process.env['GITHUB_WORKSPACE'];
1729-
if (!githubWorkspacePath) {
1730-
throw new Error('GITHUB_WORKSPACE not defined');
1731-
}
1732-
githubWorkspacePath = path.resolve(githubWorkspacePath);
1733-
core.debug(`GITHUB_WORKSPACE = '${githubWorkspacePath}'`);
1734-
fsHelper.directoryExistsSync(githubWorkspacePath, true);
1727+
// Working directory
1728+
let workingDirectory = core.getInput('workingDirectory') || process.env['GITHUB_WORKSPACE'];
1729+
if (!workingDirectory) {
1730+
throw new Error('working dir not defined');
1731+
}
1732+
workingDirectory = path.resolve(workingDirectory);
1733+
core.debug(`working directory = '${workingDirectory}'`);
1734+
fsHelper.directoryExistsSync(workingDirectory, true);
17351735
// Qualified repository
17361736
const qualifiedRepository = core.getInput('repository') ||
17371737
`${github.context.repo.owner}/${github.context.repo.repo}`;
@@ -1746,9 +1746,9 @@ function getInputs() {
17461746
result.repositoryName = splitRepository[1];
17471747
// Repository path
17481748
result.repositoryPath = core.getInput('path') || '.';
1749-
result.repositoryPath = path.resolve(githubWorkspacePath, result.repositoryPath);
1750-
if (!(result.repositoryPath + path.sep).startsWith(githubWorkspacePath + path.sep)) {
1751-
throw new Error(`Repository path '${result.repositoryPath}' is not under '${githubWorkspacePath}'`);
1749+
result.repositoryPath = path.resolve(workingDirectory, result.repositoryPath);
1750+
if (!(result.repositoryPath + path.sep).startsWith(workingDirectory + path.sep)) {
1751+
throw new Error(`Repository path '${result.repositoryPath}' is not under '${workingDirectory}'`);
17521752
}
17531753
// Workflow repository?
17541754
const isWorkflowRepository = qualifiedRepository.toUpperCase() ===

src/input-helper.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import {IGitSourceSettings} from './git-source-settings'
88
export async function getInputs(): Promise<IGitSourceSettings> {
99
const result = {} as unknown as IGitSourceSettings
1010

11-
// GitHub workspace
12-
let githubWorkspacePath = process.env['GITHUB_WORKSPACE']
13-
if (!githubWorkspacePath) {
14-
throw new Error('GITHUB_WORKSPACE not defined')
11+
// Working directory
12+
let workingDirectory = core.getInput('working-directory') || process.env['GITHUB_WORKSPACE']
13+
if (!workingDirectory) {
14+
throw new Error('working dir not defined')
1515
}
16-
githubWorkspacePath = path.resolve(githubWorkspacePath)
17-
core.debug(`GITHUB_WORKSPACE = '${githubWorkspacePath}'`)
18-
fsHelper.directoryExistsSync(githubWorkspacePath, true)
16+
workingDirectory = path.resolve(workingDirectory)
17+
core.debug(`working directory = '${workingDirectory}'`)
18+
fsHelper.directoryExistsSync(workingDirectory, true)
1919

2020
// Qualified repository
2121
const qualifiedRepository =
@@ -38,16 +38,16 @@ export async function getInputs(): Promise<IGitSourceSettings> {
3838
// Repository path
3939
result.repositoryPath = core.getInput('path') || '.'
4040
result.repositoryPath = path.resolve(
41-
githubWorkspacePath,
41+
workingDirectory,
4242
result.repositoryPath
4343
)
4444
if (
4545
!(result.repositoryPath + path.sep).startsWith(
46-
githubWorkspacePath + path.sep
46+
workingDirectory
4747
)
4848
) {
4949
throw new Error(
50-
`Repository path '${result.repositoryPath}' is not under '${githubWorkspacePath}'`
50+
`Repository path '${result.repositoryPath + path.sep}' is not under '${workingDirectory}'`
5151
)
5252
}
5353

0 commit comments

Comments
 (0)