Skip to content

Commit

Permalink
Allow Input and Output from Githubs env files.
Browse files Browse the repository at this point in the history
  • Loading branch information
kohlerdominik committed Feb 27, 2024
1 parent 7ef9ed2 commit 3842cbe
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@typescript-eslint/no-inferrable-types": "error",
"@typescript-eslint/no-misused-new": "error",
"@typescript-eslint/no-namespace": "error",
"@typescript-eslint/no-non-null-assertion": "warn",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-unnecessary-qualifier": "error",
"@typescript-eslint/no-unnecessary-type-assertion": "error",
"@typescript-eslint/no-useless-constructor": "error",
Expand All @@ -51,4 +51,4 @@
"es6": true,
"jest/globals": true
}
}
}
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ inputs:
description: 'Custom volume mounts'
workdir:
required: false
description: "Custom workdir"
description: 'Custom workdir'
tempdir:
required: false
description: 'Custom tempdir'
default: /tmp
options:
required: false
description: 'Additional run options'
Expand Down
75 changes: 65 additions & 10 deletions dist/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/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "docker-run-action",
"version": "1.2.0",
"version": "2.0.0",
"private": true,
"description": "Run commands in a docker container.",
"main": "lib/main.js",
Expand Down
32 changes: 24 additions & 8 deletions src/dockerRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@ import * as exec from '@actions/exec'
import * as fs from 'fs'
import {v4 as uuidv4} from 'uuid'
import * as input from './input'
import {FileMap, Mapping} from './fileMap'

export const COMMAND_FILE_NAME = `${uuidv4()}.sh`
export const LOCAL_COMMAND_PATH = `${process.env.RUNNER_TEMP}/${COMMAND_FILE_NAME}`
export const CONTAINER_COMMAND_PATH = `/tmp/${COMMAND_FILE_NAME}`
const fileMap = new FileMap(input.get('tempdir'))

fileMap.pushRunnerPath('GITHUB_ENV', process.env.GITHUB_ENV)
fileMap.pushRunnerPath('GITHUB_PATH', process.env.GITHUB_PATH)
fileMap.pushRunnerPath('GITHUB_OUTPUT', process.env.GITHUB_OUTPUT)
fileMap.pushRunnerPath('GITHUB_STATE', process.env.GITHUB_STATE)
fileMap.pushRunnerPath('GITHUB_STEP_SUMMARY', process.env.GITHUB_STEP_SUMMARY)
const command = fileMap.pushRunnerPath(
'CONTAINER_COMMAND',
`${process.env.RUNNER_TEMP}/command_${uuidv4()}`
)

export async function runContainer(): Promise<void> {
fs.writeFileSync(LOCAL_COMMAND_PATH, input.get('run'), {mode: 0o755})
fs.writeFileSync(command!.runner.path, input.get('run'), {mode: 0o755})
core.info(`
Wrote instruction file to "${LOCAL_COMMAND_PATH}"
Wrote instruction file to "${command!.runner.path}"
with these instructions:
----- START OF FILE -----
${fs.readFileSync(LOCAL_COMMAND_PATH).toString()}
${fs.readFileSync(command!.runner.path).toString()}
----- END OF FILE -----`)

await exec.exec('docker', [
Expand All @@ -27,14 +36,21 @@ ${fs.readFileSync(LOCAL_COMMAND_PATH).toString()}
...(input.has('workdir') ? [`--workdir=${input.get('workdir')}`] : []),
// environment options
...input.getEnvironment(),
...fileMap.map(
(item: Mapping, key: string): string =>
`--env=${key}=${item.container.path}`
),
// volume options
`--volume=${LOCAL_COMMAND_PATH}:${CONTAINER_COMMAND_PATH}`,
...fileMap.map(
(item: Mapping): string =>
`--volume=${item.runner.path}:${item.container.path}`
),
...input.getVolumes(),
// other options
...input.getSplittet('options'),
input.get('image'),
input.get('shell'),
'-e',
CONTAINER_COMMAND_PATH
command!.container.path
])
}
56 changes: 56 additions & 0 deletions src/fileMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
export interface File {
path: string
file: string
dir: string
}

export interface Mapping {
runner: File
container: File
}

export class FileMap {
items: Map<string, Mapping> = new Map()
containerTempDir: string

constructor(containerTempDir: string) {
this.containerTempDir = containerTempDir.endsWith('/')
? containerTempDir
: `${containerTempDir}/`
}

map<U>(callbackfn: (value: Mapping, key: string) => U): U[] {
return Array.from(this.items.entries()).map(([key, value]) =>
callbackfn(value, key)
)
}

pushRunnerPath(key: string, runnerPath: unknown): Mapping | undefined {
if (typeof runnerPath === 'string' && runnerPath.length > 0) {
const mapping = this.runnerPathToMapping(runnerPath)
this.items.set(key, mapping)
}

return this.items.get(key)
}

protected runnerPathToMapping(runnerPath: string): Mapping {
const runner = this.pathToFile(runnerPath)

const container = {
dir: this.containerTempDir,
file: runner.file,
path: this.containerTempDir + runner.file
}

return {runner, container}
}

protected pathToFile(path: string): File {
const lastSlashIndex = path.lastIndexOf('/')
const dir = path.substring(0, lastSlashIndex)
const file = path.substring(lastSlashIndex + 1)

return {path, dir, file}
}
}
4 changes: 4 additions & 0 deletions src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ export function getEnvironment(): string[] {
export function getVolumes(): string[] {
return getSplittet('volumes').map((volume: string) => `--volume=${volume}`)
}

export function getTempDir(): string {
return get('tempDir')
}

0 comments on commit 3842cbe

Please sign in to comment.