Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to customize working directory #1493

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -71,7 +71,7 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/
# Default: true
persist-credentials: ''

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

# Whether to execute `git clean -ffdx && git reset --hard HEAD` before fetching
Expand Down Expand Up @@ -126,6 +126,10 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/
# running from unless specified. Example URLs are https://github.com or
# https://my-ghes-server.example.com
github-server-url: ''

# Provide the working directory for the git commands to execute into, defaults to
# $GITHUB_WORKSPACE
working-directory: ''
```
<!-- end usage -->

Expand Down
2 changes: 1 addition & 1 deletion __test__/git-auth-helper.test.ts
Expand Up @@ -824,7 +824,7 @@ async function setup(testName: string): Promise<void> {
sshUser: '',
workflowOrganizationId: 123456,
setSafeDirectory: true,
githubServerUrl: githubServerUrl
githubServerUrl: githubServerUrl,
}
}

Expand Down
21 changes: 21 additions & 0 deletions __test__/input-helper.test.ts
Expand Up @@ -144,4 +144,25 @@ describe('input-helper tests', () => {
const settings: IGitSourceSettings = await inputHelper.getInputs()
expect(settings.workflowOrganizationId).toBe(123456)
})

it('sets a different working directory', async() => {
inputs['working-directory'] = '/home/user/test'
inputs['path'] = 'path/to/repo'
const settings: IGitSourceSettings = await inputHelper.getInputs()
expect(settings.repositoryPath).toBe(path.resolve('/home/user/test/path/to/repo'))
})

it('sets a working directory on root', async() => {
inputs['working-directory'] = '/'
inputs['path'] = 'path/to/repo'
const settings: IGitSourceSettings = await inputHelper.getInputs()
expect(settings.repositoryPath).toBe(path.resolve('/path/to/repo'))
})

it('sets a working directory on root and repository path is set to empty', async() => {
inputs['working-directory'] = '/'
inputs['path'] = ''
const settings: IGitSourceSettings = await inputHelper.getInputs()
expect(settings.repositoryPath).toBe(path.resolve('/'))
})
})
5 changes: 4 additions & 1 deletion action.yml
Expand Up @@ -53,7 +53,7 @@ inputs:
description: 'Whether to configure the token or SSH key with the local git config'
default: true
path:
description: 'Relative path under $GITHUB_WORKSPACE to place the repository'
description: 'Relative path under $GITHUB_WORKSPACE/working-directory to place the repository'
clean:
description: 'Whether to execute `git clean -ffdx && git reset --hard HEAD` before fetching'
default: true
Expand Down Expand Up @@ -98,6 +98,9 @@ inputs:
github-server-url:
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
required: false
working-directory:
description: Provide the working directory for the git commands to execute into, defaults to $GITHUB_WORKSPACE
required: false
runs:
using: node20
main: dist/index.js
Expand Down
22 changes: 11 additions & 11 deletions dist/index.js
Expand Up @@ -1716,14 +1716,14 @@ const workflowContextHelper = __importStar(__nccwpck_require__(9568));
function getInputs() {
return __awaiter(this, void 0, void 0, function* () {
const result = {};
// GitHub workspace
let githubWorkspacePath = process.env['GITHUB_WORKSPACE'];
if (!githubWorkspacePath) {
throw new Error('GITHUB_WORKSPACE not defined');
}
githubWorkspacePath = path.resolve(githubWorkspacePath);
core.debug(`GITHUB_WORKSPACE = '${githubWorkspacePath}'`);
fsHelper.directoryExistsSync(githubWorkspacePath, true);
// Working directory
let workingDirectory = core.getInput('workingDirectory') || process.env['GITHUB_WORKSPACE'];
if (!workingDirectory) {
throw new Error('working dir not defined');
}
workingDirectory = path.resolve(workingDirectory);
core.debug(`working directory = '${workingDirectory}'`);
fsHelper.directoryExistsSync(workingDirectory, true);
// Qualified repository
const qualifiedRepository = core.getInput('repository') ||
`${github.context.repo.owner}/${github.context.repo.repo}`;
Expand All @@ -1738,9 +1738,9 @@ function getInputs() {
result.repositoryName = splitRepository[1];
// Repository path
result.repositoryPath = core.getInput('path') || '.';
result.repositoryPath = path.resolve(githubWorkspacePath, result.repositoryPath);
if (!(result.repositoryPath + path.sep).startsWith(githubWorkspacePath + path.sep)) {
throw new Error(`Repository path '${result.repositoryPath}' is not under '${githubWorkspacePath}'`);
result.repositoryPath = path.resolve(workingDirectory, result.repositoryPath);
if (!(result.repositoryPath + path.sep).startsWith(workingDirectory + path.sep)) {
throw new Error(`Repository path '${result.repositoryPath}' is not under '${workingDirectory}'`);
}
// Workflow repository?
const isWorkflowRepository = qualifiedRepository.toUpperCase() ===
Expand Down
20 changes: 10 additions & 10 deletions src/input-helper.ts
Expand Up @@ -8,14 +8,14 @@ import {IGitSourceSettings} from './git-source-settings'
export async function getInputs(): Promise<IGitSourceSettings> {
const result = {} as unknown as IGitSourceSettings

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

// Qualified repository
const qualifiedRepository =
Expand All @@ -38,16 +38,16 @@ export async function getInputs(): Promise<IGitSourceSettings> {
// Repository path
result.repositoryPath = core.getInput('path') || '.'
result.repositoryPath = path.resolve(
githubWorkspacePath,
workingDirectory,
result.repositoryPath
)
if (
!(result.repositoryPath + path.sep).startsWith(
githubWorkspacePath + path.sep
workingDirectory
)
) {
throw new Error(
`Repository path '${result.repositoryPath}' is not under '${githubWorkspacePath}'`
`Repository path '${result.repositoryPath + path.sep}' is not under '${workingDirectory}'`
)
}

Expand Down