Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ inputs:
sidecar-env-vars:
description: 'The environment variables to pass to the sidecar container'
required: false
docker-env-inherit:
description: 'Environment variable names to inherit from runner to docker container (comma-separated list or array in config.yml)'
required: false
sidecar-env-inherit:
description: 'Environment variable names to inherit from runner to sidecar container (comma-separated list or array in config.yml)'
required: false
custom-defaults-paths:
description: 'Specify comma-separated custom defaults paths'
required: false
Expand Down
42 changes: 39 additions & 3 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.

4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ export interface ActionConfiguration {
dockerImage: string
dockerOptions: string
dockerEnvVars: string
dockerEnvInherit: string
sidecarImage: string
sidecarOptions: string
sidecarEnvVars: string
sidecarEnvInherit: string
retrieveDefaultConfig: boolean
customDefaultsPaths: string
customStageConditionsPath: string
Expand Down Expand Up @@ -103,9 +105,11 @@ export async function getActionConfig (options: InputOptions): Promise<ActionCon
dockerImage: getValue('docker-image'),
dockerOptions: getValue('docker-options'),
dockerEnvVars: getValue('docker-env-vars'),
dockerEnvInherit: getValue('docker-env-inherit'),
sidecarImage: getValue('sidecar-image'),
sidecarOptions: getValue('sidecar-options'),
sidecarEnvVars: getValue('sidecar-env-vars'),
sidecarEnvInherit: getValue('sidecar-env-inherit'),
retrieveDefaultConfig: getValue('retrieve-default-config') === 'true',
customDefaultsPaths: getValue('custom-defaults-paths'),
customStageConditionsPath: getValue('custom-stage-conditions-path'),
Expand Down
38 changes: 38 additions & 0 deletions src/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export async function startContainer (actionCfg: ActionConfiguration, ctxConfig:

dockerRunArgs.push(
...parseDockerEnvVars(actionCfg.dockerEnvVars, ctxConfig.dockerEnvVars),
...parseDockerEnvInherit(actionCfg.dockerEnvInherit !== '' ? actionCfg.dockerEnvInherit : ctxConfig.dockerEnvInherit),
...getProxyEnvVars(),
...getOrchestratorEnvVars(),
...getVaultEnvVars(),
Expand Down Expand Up @@ -114,6 +115,8 @@ export function getOrchestratorEnvVars (): string[] {
'--env', 'GITHUB_REPOSITORY',
'--env', 'GITHUB_SHA',
'--env', 'GITHUB_WORKFLOW_REF',
// Workspace (needed for copying artifacts from container builds)
'--env', 'GITHUB_WORKSPACE',
// Pull Request Info (needed for sonarExecuteScan)
'--env', 'GITHUB_HEAD_REF',
'--env', 'GITHUB_BASE_REF',
Expand Down Expand Up @@ -164,6 +167,41 @@ export function getDockerImageFromEnvVar (dockerImage: string): string[] {
]
}

/**
* Parse docker-env-inherit input to inherit environment variables from runner.
* Accepts comma-separated list of env var names or an array (from config.yml).
* Example inputs:
* - "MY_VAR,ANOTHER_VAR" (comma-separated string)
* - ["MY_VAR", "ANOTHER_VAR"] (array from config.yml)
* @param envInherit - The env-inherit input value (from action input or context config)
* @returns Array of --env arguments for docker run
*/
export function parseDockerEnvInherit (envInherit: string | string[] | undefined): string[] {
if (envInherit === undefined || envInherit === '') {
return []
}

const result: string[] = []
let envVarNames: string[] = []

if (Array.isArray(envInherit)) {
// Already an array from context config (config.yml)
envVarNames = envInherit
} else if (typeof envInherit === 'string') {
// Parse as comma-separated list
envVarNames = envInherit.split(',').map(name => name.trim()).filter(name => name !== '')
}

// Generate --env arguments for each variable name (inherits value from host)
for (const name of envVarNames) {
if (typeof name === 'string' && name.trim() !== '') {
result.push('--env', name.trim())
}
}

return result
}

export async function dockerExecReadOutput (dockerRunArgs: string[]): Promise<string> {
let dockerOutput = ''
let dockerError = ''
Expand Down
4 changes: 3 additions & 1 deletion src/sidecar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
dockerExecReadOutput,
getOrchestratorEnvVars,
getProxyEnvVars,
getVaultEnvVars
getVaultEnvVars,
parseDockerEnvInherit
} from './docker'
import { v4 as uuidv4 } from 'uuid'
import { debug, info, warning } from '@actions/core'
Expand Down Expand Up @@ -45,6 +46,7 @@ export async function startSidecar (actionCfg: ActionConfiguration, ctxConfig: a

dockerRunArgs.push(
...parseDockerEnvVars(actionCfg.sidecarEnvVars, ctxConfig.sidecarEnvVars),
...parseDockerEnvInherit(actionCfg.sidecarEnvInherit !== '' ? actionCfg.sidecarEnvInherit : ctxConfig.sidecarEnvInherit),
...getProxyEnvVars(),
...getOrchestratorEnvVars(),
...getVaultEnvVars(),
Expand Down
49 changes: 48 additions & 1 deletion test/docker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
startContainer,
stopContainer,
dockerExecReadOutput,
getDockerImageFromEnvVar
getDockerImageFromEnvVar,
parseDockerEnvInherit
} from '../src/docker'

jest.mock('@actions/core')
Expand All @@ -42,9 +43,11 @@ describe('Docker', () => {
dockerImage: '',
dockerOptions: '',
dockerEnvVars: '',
dockerEnvInherit: '',
sidecarImage: '',
sidecarOptions: '',
sidecarEnvVars: '',
sidecarEnvInherit: '',
retrieveDefaultConfig: false,
customDefaultsPaths: '',
customStageConditionsPath: '',
Expand Down Expand Up @@ -306,4 +309,48 @@ describe('Docker', () => {

await expect(dockerExecReadOutput(dockerArgs)).rejects.toThrow('docker execute failed with exit code')
})

test('getOrchestratorEnvVars includes GITHUB_WORKSPACE', () => {
const envVars = getOrchestratorEnvVars()
const idx = envVars.indexOf('GITHUB_WORKSPACE')
expect(idx).toBeGreaterThan(0)
expect(envVars[idx - 1]).toBe('--env')
})

describe('parseDockerEnvInherit', () => {
test('returns empty array for undefined input', () => {
const result = parseDockerEnvInherit(undefined)
expect(result).toEqual([])
})

test('returns empty array for empty string', () => {
const result = parseDockerEnvInherit('')
expect(result).toEqual([])
})

test('parses comma-separated list', () => {
const result = parseDockerEnvInherit('MY_VAR,ANOTHER_VAR')
expect(result).toEqual(['--env', 'MY_VAR', '--env', 'ANOTHER_VAR'])
})

test('parses comma-separated list with spaces', () => {
const result = parseDockerEnvInherit('MY_VAR, ANOTHER_VAR , THIRD_VAR')
expect(result).toEqual(['--env', 'MY_VAR', '--env', 'ANOTHER_VAR', '--env', 'THIRD_VAR'])
})

test('handles array input (from config.yml)', () => {
const result = parseDockerEnvInherit(['MY_VAR', 'ANOTHER_VAR'])
expect(result).toEqual(['--env', 'MY_VAR', '--env', 'ANOTHER_VAR'])
})

test('handles single value', () => {
const result = parseDockerEnvInherit('MY_VAR')
expect(result).toEqual(['--env', 'MY_VAR'])
})

test('filters empty values in comma-separated list', () => {
const result = parseDockerEnvInherit('MY_VAR,,ANOTHER_VAR,')
expect(result).toEqual(['--env', 'MY_VAR', '--env', 'ANOTHER_VAR'])
})
})
})
2 changes: 2 additions & 0 deletions test/sidecar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ describe('Sidecar', () => {
dockerImage: '',
dockerOptions: '',
dockerEnvVars: '',
dockerEnvInherit: '',
sidecarImage: '',
sidecarOptions: '',
sidecarEnvVars: '',
sidecarEnvInherit: '',
retrieveDefaultConfig: false,
customDefaultsPaths: '',
customStageConditionsPath: '',
Expand Down
Loading