Skip to content

Commit 3842cbe

Browse files
committed
Allow Input and Output from Githubs env files.
1 parent 7ef9ed2 commit 3842cbe

File tree

8 files changed

+158
-23
lines changed

8 files changed

+158
-23
lines changed

.eslintrc.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"@typescript-eslint/no-inferrable-types": "error",
3030
"@typescript-eslint/no-misused-new": "error",
3131
"@typescript-eslint/no-namespace": "error",
32-
"@typescript-eslint/no-non-null-assertion": "warn",
32+
"@typescript-eslint/no-non-null-assertion": "off",
3333
"@typescript-eslint/no-unnecessary-qualifier": "error",
3434
"@typescript-eslint/no-unnecessary-type-assertion": "error",
3535
"@typescript-eslint/no-useless-constructor": "error",
@@ -51,4 +51,4 @@
5151
"es6": true,
5252
"jest/globals": true
5353
}
54-
}
54+
}

action.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ inputs:
2727
description: 'Custom volume mounts'
2828
workdir:
2929
required: false
30-
description: "Custom workdir"
30+
description: 'Custom workdir'
31+
tempdir:
32+
required: false
33+
description: 'Custom tempdir'
34+
default: /tmp
3135
options:
3236
required: false
3337
description: 'Additional run options'

dist/index.js

Lines changed: 65 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "docker-run-action",
3-
"version": "1.2.0",
3+
"version": "2.0.0",
44
"private": true,
55
"description": "Run commands in a docker container.",
66
"main": "lib/main.js",

src/dockerRun.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,27 @@ import * as exec from '@actions/exec'
33
import * as fs from 'fs'
44
import {v4 as uuidv4} from 'uuid'
55
import * as input from './input'
6+
import {FileMap, Mapping} from './fileMap'
67

7-
export const COMMAND_FILE_NAME = `${uuidv4()}.sh`
8-
export const LOCAL_COMMAND_PATH = `${process.env.RUNNER_TEMP}/${COMMAND_FILE_NAME}`
9-
export const CONTAINER_COMMAND_PATH = `/tmp/${COMMAND_FILE_NAME}`
8+
const fileMap = new FileMap(input.get('tempdir'))
9+
10+
fileMap.pushRunnerPath('GITHUB_ENV', process.env.GITHUB_ENV)
11+
fileMap.pushRunnerPath('GITHUB_PATH', process.env.GITHUB_PATH)
12+
fileMap.pushRunnerPath('GITHUB_OUTPUT', process.env.GITHUB_OUTPUT)
13+
fileMap.pushRunnerPath('GITHUB_STATE', process.env.GITHUB_STATE)
14+
fileMap.pushRunnerPath('GITHUB_STEP_SUMMARY', process.env.GITHUB_STEP_SUMMARY)
15+
const command = fileMap.pushRunnerPath(
16+
'CONTAINER_COMMAND',
17+
`${process.env.RUNNER_TEMP}/command_${uuidv4()}`
18+
)
1019

1120
export async function runContainer(): Promise<void> {
12-
fs.writeFileSync(LOCAL_COMMAND_PATH, input.get('run'), {mode: 0o755})
21+
fs.writeFileSync(command!.runner.path, input.get('run'), {mode: 0o755})
1322
core.info(`
14-
Wrote instruction file to "${LOCAL_COMMAND_PATH}"
23+
Wrote instruction file to "${command!.runner.path}"
1524
with these instructions:
1625
----- START OF FILE -----
17-
${fs.readFileSync(LOCAL_COMMAND_PATH).toString()}
26+
${fs.readFileSync(command!.runner.path).toString()}
1827
----- END OF FILE -----`)
1928

2029
await exec.exec('docker', [
@@ -27,14 +36,21 @@ ${fs.readFileSync(LOCAL_COMMAND_PATH).toString()}
2736
...(input.has('workdir') ? [`--workdir=${input.get('workdir')}`] : []),
2837
// environment options
2938
...input.getEnvironment(),
39+
...fileMap.map(
40+
(item: Mapping, key: string): string =>
41+
`--env=${key}=${item.container.path}`
42+
),
3043
// volume options
31-
`--volume=${LOCAL_COMMAND_PATH}:${CONTAINER_COMMAND_PATH}`,
44+
...fileMap.map(
45+
(item: Mapping): string =>
46+
`--volume=${item.runner.path}:${item.container.path}`
47+
),
3248
...input.getVolumes(),
3349
// other options
3450
...input.getSplittet('options'),
3551
input.get('image'),
3652
input.get('shell'),
3753
'-e',
38-
CONTAINER_COMMAND_PATH
54+
command!.container.path
3955
])
4056
}

src/fileMap.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
export interface File {
2+
path: string
3+
file: string
4+
dir: string
5+
}
6+
7+
export interface Mapping {
8+
runner: File
9+
container: File
10+
}
11+
12+
export class FileMap {
13+
items: Map<string, Mapping> = new Map()
14+
containerTempDir: string
15+
16+
constructor(containerTempDir: string) {
17+
this.containerTempDir = containerTempDir.endsWith('/')
18+
? containerTempDir
19+
: `${containerTempDir}/`
20+
}
21+
22+
map<U>(callbackfn: (value: Mapping, key: string) => U): U[] {
23+
return Array.from(this.items.entries()).map(([key, value]) =>
24+
callbackfn(value, key)
25+
)
26+
}
27+
28+
pushRunnerPath(key: string, runnerPath: unknown): Mapping | undefined {
29+
if (typeof runnerPath === 'string' && runnerPath.length > 0) {
30+
const mapping = this.runnerPathToMapping(runnerPath)
31+
this.items.set(key, mapping)
32+
}
33+
34+
return this.items.get(key)
35+
}
36+
37+
protected runnerPathToMapping(runnerPath: string): Mapping {
38+
const runner = this.pathToFile(runnerPath)
39+
40+
const container = {
41+
dir: this.containerTempDir,
42+
file: runner.file,
43+
path: this.containerTempDir + runner.file
44+
}
45+
46+
return {runner, container}
47+
}
48+
49+
protected pathToFile(path: string): File {
50+
const lastSlashIndex = path.lastIndexOf('/')
51+
const dir = path.substring(0, lastSlashIndex)
52+
const file = path.substring(lastSlashIndex + 1)
53+
54+
return {path, dir, file}
55+
}
56+
}

src/input.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ export function getEnvironment(): string[] {
2727
export function getVolumes(): string[] {
2828
return getSplittet('volumes').map((volume: string) => `--volume=${volume}`)
2929
}
30+
31+
export function getTempDir(): string {
32+
return get('tempDir')
33+
}

0 commit comments

Comments
 (0)