Skip to content

Commit 4dc5cb4

Browse files
authored
add inheritEnv action parameter (#295)
1 parent 4432f30 commit 4dc5cb4

File tree

9 files changed

+48
-7
lines changed

9 files changed

+48
-7
lines changed

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ inputs:
4444
env:
4545
required: false
4646
description: Specify environment variables to pass to the docker run command
47+
inheritEnv:
48+
required: false
49+
default: false
50+
description: Inherit all environment variables of the runner CI machine.
4751
skipContainerUserIdUpdate:
4852
required: false
4953
default: false

azdo-task/DevcontainersCi/src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ export async function runMain(): Promise<void> {
4444
const relativeConfigFile = task.getInput('configFile');
4545
const runCommand = task.getInput('runCmd');
4646
const envs = task.getInput('env')?.split('\n') ?? [];
47-
const inputEnvsWithDefaults = populateDefaults(envs);
47+
const inheritEnv = (task.getInput('inheritEnv') ?? 'false') === 'true';
48+
const inputEnvsWithDefaults = populateDefaults(envs, inheritEnv);
4849
const cacheFrom = task.getInput('cacheFrom')?.split('\n') ?? [];
4950
const noCache = (task.getInput('noCache') ?? 'false') === 'true';
5051
const skipContainerUserIdUpdate =

azdo-task/DevcontainersCi/task.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@
6464
"label": "Specify environment variables to pass to the docker run command",
6565
"required": false
6666
},
67+
{
68+
"name": "inheritEnv",
69+
"type": "boolean",
70+
"label": "Inherit all environment variables of the runner CI machine",
71+
"defaultValue": false,
72+
"required": false
73+
},
6774
{
6875
"name": "push",
6976
"type": "pickList",
@@ -133,4 +140,4 @@
133140
"argumentFormat": ""
134141
}
135142
}
136-
}
143+
}

azdo-task/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ In the example above, the devcontainer-build-run will perform the following step
7474
| configFile | false | Use this to specify the repo-relative path to the devcontainer.json file. Defaults to `./.devcontainer/devcontainer.json` and `./.devcontainer.json`. |
7575
| runCmd | true | The command to run after building the dev container image |
7676
| env | false | Specify environment variables to pass to the dev container when run |
77+
| inheritEnv | false | Inherit all environment variables of the runner CI machine |
7778
| push | false | One of: `never`, `filter`, `always`. When set to `filter`, the image if pushed if the `sourceBranchFilterForPush`, `buildReasonsForPush`, and `pushOnFailedBuild` conditions are met. Defaults to `filter` if `imageName` is set, `never` otherwise. |
7879
| pushOnFailedBuild | false | If `false` (default), only push if the build is successful. Set to true to push on failed builds |
7980
| sourceBranchFilterForPush | false | Allows you to limit which branch's builds are pushed to the registry (only specified branches are allowed to push). If empty, all branches are allowed |

common/__tests__/envvars.test.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,34 @@ describe('substituteValues', () => {
3333
describe('populateDefaults', () => {
3434
test('returns original inputs when fully specified', () => {
3535
const input = ['TEST_ENV1=value1', 'TEST_ENV2=value2'];
36-
const result = populateDefaults(input);
36+
const result = populateDefaults(input, false);
3737
expect(result).toEqual(['TEST_ENV1=value1', 'TEST_ENV2=value2']);
3838
});
3939

4040
test('adds process env value when set and input value not provided', () => {
4141
const input = ['TEST_ENV1', 'TEST_ENV2=value2'];
4242
process.env.TEST_ENV1 = 'TestEnvValue1';
43-
const result = populateDefaults(input);
43+
const result = populateDefaults(input, false);
4444
expect(result).toEqual(['TEST_ENV1=TestEnvValue1', 'TEST_ENV2=value2']);
4545
});
4646

4747
test('skips value when process env value not set and input value not provided', () => {
4848
const input = ['TEST_ENV1', 'TEST_ENV2=value2'];
4949
delete process.env.TEST_ENV1;
50-
const result = populateDefaults(input);
50+
const result = populateDefaults(input, false);
5151
expect(result).toEqual(['TEST_ENV2=value2']);
5252
});
53+
54+
test('inherits process env when asked', () => {
55+
const originalEnv = process.env;
56+
try {
57+
process.env = {TEST_ENV1: 'value1'};
58+
const input = ['TEST_ENV2=value2'];
59+
const result = populateDefaults(input, true);
60+
expect(result).toEqual(['TEST_ENV1=value1', 'TEST_ENV2=value2']);
61+
}
62+
finally {
63+
process.env = originalEnv;
64+
}
65+
});
5366
});

common/src/envvars.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,20 @@ function getSubstitutionValue(regexMatch: string, placeholder: string): string {
2929
// In the latter case, the corresponding returned item would be "BAR=hi"
3030
// where the value is taken from the matching process env var.
3131
// In the case of values not set in the process, they are omitted
32-
export function populateDefaults(envs: string[]): string[] {
32+
export function populateDefaults(envs: string[], inheritEnv: boolean): string[] {
3333
const result: string[] = [];
34+
if (inheritEnv) {
35+
for (const [key, value] of Object.entries(process.env)) {
36+
switch (key) {
37+
case 'PATH':
38+
// don't copy these by default (user can still explicitly specify them).
39+
break;
40+
default:
41+
result.push(`${key}=${value}`);
42+
break;
43+
}
44+
}
45+
}
3446
for (let i = 0; i < envs.length; i++) {
3547
const inputEnv = envs[i];
3648
if (inputEnv.indexOf('=') >= 0) {

docs/azure-devops-task.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ In the example above, the devcontainer-build-run will perform the following step
7474
| configFile | false | Use this to specify the repo-relative path to the devcontainer.json file. Defaults to `./.devcontainer/devcontainer.json` and `./.devcontainer.json`. |
7575
| runCmd | true | The command to run after building the dev container image |
7676
| env | false | Specify environment variables to pass to the dev container when run |
77+
| inheritEnv | false | Inherit all environment variables of the runner CI machine |
7778
| push | false | One of: `never`, `filter`, `always`. When set to `filter`, the image if pushed if the `sourceBranchFilterForPush`, `buildReasonsForPush`, and `pushOnFailedBuild` conditions are met. Defaults to `filter` if `imageName` is set, `never` otherwise. |
7879
| pushOnFailedBuild | false | If `false` (default), only push if the build is successful. Set to true to push on failed builds |
7980
| sourceBranchFilterForPush | false | Allows you to limit which branch's builds are pushed to the registry (only specified branches are allowed to push). If empty, all branches are allowed |

docs/github-action.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ The [`devcontainers/ci` action](https://github.com/marketplace/actions/devcontai
133133
| configFile | false | Use this to specify the repo-relative path to the devcontainer.json file. Defaults to `./.devcontainer/devcontainer.json` and `./.devcontainer.json`. |
134134
| runCmd | true | The command to run after building the dev container image |
135135
| env | false | Specify environment variables to pass to the dev container when run |
136+
| inheritEnv | false | Inherit all environment variables of the runner CI machine |
136137
| checkoutPath | false | Only used for development/testing |
137138
| push | false | Control when images are pushed. Options are `never`, `filter`, `always`. For `filter`, images are pushed if the `refFilterForPush` and `eventFilterForPush` conditions are met. Defaults to `filter` if `imageName` is set, `never` otherwise. |
138139
| refFilterForPush | false | Set the source branches (e.g. `refs/heads/main`) that are allowed to trigger a push of the dev container image. Leave empty to allow all (default) |

github-action/src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ export async function runMain(): Promise<void> {
5353
);
5454
const runCommand = core.getInput('runCmd');
5555
const inputEnvs: string[] = core.getMultilineInput('env');
56-
const inputEnvsWithDefaults = populateDefaults(inputEnvs);
56+
const inheritEnv: boolean = core.getBooleanInput('inheritEnv');
57+
const inputEnvsWithDefaults = populateDefaults(inputEnvs, inheritEnv);
5758
const cacheFrom: string[] = core.getMultilineInput('cacheFrom');
5859
const noCache: boolean = core.getBooleanInput('noCache');
5960
const skipContainerUserIdUpdate = core.getBooleanInput(

0 commit comments

Comments
 (0)