Skip to content

Commit

Permalink
Merge pull request #37 from Kesin11/prefix_suffix
Browse files Browse the repository at this point in the history
Add prefix and suffix options
  • Loading branch information
Kesin11 authored Dec 26, 2022
2 parents fbaf050 + d168f7c commit 8ce2f2a
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 44 deletions.
32 changes: 29 additions & 3 deletions .github/workflows/actions-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ jobs:
- name: $GITHUB_WORKSPACE.bak is exists
run: test -d ${GITHUB_WORKSPACE}.bak
- name: Created workspace is not symlkink
# Default dir name: ${YAML_NAME}-${JOB_NAME}
# Default dir name: ${workflow-yaml-name}-${job-name}
# But github hosted runner still use v2.299.1 at 2022/12/26, so this test use ${workflow-name}-${job-name}
run: |
test -d $RUNNER_WORKSPACE/Actions_integrate_test-default_name
if [ -L $RUNNER_WORKSPACE/Actions_integrate_test-default_name ]; then
test -d $RUNNER_WORKSPACE/actions_integrate_test-default_name
if [ -L $RUNNER_WORKSPACE/actions_integrate_test-default_name ]; then
exit 1
fi
Expand Down Expand Up @@ -69,3 +70,28 @@ jobs:
- name: Checkout to new workspace for action post process
uses: actions/checkout@v3
if: ${{ always() }}

workspace_name_with_prefix_suffix:
strategy:
matrix:
runner: [ubuntu-latest, macos-latest]
fail-fast: false
runs-on: ${{ matrix.runner }}
steps:
- uses: actions/checkout@v3
- uses: ./
with:
workspace-name: "test-dir"
prefix: "Prefix-"
suffix: "-Suffix"

- name: Created workspace is not symlkink
run: |
test -d $RUNNER_WORKSPACE/prefix-test-dir-suffix
if [ -L $RUNNER_WORKSPACE/prefix-test-dir-suffix ]; then
exit 1
fi
- name: Checkout to new workspace for action post process
uses: actions/checkout@v3
if: ${{ always() }}
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,21 @@ jobs:
# Use before actions/checkout
- uses: Kesin11/setup-job-workspace-action@v1
with:
# You can change workspace name from default: ${workspace-yaml-name}-${job-name}
workspace_name: foo_bar_workspace
# You can change workspace name from default: ${workflow-yaml-name}-${job-name}
workspace-name: foo_bar_workspace
- uses: actions/checkout@v3

# ... your build steps

with_prefix_and_suffix:
runs-on: [self-hosted]
steps:
- uses: Kesin11/setup-job-workspace-action@v1
with:
# You can set prefix and suffix to default workspace name and also `workspace-name`.
# ex: "prefix-${workflow-yaml-name}-${job-name}-suffix"
prefix: "prefix-"
suffix: "-suffix"
- uses: actions/checkout@v3

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

## Notice
### Default workspace name is different for older runner version.
`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}`.

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
. 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).

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.

## How it works
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.

Expand Down
54 changes: 42 additions & 12 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { expect, test, beforeEach, afterEach } from '@jest/globals'
import { createDirName, replaceWorkspace } from '../src/workspace'

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

test('createDirName() returns workspaceName', async () => {
const workspaceName = 'test-dir'
const actual = createDirName(contextMock, workspaceName)
const actual = createDirName(contextMock, workspaceName, "", "")
await expect(actual).toEqual(workspaceName)
})

test('createDirName() returns escaped workspaceName', async () => {
const workspaceName = 'test dir'
const actual = createDirName(contextMock, workspaceName)
const workspaceName = 'Test Dir'
const actual = createDirName(contextMock, workspaceName, "", "")
await expect(actual).toEqual('test_dir')
})

test('createDirName() returns escaped workspaceName with prefix and suffix', async () => {
const workspaceName = 'Test Dir'
const prefix = "Prefix-"
const suffix = "-Suffix"
const actual = createDirName(contextMock, workspaceName, prefix, suffix)
await expect(actual).toEqual("prefix-test_dir-suffix")
})

test('createDirName() returns default name', async () => {
const actual = createDirName(contextMock, "")
const actual = createDirName(contextMock, "", "", "")
await expect(actual).toEqual(`${workflowName}-${jobName}`)
})

test('createDirName() returns escaped default name', async () => {
const jobName = 'test Job'
const jobName = 'Test Job'
const overrideMock = {
...contextMock,
job: jobName
} as unknown as Context

const actual = createDirName(overrideMock, "", "", "")
await expect(actual).toEqual(`${workflowName}-test_job`)
})

test('createDirName() returns escaped default name with prefix and suffix', async () => {
const jobName = 'Test Job'
const overrideMock = {
...contextMock,
job: jobName
} as unknown as Context
const prefix = "Prefix-"
const suffix = "-Suffix"

const actual = createDirName(overrideMock, "")
await expect(actual).toEqual(`${workflowName}-test_Job`)
const actual = createDirName(overrideMock, "", prefix, suffix)
await expect(actual).toEqual(`prefix-${workflowName}-test_job-suffix`)
})

test('replaceWorkspace() with workspaceName', async () => {
const workspaceName = 'test-dir'
await replaceWorkspace(contextMock, workspaceName)
const inputs = {
workspaceName: 'test-dir',
prefix: '',
suffix: '',
}
await replaceWorkspace(contextMock, inputs)

const virtualWorkspacePath = path.join(process.env.RUNNER_WORKSPACE!, workspaceName)
const virtualWorkspacePath = path.join(process.env.RUNNER_WORKSPACE!, inputs.workspaceName)

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

test('replaceWorkspace() with default input', async () => {
await replaceWorkspace(contextMock, "")
const inputs = {
workspaceName: '',
prefix: '',
suffix: '',
}
await replaceWorkspace(contextMock, inputs)

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

Expand Down
9 changes: 7 additions & 2 deletions __tests__/post.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { expect, test, beforeEach, afterEach } from '@jest/globals'
import { replaceWorkspace, restoreWorkspace } from '../src/workspace'

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

// Do main step of own actions before each test
await replaceWorkspace(contextMock, "")
const inputs = {
workspaceName: '',
prefix: '',
suffix: '',
}
await replaceWorkspace(contextMock, inputs)
})

afterEach(async () => {
Expand Down
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ inputs:
workspace-name:
required: false
description: 'Input workspace directory name if you need to change from default: ${workspace-yaml-name}-${job-name}'
prefix:
required: false
description: 'Set prefix for workspace directory name. default: ""'
default: ""
suffix:
required: false
description: 'Set suffix for workspace directory name. default: ""'
default: ""
runs:
using: 'node16'
main: 'dist/main/index.js'
Expand Down
20 changes: 12 additions & 8 deletions dist/main/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/main/index.js.map

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions dist/post/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/post/index.js.map

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import { replaceWorkspace } from './workspace'
import { InputOptions, replaceWorkspace } from './workspace'

async function run (): Promise<void> {
try {
const workspaceName: string = core.getInput('workspace-name')
await replaceWorkspace(github.context, workspaceName)
const inputs: InputOptions = {
workspaceName: core.getInput('workspace-name'),
prefix: core.getInput('prefix'),
suffix: core.getInput('suffix')
}
await replaceWorkspace(github.context, inputs)
} catch (error) {
core.error(JSON.stringify(error))
if (error instanceof Error) core.setFailed(error.message)
Expand Down
17 changes: 11 additions & 6 deletions src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@ import { Context } from '@actions/github/lib/context'
import { getRunnerWorkspacePath, getWorkflowName, getWorkspacePath } from './github_env'

function escapeDirName (rawDirName: string): string {
return rawDirName.trim().replace(/\s/g, '_')
return rawDirName.trim().replace(/\s/g, '_').toLowerCase()
}

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

const workflowName = getWorkflowName()
return escapeDirName(`${workflowName}-${context.job}`)
return escapeDirName(`${prefix}${workflowName}-${context.job}${suffix}`)
}

export async function replaceWorkspace (context: Context, workspaceName: string): Promise<void> {
export interface InputOptions {
workspaceName: string
prefix: string
suffix: string
}
export async function replaceWorkspace (context: Context, inputs: InputOptions): Promise<void> {
// mv ${GITHUB_WORKSPACE} ${GITHUB_WORKSPACE}.bak
const workspacePath = getWorkspacePath()
const workspaceBakPath = workspacePath + '.bak'
Expand All @@ -27,7 +32,7 @@ export async function replaceWorkspace (context: Context, workspaceName: string)
// WORKFLOW_YAML=$(basename "${{ github.event.workflow }}" .yml)
// TMP_DIR="${RUNNER_WORKSPACE}/${WORKFLOW_YAML}-${{ github.job }}"
// mkdir -p ${TMP_DIR}
const virtualWorkspacePath = path.join(getRunnerWorkspacePath(), createDirName(context, workspaceName))
const virtualWorkspacePath = path.join(getRunnerWorkspacePath(), createDirName(context, inputs.workspaceName, inputs.prefix, inputs.suffix))
await io.mkdirP(virtualWorkspacePath)
core.info(`mkdir -p ${virtualWorkspacePath}`)

Expand Down

0 comments on commit 8ce2f2a

Please sign in to comment.