mirror of
https://github.com/actions/runner-container-hooks.git
synced 2025-12-14 16:46:43 +00:00
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
This commit is contained in:
@@ -5,7 +5,10 @@
|
|||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "jest --runInBand",
|
"test": "jest --runInBand",
|
||||||
"build": "npx tsc && npx ncc build"
|
"build": "npx tsc && npx ncc build",
|
||||||
|
"format": "prettier --write '**/*.ts'",
|
||||||
|
"format-check": "prettier --check '**/*.ts'",
|
||||||
|
"lint": "eslint src/**/*.ts"
|
||||||
},
|
},
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
@@ -43,11 +43,16 @@ export async function createContainer(
|
|||||||
|
|
||||||
if (args.environmentVariables) {
|
if (args.environmentVariables) {
|
||||||
for (const [key] of Object.entries(args.environmentVariables)) {
|
for (const [key] of Object.entries(args.environmentVariables)) {
|
||||||
dockerArgs.push('-e')
|
dockerArgs.push('-e', key)
|
||||||
dockerArgs.push(key)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dockerArgs.push('-e', 'GITHUB_ACTIONS=true')
|
||||||
|
// Use same behavior as the runner https://github.com/actions/runner/blob/27d9c886ab9a45e0013cb462529ac85d581f8c41/src/Runner.Worker/Container/DockerCommandManager.cs#L150
|
||||||
|
if (!('CI' in (args.environmentVariables ?? {}))) {
|
||||||
|
dockerArgs.push('-e', 'CI=true')
|
||||||
|
}
|
||||||
|
|
||||||
const mountVolumes = [
|
const mountVolumes = [
|
||||||
...(args.userMountVolumes || []),
|
...(args.userMountVolumes || []),
|
||||||
...(args.systemMountVolumes || [])
|
...(args.systemMountVolumes || [])
|
||||||
@@ -403,11 +408,16 @@ export async function containerRun(
|
|||||||
}
|
}
|
||||||
if (args.environmentVariables) {
|
if (args.environmentVariables) {
|
||||||
for (const [key] of Object.entries(args.environmentVariables)) {
|
for (const [key] of Object.entries(args.environmentVariables)) {
|
||||||
dockerArgs.push('-e')
|
dockerArgs.push('-e', key)
|
||||||
dockerArgs.push(key)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dockerArgs.push('-e', 'GITHUB_ACTIONS=true')
|
||||||
|
// Use same behavior as the runner https://github.com/actions/runner/blob/27d9c886ab9a45e0013cb462529ac85d581f8c41/src/Runner.Worker/Container/DockerCommandManager.cs#L150
|
||||||
|
if (!('CI' in (args.environmentVariables ?? {}))) {
|
||||||
|
dockerArgs.push('-e', 'CI=true')
|
||||||
|
}
|
||||||
|
|
||||||
const mountVolumes = [
|
const mountVolumes = [
|
||||||
...(args.userMountVolumes || []),
|
...(args.userMountVolumes || []),
|
||||||
...(args.systemMountVolumes || [])
|
...(args.systemMountVolumes || [])
|
||||||
|
|||||||
@@ -75,4 +75,22 @@ describe('run script step', () => {
|
|||||||
runScriptStep(definitions.runScriptStep.args, prepareJobResponse.state)
|
runScriptStep(definitions.runScriptStep.args, prepareJobResponse.state)
|
||||||
).resolves.not.toThrow()
|
).resolves.not.toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('Should confirm that CI and GITHUB_ACTIONS are set', async () => {
|
||||||
|
definitions.runScriptStep.args.entryPoint = '/bin/bash'
|
||||||
|
definitions.runScriptStep.args.entryPointArgs = [
|
||||||
|
'-c',
|
||||||
|
`'if [[ ! $(env | grep "^CI=") = "CI=true" ]]; then exit 1; fi'`
|
||||||
|
]
|
||||||
|
await expect(
|
||||||
|
runScriptStep(definitions.runScriptStep.args, prepareJobResponse.state)
|
||||||
|
).resolves.not.toThrow()
|
||||||
|
definitions.runScriptStep.args.entryPointArgs = [
|
||||||
|
'-c',
|
||||||
|
`'if [[ ! $(env | grep "^GITHUB_ACTIONS=") = "GITHUB_ACTIONS=true" ]]; then exit 1; fi'`
|
||||||
|
]
|
||||||
|
await expect(
|
||||||
|
runScriptStep(definitions.runScriptStep.args, prepareJobResponse.state)
|
||||||
|
).resolves.not.toThrow()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -229,6 +229,18 @@ export function createContainerSpec(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
podContainer.env.push({
|
||||||
|
name: 'GITHUB_ACTIONS',
|
||||||
|
value: 'true'
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!('CI' in container['environmentVariables'])) {
|
||||||
|
podContainer.env.push({
|
||||||
|
name: 'CI',
|
||||||
|
value: 'true'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
podContainer.volumeMounts = containerVolumes(
|
podContainer.volumeMounts = containerVolumes(
|
||||||
container.userMountVolumes,
|
container.userMountVolumes,
|
||||||
jobContainer
|
jobContainer
|
||||||
|
|||||||
@@ -29,7 +29,14 @@ export async function runContainerStep(
|
|||||||
let secretName: string | undefined = undefined
|
let secretName: string | undefined = undefined
|
||||||
if (stepContainer.environmentVariables) {
|
if (stepContainer.environmentVariables) {
|
||||||
try {
|
try {
|
||||||
secretName = await createSecretForEnvs(stepContainer.environmentVariables)
|
const envs = JSON.parse(
|
||||||
|
JSON.stringify(stepContainer.environmentVariables)
|
||||||
|
)
|
||||||
|
envs['GITHUB_ACTIONS'] = 'true'
|
||||||
|
if (!('CI' in envs)) {
|
||||||
|
envs.CI = 'true'
|
||||||
|
}
|
||||||
|
secretName = await createSecretForEnvs(envs)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
core.debug(`createSecretForEnvs failed: ${JSON.stringify(err)}`)
|
core.debug(`createSecretForEnvs failed: ${JSON.stringify(err)}`)
|
||||||
const message = (err as any)?.response?.body?.message || err
|
const message = (err as any)?.response?.body?.message || err
|
||||||
|
|||||||
@@ -62,6 +62,54 @@ describe('Prepare job', () => {
|
|||||||
).resolves.not.toThrow()
|
).resolves.not.toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should prepare job with envs CI and GITHUB_ACTIONS', async () => {
|
||||||
|
await prepareJob(prepareJobData.args, prepareJobOutputFilePath)
|
||||||
|
|
||||||
|
const content = JSON.parse(
|
||||||
|
fs.readFileSync(prepareJobOutputFilePath).toString()
|
||||||
|
)
|
||||||
|
|
||||||
|
const got = await getPodByName(content.state.jobPod)
|
||||||
|
expect(got.spec?.containers[0].env).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
{ name: 'CI', value: 'true' },
|
||||||
|
{ name: 'GITHUB_ACTIONS', value: 'true' }
|
||||||
|
])
|
||||||
|
)
|
||||||
|
expect(got.spec?.containers[1].env).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
{ name: 'CI', value: 'true' },
|
||||||
|
{ name: 'GITHUB_ACTIONS', value: 'true' }
|
||||||
|
])
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should not override CI env var if already set', async () => {
|
||||||
|
prepareJobData.args.container.environmentVariables = {
|
||||||
|
CI: 'false'
|
||||||
|
}
|
||||||
|
|
||||||
|
await prepareJob(prepareJobData.args, prepareJobOutputFilePath)
|
||||||
|
|
||||||
|
const content = JSON.parse(
|
||||||
|
fs.readFileSync(prepareJobOutputFilePath).toString()
|
||||||
|
)
|
||||||
|
|
||||||
|
const got = await getPodByName(content.state.jobPod)
|
||||||
|
expect(got.spec?.containers[0].env).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
{ name: 'CI', value: 'false' },
|
||||||
|
{ name: 'GITHUB_ACTIONS', value: 'true' }
|
||||||
|
])
|
||||||
|
)
|
||||||
|
expect(got.spec?.containers[1].env).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
{ name: 'CI', value: 'true' },
|
||||||
|
{ name: 'GITHUB_ACTIONS', value: 'true' }
|
||||||
|
])
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
it('should throw an exception if the user volume mount is absolute path outside of GITHUB_WORKSPACE', async () => {
|
it('should throw an exception if the user volume mount is absolute path outside of GITHUB_WORKSPACE', async () => {
|
||||||
prepareJobData.args.container.userMountVolumes = [
|
prepareJobData.args.container.userMountVolumes = [
|
||||||
{
|
{
|
||||||
@@ -133,9 +181,13 @@ describe('Prepare job', () => {
|
|||||||
expect(got.spec?.containers[1].image).toBe('redis')
|
expect(got.spec?.containers[1].image).toBe('redis')
|
||||||
expect(got.spec?.containers[1].command).toBeFalsy()
|
expect(got.spec?.containers[1].command).toBeFalsy()
|
||||||
expect(got.spec?.containers[1].args).toBeFalsy()
|
expect(got.spec?.containers[1].args).toBeFalsy()
|
||||||
expect(got.spec?.containers[1].env).toEqual([
|
expect(got.spec?.containers[1].env).toEqual(
|
||||||
{ name: 'ENV2', value: 'value2' }
|
expect.arrayContaining([
|
||||||
])
|
{ name: 'CI', value: 'true' },
|
||||||
|
{ name: 'GITHUB_ACTIONS', value: 'true' },
|
||||||
|
{ name: 'ENV2', value: 'value2' }
|
||||||
|
])
|
||||||
|
)
|
||||||
expect(got.spec?.containers[1].resources).toEqual({
|
expect(got.spec?.containers[1].resources).toEqual({
|
||||||
requests: { memory: '1Mi', cpu: '1' },
|
requests: { memory: '1Mi', cpu: '1' },
|
||||||
limits: { memory: '1Gi', cpu: '2' }
|
limits: { memory: '1Gi', cpu: '2' }
|
||||||
|
|||||||
@@ -78,4 +78,15 @@ describe('Run container step', () => {
|
|||||||
runContainerStep(runContainerStepData.args)
|
runContainerStep(runContainerStepData.args)
|
||||||
).resolves.not.toThrow()
|
).resolves.not.toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should run container step with envs CI and GITHUB_ACTIONS', async () => {
|
||||||
|
runContainerStepData.args.entryPoint = 'bash'
|
||||||
|
runContainerStepData.args.entryPointArgs = [
|
||||||
|
'-c',
|
||||||
|
"'if [[ -z $GITHUB_ACTIONS ]] || [[ -z $CI ]]; then exit 1; fi'"
|
||||||
|
]
|
||||||
|
await expect(
|
||||||
|
runContainerStep(runContainerStepData.args)
|
||||||
|
).resolves.not.toThrow()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user