Skip to content

Commit 8ce2f2a

Browse files
authored
Merge pull request #37 from Kesin11/prefix_suffix
Add prefix and suffix options
2 parents fbaf050 + d168f7c commit 8ce2f2a

File tree

11 files changed

+148
-44
lines changed

11 files changed

+148
-44
lines changed

.github/workflows/actions-test.yml

+29-3
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ jobs:
2727
- name: $GITHUB_WORKSPACE.bak is exists
2828
run: test -d ${GITHUB_WORKSPACE}.bak
2929
- name: Created workspace is not symlkink
30-
# Default dir name: ${YAML_NAME}-${JOB_NAME}
30+
# Default dir name: ${workflow-yaml-name}-${job-name}
31+
# But github hosted runner still use v2.299.1 at 2022/12/26, so this test use ${workflow-name}-${job-name}
3132
run: |
32-
test -d $RUNNER_WORKSPACE/Actions_integrate_test-default_name
33-
if [ -L $RUNNER_WORKSPACE/Actions_integrate_test-default_name ]; then
33+
test -d $RUNNER_WORKSPACE/actions_integrate_test-default_name
34+
if [ -L $RUNNER_WORKSPACE/actions_integrate_test-default_name ]; then
3435
exit 1
3536
fi
3637
@@ -69,3 +70,28 @@ jobs:
6970
- name: Checkout to new workspace for action post process
7071
uses: actions/checkout@v3
7172
if: ${{ always() }}
73+
74+
workspace_name_with_prefix_suffix:
75+
strategy:
76+
matrix:
77+
runner: [ubuntu-latest, macos-latest]
78+
fail-fast: false
79+
runs-on: ${{ matrix.runner }}
80+
steps:
81+
- uses: actions/checkout@v3
82+
- uses: ./
83+
with:
84+
workspace-name: "test-dir"
85+
prefix: "Prefix-"
86+
suffix: "-Suffix"
87+
88+
- name: Created workspace is not symlkink
89+
run: |
90+
test -d $RUNNER_WORKSPACE/prefix-test-dir-suffix
91+
if [ -L $RUNNER_WORKSPACE/prefix-test-dir-suffix ]; then
92+
exit 1
93+
fi
94+
95+
- name: Checkout to new workspace for action post process
96+
uses: actions/checkout@v3
97+
if: ${{ always() }}

README.md

+24-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,21 @@ jobs:
2222
# Use before actions/checkout
2323
- uses: Kesin11/setup-job-workspace-action@v1
2424
with:
25-
# You can change workspace name from default: ${workspace-yaml-name}-${job-name}
26-
workspace_name: foo_bar_workspace
25+
# You can change workspace name from default: ${workflow-yaml-name}-${job-name}
26+
workspace-name: foo_bar_workspace
27+
- uses: actions/checkout@v3
28+
29+
# ... your build steps
30+
31+
with_prefix_and_suffix:
32+
runs-on: [self-hosted]
33+
steps:
34+
- uses: Kesin11/setup-job-workspace-action@v1
35+
with:
36+
# You can set prefix and suffix to default workspace name and also `workspace-name`.
37+
# ex: "prefix-${workflow-yaml-name}-${job-name}-suffix"
38+
prefix: "prefix-"
39+
suffix: "-suffix"
2740
- uses: actions/checkout@v3
2841

2942
# ... your build steps
@@ -32,6 +45,15 @@ jobs:
3245
### Options
3346
See [action.yml](./action.yml)
3447

48+
## Notice
49+
### Default workspace name is different for older runner version.
50+
`workspace-name` default is `${workflow-yaml-name}-${job-name}`, but when self-hosted runner version is older than [actions/[email protected]](https://github.com/actions/runner/releases/tag/v2.300.0) defalut is `${workflow-name}-${job-name}`.
51+
52+
This defference comes from technical reason that how to get workflow yaml name. First, try to get yaml name from `GITHUB_WORKFLOW_REF` environment variable that exposed from runner version v2.330.0
53+
. When this action detect runs on older runner case that like using GHES, this actions fallback to use `GITHUB_WORKFLOW` for create default `workspace-name`. `GITHUB_WORKFLOW` is equal to [workflow `name`](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#name).
54+
55+
If you want to keep the same workspace name between different versions of the runner or for future version upgrades, specify the `workspace-name` option explicitly.
56+
3557
## How it works
3658
GitHub Actions runner only has one workspace directory per repository ($GITHUB_WORKSPACE). That path is defined by the repository name, for example the workspace path of this repository is `/home/runner/work/setup-job-workspace-action/setup-job-workspace-action` in GitHub hosted Ubuntu runner.
3759

__tests__/main.test.ts

+42-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { expect, test, beforeEach, afterEach } from '@jest/globals'
66
import { createDirName, replaceWorkspace } from '../src/workspace'
77

88
const workflowName = 'test'
9-
const jobName = 'testJob'
9+
const jobName = 'testjob'
1010
const githubWorkflow = "Test workflow"
1111
const githubWorkflowRef = `"Kesin11/setup-job-workspace-action/.github/workflows/${workflowName}.yml@refs/heads/test_branch`
1212
const contextMock = {
@@ -36,37 +36,62 @@ afterEach(async () => {
3636

3737
test('createDirName() returns workspaceName', async () => {
3838
const workspaceName = 'test-dir'
39-
const actual = createDirName(contextMock, workspaceName)
39+
const actual = createDirName(contextMock, workspaceName, "", "")
4040
await expect(actual).toEqual(workspaceName)
4141
})
4242

4343
test('createDirName() returns escaped workspaceName', async () => {
44-
const workspaceName = 'test dir'
45-
const actual = createDirName(contextMock, workspaceName)
44+
const workspaceName = 'Test Dir'
45+
const actual = createDirName(contextMock, workspaceName, "", "")
4646
await expect(actual).toEqual('test_dir')
4747
})
4848

49+
test('createDirName() returns escaped workspaceName with prefix and suffix', async () => {
50+
const workspaceName = 'Test Dir'
51+
const prefix = "Prefix-"
52+
const suffix = "-Suffix"
53+
const actual = createDirName(contextMock, workspaceName, prefix, suffix)
54+
await expect(actual).toEqual("prefix-test_dir-suffix")
55+
})
56+
4957
test('createDirName() returns default name', async () => {
50-
const actual = createDirName(contextMock, "")
58+
const actual = createDirName(contextMock, "", "", "")
5159
await expect(actual).toEqual(`${workflowName}-${jobName}`)
5260
})
5361

5462
test('createDirName() returns escaped default name', async () => {
55-
const jobName = 'test Job'
63+
const jobName = 'Test Job'
64+
const overrideMock = {
65+
...contextMock,
66+
job: jobName
67+
} as unknown as Context
68+
69+
const actual = createDirName(overrideMock, "", "", "")
70+
await expect(actual).toEqual(`${workflowName}-test_job`)
71+
})
72+
73+
test('createDirName() returns escaped default name with prefix and suffix', async () => {
74+
const jobName = 'Test Job'
5675
const overrideMock = {
5776
...contextMock,
5877
job: jobName
5978
} as unknown as Context
79+
const prefix = "Prefix-"
80+
const suffix = "-Suffix"
6081

61-
const actual = createDirName(overrideMock, "")
62-
await expect(actual).toEqual(`${workflowName}-test_Job`)
82+
const actual = createDirName(overrideMock, "", prefix, suffix)
83+
await expect(actual).toEqual(`prefix-${workflowName}-test_job-suffix`)
6384
})
6485

6586
test('replaceWorkspace() with workspaceName', async () => {
66-
const workspaceName = 'test-dir'
67-
await replaceWorkspace(contextMock, workspaceName)
87+
const inputs = {
88+
workspaceName: 'test-dir',
89+
prefix: '',
90+
suffix: '',
91+
}
92+
await replaceWorkspace(contextMock, inputs)
6893

69-
const virtualWorkspacePath = path.join(process.env.RUNNER_WORKSPACE!, workspaceName)
94+
const virtualWorkspacePath = path.join(process.env.RUNNER_WORKSPACE!, inputs.workspaceName)
7095

7196
// /$RUNNER_WORKSPACE/{workspaceName}/ is exists
7297
expect(fs.accessSync(virtualWorkspacePath)).toBeUndefined()
@@ -77,7 +102,12 @@ test('replaceWorkspace() with workspaceName', async () => {
77102
})
78103

79104
test('replaceWorkspace() with default input', async () => {
80-
await replaceWorkspace(contextMock, "")
105+
const inputs = {
106+
workspaceName: '',
107+
prefix: '',
108+
suffix: '',
109+
}
110+
await replaceWorkspace(contextMock, inputs)
81111

82112
const virtualWorkspacePath = path.join(process.env.RUNNER_WORKSPACE!, `${workflowName}-${jobName}`)
83113

__tests__/post.test.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { expect, test, beforeEach, afterEach } from '@jest/globals'
66
import { replaceWorkspace, restoreWorkspace } from '../src/workspace'
77

88
const workflowName = 'test'
9-
const jobName = 'testJob'
9+
const jobName = 'testjob'
1010
const githubWorkflow = "Test workflow"
1111
const githubWorkflowRef = `"Kesin11/setup-job-workspace-action/.github/workflows/${workflowName}.yml@refs/heads/test_branch`
1212
const contextMock = {
@@ -29,7 +29,12 @@ beforeEach(async () => {
2929
await fs.promises.mkdir(process.env.GITHUB_WORKSPACE!, { recursive: true })
3030

3131
// Do main step of own actions before each test
32-
await replaceWorkspace(contextMock, "")
32+
const inputs = {
33+
workspaceName: '',
34+
prefix: '',
35+
suffix: '',
36+
}
37+
await replaceWorkspace(contextMock, inputs)
3338
})
3439

3540
afterEach(async () => {

action.yml

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ inputs:
55
workspace-name:
66
required: false
77
description: 'Input workspace directory name if you need to change from default: ${workspace-yaml-name}-${job-name}'
8+
prefix:
9+
required: false
10+
description: 'Set prefix for workspace directory name. default: ""'
11+
default: ""
12+
suffix:
13+
required: false
14+
description: 'Set suffix for workspace directory name. default: ""'
15+
default: ""
816
runs:
917
using: 'node16'
1018
main: 'dist/main/index.js'

dist/main/index.js

+12-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/main/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/post/index.js

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/post/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import * as core from '@actions/core'
22
import * as github from '@actions/github'
3-
import { replaceWorkspace } from './workspace'
3+
import { InputOptions, replaceWorkspace } from './workspace'
44

55
async function run (): Promise<void> {
66
try {
7-
const workspaceName: string = core.getInput('workspace-name')
8-
await replaceWorkspace(github.context, workspaceName)
7+
const inputs: InputOptions = {
8+
workspaceName: core.getInput('workspace-name'),
9+
prefix: core.getInput('prefix'),
10+
suffix: core.getInput('suffix')
11+
}
12+
await replaceWorkspace(github.context, inputs)
913
} catch (error) {
1014
core.error(JSON.stringify(error))
1115
if (error instanceof Error) core.setFailed(error.message)

src/workspace.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,23 @@ import { Context } from '@actions/github/lib/context'
66
import { getRunnerWorkspacePath, getWorkflowName, getWorkspacePath } from './github_env'
77

88
function escapeDirName (rawDirName: string): string {
9-
return rawDirName.trim().replace(/\s/g, '_')
9+
return rawDirName.trim().replace(/\s/g, '_').toLowerCase()
1010
}
1111

12-
export function createDirName (context: Context, workspaceName: string): string {
12+
export function createDirName (context: Context, workspaceName: string, prefix: string, suffix: string): string {
1313
core.debug(`workspaceName: ${workspaceName}`)
14-
if (workspaceName !== '') return escapeDirName(workspaceName)
14+
if (workspaceName !== '') return escapeDirName(`${prefix}${workspaceName}${suffix}`)
1515

1616
const workflowName = getWorkflowName()
17-
return escapeDirName(`${workflowName}-${context.job}`)
17+
return escapeDirName(`${prefix}${workflowName}-${context.job}${suffix}`)
1818
}
1919

20-
export async function replaceWorkspace (context: Context, workspaceName: string): Promise<void> {
20+
export interface InputOptions {
21+
workspaceName: string
22+
prefix: string
23+
suffix: string
24+
}
25+
export async function replaceWorkspace (context: Context, inputs: InputOptions): Promise<void> {
2126
// mv ${GITHUB_WORKSPACE} ${GITHUB_WORKSPACE}.bak
2227
const workspacePath = getWorkspacePath()
2328
const workspaceBakPath = workspacePath + '.bak'
@@ -27,7 +32,7 @@ export async function replaceWorkspace (context: Context, workspaceName: string)
2732
// WORKFLOW_YAML=$(basename "${{ github.event.workflow }}" .yml)
2833
// TMP_DIR="${RUNNER_WORKSPACE}/${WORKFLOW_YAML}-${{ github.job }}"
2934
// mkdir -p ${TMP_DIR}
30-
const virtualWorkspacePath = path.join(getRunnerWorkspacePath(), createDirName(context, workspaceName))
35+
const virtualWorkspacePath = path.join(getRunnerWorkspacePath(), createDirName(context, inputs.workspaceName, inputs.prefix, inputs.suffix))
3136
await io.mkdirP(virtualWorkspacePath)
3237
core.info(`mkdir -p ${virtualWorkspacePath}`)
3338

0 commit comments

Comments
 (0)