Skip to content

Commit

Permalink
Merge pull request #47 from stuartleeks/sl/env-var-substitution
Browse files Browse the repository at this point in the history
Add env var substitution
  • Loading branch information
stuartleeks authored Jun 9, 2021
2 parents 1b56900 + 48cb168 commit 4a0e31d
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 6 deletions.
27 changes: 27 additions & 0 deletions __tests__/envvars.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {
substituteValues
} from '../src/envvars'

describe('substituteValues', () => {
test('returns original string with no substitution placeholders', async () => {
const input = 'This is a test'
const result = await substituteValues(input)
expect(result).toBe(input)
})

test('Handles \'env\' substitution placeholders', async () => {
process.env.TEST_ENV1='TestEnvValue1'
process.env.TEST_ENV2='TestEnvValue2'
const input = 'TEST_ENV1: ${env:TEST_ENV1}, TEST_ENV2: ${env:TEST_ENV2}'
const result = await substituteValues(input)
expect(result).toBe('TEST_ENV1: TestEnvValue1, TEST_ENV2: TestEnvValue2')
})

test('ignores substitution placeholders that it doesn\'t understand', async () => {
const input = 'TEST_ENV: ${nothingToSee:TEST_ENV}'
const result = await substituteValues(input)
expect(result).toBe(input)
})

})

1 change: 0 additions & 1 deletion __tests__/inputs.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import parse from 'csv-parse'
import {
parseInputAsList,
parseInputAsRecord
Expand Down
42 changes: 40 additions & 2 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.

1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface DevContainerConfig {
args?: Record<string, string>
}
runArgs?: string[]
mounts?: string[]
}

export async function loadFromFile(
Expand Down
8 changes: 6 additions & 2 deletions src/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as core from '@actions/core'
import * as config from './config'
import {exec, execWithOptions} from './exec'
import {getAbsolutePath} from './file'
import {substituteValues} from './envvars'

export async function isDockerBuildXInstalled(): Promise<boolean> {
const r = await exec('docker', 'buildx', '--help')
Expand Down Expand Up @@ -43,7 +44,7 @@ export async function buildImage(

const buildArgs = devcontainerConfig.build?.args
for (const argName in buildArgs) {
const argValue = buildArgs[argName]
const argValue = substituteValues(buildArgs[argName])
args.push('--build-arg', `${argName}=${argValue}`)
}

Expand Down Expand Up @@ -98,7 +99,10 @@ export async function runContainer(
args.push('--workdir', workspaceFolder)
args.push('--user', remoteUser)
if (devcontainerConfig.runArgs) {
args.push(...devcontainerConfig.runArgs)
const subtitutedRunArgs = devcontainerConfig.runArgs.map(a =>
substituteValues(a)
)
args.push(...subtitutedRunArgs)
}
if (envs) {
for (const env of envs) {
Expand Down
26 changes: 26 additions & 0 deletions src/envvars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export function substituteValues(input: string): string {
// Find all `${...}` entries and substitute
// Note the non-greedy `.+?` match to avoid matching the start of
// one placeholder up to the end of another when multiple placeholders are present
return input.replace(/\$\{(.+?)\}/g, getSubstitutionValue)
}

function getSubstitutionValue(regexMatch: string, placeholder: string): string {
// Substitution values are in TYPE:KEY form
// e.g. env:MY_ENV

const parts = placeholder.split(':')
if (parts.length === 2) {
const type = parts[0]
const key = parts[1]
switch (type) {
case 'env':
case 'localenv':
return process.env[key] ?? ''
}
}

// if we can't process the format then return the original string
// as having it present in any output will likely make issues more obvious
return regexMatch
}

0 comments on commit 4a0e31d

Please sign in to comment.