Skip to content

Commit 898063b

Browse files
authored
repaired docker PATH export and added tests both for docker and k8s (#17)
* repaired docker PATH export and added tests both for docker and k8s * added todo comments about next major version and typeof prepend path
1 parent 266b8ed commit 898063b

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

packages/docker/src/dockerCommands/container.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,20 @@ export async function containerPorts(id: string): Promise<string[]> {
274274
return portMappings.split('\n').filter(p => !!p)
275275
}
276276

277+
export async function getContainerEnvValue(
278+
id: string,
279+
name: string
280+
): Promise<string> {
281+
const dockerArgs = [
282+
'inspect',
283+
`--format='{{range $index, $value := .Config.Env}}{{if eq (index (split $value "=") 0) "${name}"}}{{index (split $value "=") 1}}{{end}}{{end}}'`,
284+
id
285+
]
286+
const value = (await runDockerCommand(dockerArgs)).trim()
287+
const lines = value.split('\n')
288+
return lines.length ? lines[0].replace(/^'/, '').replace(/'$/, '') : ''
289+
}
290+
277291
export async function registryLogin(registry?: Registry): Promise<string> {
278292
if (!registry) {
279293
return ''
@@ -346,7 +360,16 @@ export async function containerExecStep(
346360
}
347361

348362
if (args.prependPath?.length) {
349-
dockerArgs.push('-e', `"PATH=${args.prependPath.join(':')}:$PATH"`)
363+
// TODO: remove compatibility with typeof prependPath === 'string' as we bump to next major version, the hooks will lose PrependPath compat with runners 2.293.0 and older
364+
const prependPath =
365+
typeof args.prependPath === 'string'
366+
? args.prependPath
367+
: args.prependPath.join(':')
368+
369+
dockerArgs.push(
370+
'-e',
371+
`PATH=${prependPath}:${await getContainerEnvValue(containerId, 'PATH')}`
372+
)
350373
}
351374

352375
dockerArgs.push(containerId)

packages/docker/tests/run-script-step-test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,28 @@ describe('run script step', () => {
3434
runScriptStep(definitions.runScriptStep.args, prepareJobResponse.state)
3535
).resolves.not.toThrow()
3636
})
37+
38+
it('Should have path variable changed in container with prepend path string', async () => {
39+
definitions.runScriptStep.args.prependPath = '/some/path'
40+
definitions.runScriptStep.args.entryPoint = '/bin/bash'
41+
definitions.runScriptStep.args.entryPointArgs = [
42+
'-c',
43+
`if [[ ! $(env | grep "^PATH=") = "PATH=${definitions.runScriptStep.args.prependPath}:"* ]]; then exit 1; fi`
44+
]
45+
await expect(
46+
runScriptStep(definitions.runScriptStep.args, prepareJobResponse.state)
47+
).resolves.not.toThrow()
48+
})
49+
50+
it('Should have path variable changed in container with prepend path string array', async () => {
51+
definitions.runScriptStep.args.prependPath = ['/some/other/path']
52+
definitions.runScriptStep.args.entryPoint = '/bin/bash'
53+
definitions.runScriptStep.args.entryPointArgs = [
54+
'-c',
55+
`if [[ ! $(env | grep "^PATH=") = "PATH=${definitions.runScriptStep.args.prependPath}:"* ]]; then exit 1; fi`
56+
]
57+
await expect(
58+
runScriptStep(definitions.runScriptStep.args, prepareJobResponse.state)
59+
).resolves.not.toThrow()
60+
})
3761
})

packages/k8s/src/k8s/utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ export function writeEntryPointScript(
100100
): { containerPath: string; runnerPath: string } {
101101
let exportPath = ''
102102
if (prependPath?.length) {
103-
exportPath = `export PATH=${prependPath.join(':')}:$PATH`
103+
// TODO: remove compatibility with typeof prependPath === 'string' as we bump to next major version, the hooks will lose PrependPath compat with runners 2.293.0 and older
104+
const prepend =
105+
typeof prependPath === 'string' ? prependPath : prependPath.join(':')
106+
exportPath = `export PATH=${prepend}:$PATH`
104107
}
105108
let environmentPrefix = ''
106109

packages/k8s/tests/run-script-step-test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,38 @@ describe('Run script step', () => {
7171
)
7272
).resolves.not.toThrow()
7373
})
74+
75+
it('Should have path variable changed in container with prepend path string', async () => {
76+
runScriptStepDefinition.args.prependPath = '/some/path'
77+
runScriptStepDefinition.args.entryPoint = '/bin/bash'
78+
runScriptStepDefinition.args.entryPointArgs = [
79+
'-c',
80+
`'if [[ ! $(env | grep "^PATH=") = "PATH=${runScriptStepDefinition.args.prependPath}:"* ]]; then exit 1; fi'`
81+
]
82+
83+
await expect(
84+
runScriptStep(
85+
runScriptStepDefinition.args,
86+
prepareJobOutputData.state,
87+
null
88+
)
89+
).resolves.not.toThrow()
90+
})
91+
92+
it('Should have path variable changed in container with prepend path string array', async () => {
93+
runScriptStepDefinition.args.prependPath = ['/some/other/path']
94+
runScriptStepDefinition.args.entryPoint = '/bin/bash'
95+
runScriptStepDefinition.args.entryPointArgs = [
96+
'-c',
97+
`'if [[ ! $(env | grep "^PATH=") = "PATH=${runScriptStepDefinition.args.prependPath}:"* ]]; then exit 1; fi'`
98+
]
99+
100+
await expect(
101+
runScriptStep(
102+
runScriptStepDefinition.args,
103+
prepareJobOutputData.state,
104+
null
105+
)
106+
).resolves.not.toThrow()
107+
})
74108
})

0 commit comments

Comments
 (0)