Skip to content

Commit 3f1ef27

Browse files
committed
feat(actions): add working-directory
Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
1 parent 5aaefd1 commit 3f1ef27

3 files changed

Lines changed: 37 additions & 14 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Opinionated GitHub Actions and reusable workflows for foundational continuous-in
3636

3737
- [Parse CI reports](actions/parse-ci-reports/README.md) - parses CI reports (tests, linting, coverage) into GitHub summaries and Markdown for PR comments.
3838
- [Repository owner is organization](actions/repository-owner-is-organization/README.md) - checks whether the repository owner is an organization.
39-
- [Working directory](actions/working-directory/README.md) - resolves and validates a working directory path as an absolute path.
39+
- [Working directory](actions/working-directory/README.md) - resolves and validates a working directory path, exposing absolute and workspace-relative outputs.
4040
- [Slugify](actions/slugify/README.md) - converts free-form strings into GitHub-friendly slugs.
4141

4242
## Reusable Workflows

actions/working-directory/README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ Action to resolve and validate a working directory path.
4242
4343
## Inputs
4444
45-
| **Input** | **Description** | **Required** | **Default** |
46-
| ----------------------- | ------------------------------------------------------- | ------------ | ----------- |
47-
| **`working-directory`** | Relative or absolute working directory path to resolve. | **false** | `.` |
45+
| **Input** | **Description** | **Required** | **Default** |
46+
| ------------------------------- | --------------------------------------------------------------------- | ------------ | ----------- |
47+
| **`working-directory`** | Relative or absolute working directory path to resolve. | **false** | `.` |
48+
| **`enforce-path-in-workspace`** | Whether to fail when the resolved path is outside `GITHUB_WORKSPACE`. | **false** | `true` |
4849

4950
<!-- inputs:end -->
5051
<!-- secrets:start -->
@@ -53,9 +54,10 @@ Action to resolve and validate a working directory path.
5354

5455
## Outputs
5556

56-
| **Output** | **Description** |
57-
| ----------------------- | --------------------------------------------- |
58-
| **`working-directory`** | The resolved absolute working directory path. |
57+
| **Output** | **Description** |
58+
| ----------------------------- | ------------------------------------------------------------------- |
59+
| **`absolute-path`** | The resolved absolute working directory path. |
60+
| **`workspace-relative-path`** | The resolved working directory path relative to `GITHUB_WORKSPACE`. |
5961

6062
<!-- outputs:end -->
6163
<!-- examples:start -->

actions/working-directory/action.yml

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@ inputs:
1010
description: "Relative or absolute working directory path to resolve."
1111
required: false
1212
default: "."
13+
enforce-path-in-workspace:
14+
description: "Whether to fail when the resolved path is outside GITHUB_WORKSPACE."
15+
required: false
16+
default: "true"
1317

1418
outputs:
15-
working-directory:
19+
absolute-path:
1620
description: "The resolved absolute working directory path."
17-
value: ${{ steps.working-directory.outputs.working-directory }}
21+
value: ${{ steps.working-directory.outputs.absolute-path }}
22+
workspace-relative-path:
23+
description: "The resolved working directory path relative to GITHUB_WORKSPACE."
24+
value: ${{ steps.working-directory.outputs.workspace-relative-path }}
1825

1926
runs:
2027
using: "composite"
@@ -23,22 +30,36 @@ runs:
2330
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
2431
env:
2532
WORKING_DIRECTORY_INPUT: ${{ inputs.working-directory }}
33+
ENFORCE_PATH_IN_WORKSPACE_INPUT: ${{ inputs.enforce-path-in-workspace }}
2634
with:
2735
script: |
2836
const fs = require('node:fs');
2937
const path = require('node:path');
38+
const workspaceRoot = process.env.GITHUB_WORKSPACE || process.cwd();
39+
const enforcePathInWorkspace = (process.env.ENFORCE_PATH_IN_WORKSPACE_INPUT || 'true') !== 'false';
3040
31-
let workingDirectory = process.env.WORKING_DIRECTORY_INPUT || '.';
41+
const workingDirectory = process.env.WORKING_DIRECTORY_INPUT || '.';
3242
3343
if (!path.isAbsolute(workingDirectory)) {
34-
workingDirectory = path.join(process.env.GITHUB_WORKSPACE, workingDirectory);
44+
workingDirectory = path.join(workspaceRoot, workingDirectory);
3545
}
3646
3747
if (!fs.existsSync(workingDirectory)) {
3848
core.setFailed(`The specified working directory does not exist: ${workingDirectory}`);
3949
return;
4050
}
4151
42-
workingDirectory = path.resolve(workingDirectory);
43-
core.debug(`Running in working directory: ${workingDirectory}`);
44-
core.setOutput('working-directory', workingDirectory);
52+
const absolutePath = path.resolve(workingDirectory);
53+
const workspaceRelativePath = path.relative(workspaceRoot, absolutePath) || '.';
54+
55+
if (enforcePathInWorkspace) {
56+
const isOutsideWorkspace = workspaceRelativePath.startsWith('..') || path.isAbsolute(workspaceRelativePath);
57+
58+
if (isOutsideWorkspace) {
59+
core.setFailed(`The specified working directory is outside GITHUB_WORKSPACE: ${absolutePath}`);
60+
return;
61+
}
62+
}
63+
64+
core.setOutput('absolute-path', absolutePath);
65+
core.setOutput('workspace-relative-path', workspaceRelativePath);

0 commit comments

Comments
 (0)