mirror of
https://github.com/actions/runner-container-hooks.git
synced 2025-12-15 09:16:44 +00:00
exposing env variables from runner with DOCKER_ envs to respect docker options set on host (#40)
* exposing env variables from runner with DOCKER_ prefix to respect rootless docker * Prioritize DOCKER cli over workflow envs * formatted
This commit is contained in:
@@ -427,6 +427,9 @@ export async function containerRun(
|
|||||||
dockerArgs.push(args.image)
|
dockerArgs.push(args.image)
|
||||||
if (args.entryPointArgs) {
|
if (args.entryPointArgs) {
|
||||||
for (const entryPointArg of args.entryPointArgs) {
|
for (const entryPointArg of args.entryPointArgs) {
|
||||||
|
if (!entryPointArg) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
dockerArgs.push(entryPointArg)
|
dockerArgs.push(entryPointArg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ export async function runDockerCommand(
|
|||||||
args: string[],
|
args: string[],
|
||||||
options?: RunDockerCommandOptions
|
options?: RunDockerCommandOptions
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
|
options = optionsWithDockerEnvs(options)
|
||||||
const pipes = await exec.getExecOutput('docker', args, options)
|
const pipes = await exec.getExecOutput('docker', args, options)
|
||||||
if (pipes.exitCode !== 0) {
|
if (pipes.exitCode !== 0) {
|
||||||
core.error(`Docker failed with exit code ${pipes.exitCode}`)
|
core.error(`Docker failed with exit code ${pipes.exitCode}`)
|
||||||
@@ -24,6 +25,45 @@ export async function runDockerCommand(
|
|||||||
return Promise.resolve(pipes.stdout)
|
return Promise.resolve(pipes.stdout)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function optionsWithDockerEnvs(
|
||||||
|
options?: RunDockerCommandOptions
|
||||||
|
): RunDockerCommandOptions | undefined {
|
||||||
|
// From https://docs.docker.com/engine/reference/commandline/cli/#environment-variables
|
||||||
|
const dockerCliEnvs = new Set([
|
||||||
|
'DOCKER_API_VERSION',
|
||||||
|
'DOCKER_CERT_PATH',
|
||||||
|
'DOCKER_CONFIG',
|
||||||
|
'DOCKER_CONTENT_TRUST_SERVER',
|
||||||
|
'DOCKER_CONTENT_TRUST',
|
||||||
|
'DOCKER_CONTEXT',
|
||||||
|
'DOCKER_DEFAULT_PLATFORM',
|
||||||
|
'DOCKER_HIDE_LEGACY_COMMANDS',
|
||||||
|
'DOCKER_HOST',
|
||||||
|
'DOCKER_STACK_ORCHESTRATOR',
|
||||||
|
'DOCKER_TLS_VERIFY',
|
||||||
|
'BUILDKIT_PROGRESS'
|
||||||
|
])
|
||||||
|
const dockerEnvs = {}
|
||||||
|
for (const key in process.env) {
|
||||||
|
if (dockerCliEnvs.has(key)) {
|
||||||
|
dockerEnvs[key] = process.env[key]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const newOptions = {
|
||||||
|
workingDir: options?.workingDir,
|
||||||
|
input: options?.input,
|
||||||
|
env: options?.env || {}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set docker envs or overwrite provided ones
|
||||||
|
for (const [key, value] of Object.entries(dockerEnvs)) {
|
||||||
|
newOptions.env[key] = value as string
|
||||||
|
}
|
||||||
|
|
||||||
|
return newOptions
|
||||||
|
}
|
||||||
|
|
||||||
export function sanitize(val: string): string {
|
export function sanitize(val: string): string {
|
||||||
if (!val || typeof val !== 'string') {
|
if (!val || typeof val !== 'string') {
|
||||||
return ''
|
return ''
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { sanitize } from '../src/utils'
|
import { optionsWithDockerEnvs, sanitize } from '../src/utils'
|
||||||
|
|
||||||
describe('Utilities', () => {
|
describe('Utilities', () => {
|
||||||
it('should return sanitized image name', () => {
|
it('should return sanitized image name', () => {
|
||||||
@@ -9,4 +9,41 @@ describe('Utilities', () => {
|
|||||||
const validStr = 'teststr8_one'
|
const validStr = 'teststr8_one'
|
||||||
expect(sanitize(validStr)).toBe(validStr)
|
expect(sanitize(validStr)).toBe(validStr)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('with docker options', () => {
|
||||||
|
it('should augment options with docker environment variables', () => {
|
||||||
|
process.env.DOCKER_HOST = 'unix:///run/user/1001/docker.sock'
|
||||||
|
process.env.DOCKER_NOTEXIST = 'notexist'
|
||||||
|
|
||||||
|
const optionDefinitions: any = [
|
||||||
|
undefined,
|
||||||
|
{},
|
||||||
|
{ env: {} },
|
||||||
|
{ env: { DOCKER_HOST: 'unix://var/run/docker.sock' } }
|
||||||
|
]
|
||||||
|
for (const opt of optionDefinitions) {
|
||||||
|
let options = optionsWithDockerEnvs(opt)
|
||||||
|
expect(options).toBeDefined()
|
||||||
|
expect(options?.env).toBeDefined()
|
||||||
|
expect(options?.env?.DOCKER_HOST).toBe(process.env.DOCKER_HOST)
|
||||||
|
expect(options?.env?.DOCKER_NOTEXIST).toBeUndefined()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should not overwrite other options', () => {
|
||||||
|
process.env.DOCKER_HOST = 'unix:///run/user/1001/docker.sock'
|
||||||
|
const opt = {
|
||||||
|
workingDir: 'test',
|
||||||
|
input: Buffer.from('test')
|
||||||
|
}
|
||||||
|
|
||||||
|
const options = optionsWithDockerEnvs(opt)
|
||||||
|
expect(options).toBeDefined()
|
||||||
|
expect(options?.workingDir).toBe(opt.workingDir)
|
||||||
|
expect(options?.input).toBe(opt.input)
|
||||||
|
expect(options?.env).toStrictEqual({
|
||||||
|
DOCKER_HOST: process.env.DOCKER_HOST
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user