Skip to content

Commit 375992c

Browse files
authored
Expose CI=true and GITHUB_ACTIONS env variables (#215)
* Expose CI=true and GITHUB_ACTIONS env variables * fmt * revert the prettier and finish this * revert package-lock.json
1 parent aae800a commit 375992c

File tree

7 files changed

+122
-9
lines changed

7 files changed

+122
-9
lines changed

packages/docker/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
"main": "lib/index.js",
66
"scripts": {
77
"test": "jest --runInBand",
8-
"build": "npx tsc && npx ncc build"
8+
"build": "npx tsc && npx ncc build",
9+
"format": "prettier --write '**/*.ts'",
10+
"format-check": "prettier --check '**/*.ts'",
11+
"lint": "eslint src/**/*.ts"
912
},
1013
"author": "",
1114
"license": "MIT",

packages/docker/src/dockerCommands/container.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,16 @@ export async function createContainer(
4343

4444
if (args.environmentVariables) {
4545
for (const [key] of Object.entries(args.environmentVariables)) {
46-
dockerArgs.push('-e')
47-
dockerArgs.push(key)
46+
dockerArgs.push('-e', key)
4847
}
4948
}
5049

50+
dockerArgs.push('-e', 'GITHUB_ACTIONS=true')
51+
// Use same behavior as the runner https://github.com/actions/runner/blob/27d9c886ab9a45e0013cb462529ac85d581f8c41/src/Runner.Worker/Container/DockerCommandManager.cs#L150
52+
if (!('CI' in (args.environmentVariables ?? {}))) {
53+
dockerArgs.push('-e', 'CI=true')
54+
}
55+
5156
const mountVolumes = [
5257
...(args.userMountVolumes || []),
5358
...(args.systemMountVolumes || [])
@@ -403,11 +408,16 @@ export async function containerRun(
403408
}
404409
if (args.environmentVariables) {
405410
for (const [key] of Object.entries(args.environmentVariables)) {
406-
dockerArgs.push('-e')
407-
dockerArgs.push(key)
411+
dockerArgs.push('-e', key)
408412
}
409413
}
410414

415+
dockerArgs.push('-e', 'GITHUB_ACTIONS=true')
416+
// Use same behavior as the runner https://github.com/actions/runner/blob/27d9c886ab9a45e0013cb462529ac85d581f8c41/src/Runner.Worker/Container/DockerCommandManager.cs#L150
417+
if (!('CI' in (args.environmentVariables ?? {}))) {
418+
dockerArgs.push('-e', 'CI=true')
419+
}
420+
411421
const mountVolumes = [
412422
...(args.userMountVolumes || []),
413423
...(args.systemMountVolumes || [])

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,22 @@ describe('run script step', () => {
7575
runScriptStep(definitions.runScriptStep.args, prepareJobResponse.state)
7676
).resolves.not.toThrow()
7777
})
78+
79+
it('Should confirm that CI and GITHUB_ACTIONS are set', async () => {
80+
definitions.runScriptStep.args.entryPoint = '/bin/bash'
81+
definitions.runScriptStep.args.entryPointArgs = [
82+
'-c',
83+
`'if [[ ! $(env | grep "^CI=") = "CI=true" ]]; then exit 1; fi'`
84+
]
85+
await expect(
86+
runScriptStep(definitions.runScriptStep.args, prepareJobResponse.state)
87+
).resolves.not.toThrow()
88+
definitions.runScriptStep.args.entryPointArgs = [
89+
'-c',
90+
`'if [[ ! $(env | grep "^GITHUB_ACTIONS=") = "GITHUB_ACTIONS=true" ]]; then exit 1; fi'`
91+
]
92+
await expect(
93+
runScriptStep(definitions.runScriptStep.args, prepareJobResponse.state)
94+
).resolves.not.toThrow()
95+
})
7896
})

packages/k8s/src/hooks/prepare-job.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,18 @@ export function createContainerSpec(
229229
}
230230
}
231231

232+
podContainer.env.push({
233+
name: 'GITHUB_ACTIONS',
234+
value: 'true'
235+
})
236+
237+
if (!('CI' in container['environmentVariables'])) {
238+
podContainer.env.push({
239+
name: 'CI',
240+
value: 'true'
241+
})
242+
}
243+
232244
podContainer.volumeMounts = containerVolumes(
233245
container.userMountVolumes,
234246
jobContainer

packages/k8s/src/hooks/run-container-step.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ export async function runContainerStep(
2929
let secretName: string | undefined = undefined
3030
if (stepContainer.environmentVariables) {
3131
try {
32-
secretName = await createSecretForEnvs(stepContainer.environmentVariables)
32+
const envs = JSON.parse(
33+
JSON.stringify(stepContainer.environmentVariables)
34+
)
35+
envs['GITHUB_ACTIONS'] = 'true'
36+
if (!('CI' in envs)) {
37+
envs.CI = 'true'
38+
}
39+
secretName = await createSecretForEnvs(envs)
3340
} catch (err) {
3441
core.debug(`createSecretForEnvs failed: ${JSON.stringify(err)}`)
3542
const message = (err as any)?.response?.body?.message || err

packages/k8s/tests/prepare-job-test.ts

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,54 @@ describe('Prepare job', () => {
6262
).resolves.not.toThrow()
6363
})
6464

65+
it('should prepare job with envs CI and GITHUB_ACTIONS', async () => {
66+
await prepareJob(prepareJobData.args, prepareJobOutputFilePath)
67+
68+
const content = JSON.parse(
69+
fs.readFileSync(prepareJobOutputFilePath).toString()
70+
)
71+
72+
const got = await getPodByName(content.state.jobPod)
73+
expect(got.spec?.containers[0].env).toEqual(
74+
expect.arrayContaining([
75+
{ name: 'CI', value: 'true' },
76+
{ name: 'GITHUB_ACTIONS', value: 'true' }
77+
])
78+
)
79+
expect(got.spec?.containers[1].env).toEqual(
80+
expect.arrayContaining([
81+
{ name: 'CI', value: 'true' },
82+
{ name: 'GITHUB_ACTIONS', value: 'true' }
83+
])
84+
)
85+
})
86+
87+
it('should not override CI env var if already set', async () => {
88+
prepareJobData.args.container.environmentVariables = {
89+
CI: 'false'
90+
}
91+
92+
await prepareJob(prepareJobData.args, prepareJobOutputFilePath)
93+
94+
const content = JSON.parse(
95+
fs.readFileSync(prepareJobOutputFilePath).toString()
96+
)
97+
98+
const got = await getPodByName(content.state.jobPod)
99+
expect(got.spec?.containers[0].env).toEqual(
100+
expect.arrayContaining([
101+
{ name: 'CI', value: 'false' },
102+
{ name: 'GITHUB_ACTIONS', value: 'true' }
103+
])
104+
)
105+
expect(got.spec?.containers[1].env).toEqual(
106+
expect.arrayContaining([
107+
{ name: 'CI', value: 'true' },
108+
{ name: 'GITHUB_ACTIONS', value: 'true' }
109+
])
110+
)
111+
})
112+
65113
it('should throw an exception if the user volume mount is absolute path outside of GITHUB_WORKSPACE', async () => {
66114
prepareJobData.args.container.userMountVolumes = [
67115
{
@@ -133,9 +181,13 @@ describe('Prepare job', () => {
133181
expect(got.spec?.containers[1].image).toBe('redis')
134182
expect(got.spec?.containers[1].command).toBeFalsy()
135183
expect(got.spec?.containers[1].args).toBeFalsy()
136-
expect(got.spec?.containers[1].env).toEqual([
137-
{ name: 'ENV2', value: 'value2' }
138-
])
184+
expect(got.spec?.containers[1].env).toEqual(
185+
expect.arrayContaining([
186+
{ name: 'CI', value: 'true' },
187+
{ name: 'GITHUB_ACTIONS', value: 'true' },
188+
{ name: 'ENV2', value: 'value2' }
189+
])
190+
)
139191
expect(got.spec?.containers[1].resources).toEqual({
140192
requests: { memory: '1Mi', cpu: '1' },
141193
limits: { memory: '1Gi', cpu: '2' }

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,15 @@ describe('Run container step', () => {
7878
runContainerStep(runContainerStepData.args)
7979
).resolves.not.toThrow()
8080
})
81+
82+
it('should run container step with envs CI and GITHUB_ACTIONS', async () => {
83+
runContainerStepData.args.entryPoint = 'bash'
84+
runContainerStepData.args.entryPointArgs = [
85+
'-c',
86+
"'if [[ -z $GITHUB_ACTIONS ]] || [[ -z $CI ]]; then exit 1; fi'"
87+
]
88+
await expect(
89+
runContainerStep(runContainerStepData.args)
90+
).resolves.not.toThrow()
91+
})
8192
})

0 commit comments

Comments
 (0)