Skip to content

Commit

Permalink
add inheritEnv action parameter (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarTawfik committed Jul 18, 2024
1 parent 4432f30 commit 4dc5cb4
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 7 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ inputs:
env:
required: false
description: Specify environment variables to pass to the docker run command
inheritEnv:
required: false
default: false
description: Inherit all environment variables of the runner CI machine.
skipContainerUserIdUpdate:
required: false
default: false
Expand Down
3 changes: 2 additions & 1 deletion azdo-task/DevcontainersCi/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export async function runMain(): Promise<void> {
const relativeConfigFile = task.getInput('configFile');
const runCommand = task.getInput('runCmd');
const envs = task.getInput('env')?.split('\n') ?? [];
const inputEnvsWithDefaults = populateDefaults(envs);
const inheritEnv = (task.getInput('inheritEnv') ?? 'false') === 'true';
const inputEnvsWithDefaults = populateDefaults(envs, inheritEnv);
const cacheFrom = task.getInput('cacheFrom')?.split('\n') ?? [];
const noCache = (task.getInput('noCache') ?? 'false') === 'true';
const skipContainerUserIdUpdate =
Expand Down
9 changes: 8 additions & 1 deletion azdo-task/DevcontainersCi/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@
"label": "Specify environment variables to pass to the docker run command",
"required": false
},
{
"name": "inheritEnv",
"type": "boolean",
"label": "Inherit all environment variables of the runner CI machine",
"defaultValue": false,
"required": false
},
{
"name": "push",
"type": "pickList",
Expand Down Expand Up @@ -133,4 +140,4 @@
"argumentFormat": ""
}
}
}
}
1 change: 1 addition & 0 deletions azdo-task/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ In the example above, the devcontainer-build-run will perform the following step
| configFile | false | Use this to specify the repo-relative path to the devcontainer.json file. Defaults to `./.devcontainer/devcontainer.json` and `./.devcontainer.json`. |
| runCmd | true | The command to run after building the dev container image |
| env | false | Specify environment variables to pass to the dev container when run |
| inheritEnv | false | Inherit all environment variables of the runner CI machine |
| 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. |
| pushOnFailedBuild | false | If `false` (default), only push if the build is successful. Set to true to push on failed builds |
| 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 |
Expand Down
19 changes: 16 additions & 3 deletions common/__tests__/envvars.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,34 @@ describe('substituteValues', () => {
describe('populateDefaults', () => {
test('returns original inputs when fully specified', () => {
const input = ['TEST_ENV1=value1', 'TEST_ENV2=value2'];
const result = populateDefaults(input);
const result = populateDefaults(input, false);
expect(result).toEqual(['TEST_ENV1=value1', 'TEST_ENV2=value2']);
});

test('adds process env value when set and input value not provided', () => {
const input = ['TEST_ENV1', 'TEST_ENV2=value2'];
process.env.TEST_ENV1 = 'TestEnvValue1';
const result = populateDefaults(input);
const result = populateDefaults(input, false);
expect(result).toEqual(['TEST_ENV1=TestEnvValue1', 'TEST_ENV2=value2']);
});

test('skips value when process env value not set and input value not provided', () => {
const input = ['TEST_ENV1', 'TEST_ENV2=value2'];
delete process.env.TEST_ENV1;
const result = populateDefaults(input);
const result = populateDefaults(input, false);
expect(result).toEqual(['TEST_ENV2=value2']);
});

test('inherits process env when asked', () => {
const originalEnv = process.env;
try {
process.env = {TEST_ENV1: 'value1'};
const input = ['TEST_ENV2=value2'];
const result = populateDefaults(input, true);
expect(result).toEqual(['TEST_ENV1=value1', 'TEST_ENV2=value2']);
}
finally {
process.env = originalEnv;
}
});
});
14 changes: 13 additions & 1 deletion common/src/envvars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,20 @@ function getSubstitutionValue(regexMatch: string, placeholder: string): string {
// In the latter case, the corresponding returned item would be "BAR=hi"
// where the value is taken from the matching process env var.
// In the case of values not set in the process, they are omitted
export function populateDefaults(envs: string[]): string[] {
export function populateDefaults(envs: string[], inheritEnv: boolean): string[] {
const result: string[] = [];
if (inheritEnv) {
for (const [key, value] of Object.entries(process.env)) {
switch (key) {
case 'PATH':
// don't copy these by default (user can still explicitly specify them).
break;
default:
result.push(`${key}=${value}`);
break;
}
}
}
for (let i = 0; i < envs.length; i++) {
const inputEnv = envs[i];
if (inputEnv.indexOf('=') >= 0) {
Expand Down
1 change: 1 addition & 0 deletions docs/azure-devops-task.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ In the example above, the devcontainer-build-run will perform the following step
| configFile | false | Use this to specify the repo-relative path to the devcontainer.json file. Defaults to `./.devcontainer/devcontainer.json` and `./.devcontainer.json`. |
| runCmd | true | The command to run after building the dev container image |
| env | false | Specify environment variables to pass to the dev container when run |
| inheritEnv | false | Inherit all environment variables of the runner CI machine |
| 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. |
| pushOnFailedBuild | false | If `false` (default), only push if the build is successful. Set to true to push on failed builds |
| 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 |
Expand Down
1 change: 1 addition & 0 deletions docs/github-action.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ The [`devcontainers/ci` action](https://github.com/marketplace/actions/devcontai
| configFile | false | Use this to specify the repo-relative path to the devcontainer.json file. Defaults to `./.devcontainer/devcontainer.json` and `./.devcontainer.json`. |
| runCmd | true | The command to run after building the dev container image |
| env | false | Specify environment variables to pass to the dev container when run |
| inheritEnv | false | Inherit all environment variables of the runner CI machine |
| checkoutPath | false | Only used for development/testing |
| 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. |
| 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) |
Expand Down
3 changes: 2 additions & 1 deletion github-action/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export async function runMain(): Promise<void> {
);
const runCommand = core.getInput('runCmd');
const inputEnvs: string[] = core.getMultilineInput('env');
const inputEnvsWithDefaults = populateDefaults(inputEnvs);
const inheritEnv: boolean = core.getBooleanInput('inheritEnv');
const inputEnvsWithDefaults = populateDefaults(inputEnvs, inheritEnv);
const cacheFrom: string[] = core.getMultilineInput('cacheFrom');
const noCache: boolean = core.getBooleanInput('noCache');
const skipContainerUserIdUpdate = core.getBooleanInput(
Expand Down

0 comments on commit 4dc5cb4

Please sign in to comment.