Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Expand envrionment variables on secrets #824

Merged
merged 6 commits into from
Jul 16, 2023
Merged
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
13 changes: 13 additions & 0 deletions packages/core/src/lib/interpolate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,17 @@ describe('String interpolation', () => {
])('given command "%s", should return "%s"', (command: string, expected: string) => {
expect(interpolate(command)).toEqual(expected);
});

it('should expand ${HOME}', () => {
expect(interpolate('${HOME}')).toEqual(process.env['HOME']);
});

it('should expand $HOME', () => {
expect(interpolate('$HOME')).toEqual(process.env['HOME']);
});

it('should not expand', () => {
const value = 'HOME';
expect(interpolate(value)).toEqual(value);
});
});
2 changes: 1 addition & 1 deletion packages/nx-container/docs/inputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Following inputs can be used as `step.with` keys
| `push` | Bool | [Push](https://github.com/docker/buildx#--push) is a shorthand for `--output=type=registry` (default `false`) |
| `quiet` | Bool | Run executor without printing engine info |
| `secret-files` | List | List of secret files to expose to the build (eg. key=filename, MY_SECRET=./secret.txt) |
| `secrets` | List | List of secrets to expose to the build (eg. `key=value`, `GIT_AUTH_TOKEN=mytoken`) |
| `secrets` | List | List of secrets to expose to the build (eg. `key=value`, `GIT_AUTH_TOKEN=mytoken`, `NPM_TOKEN=${NPM_TOKEN}` will use the environment variable) |
| `shm-size`¹ | String | Size of [`/dev/shm`](https://github.com/docker/buildx/blob/master/docs/reference/buildx_build.md#-size-of-devshm---shm-size) (e.g., `2g`) |
| `ssh` | List | List of SSH agent socket or keys to expose to the build |
| `tags` | List/CSV | List of tags |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from 'node:fs';
import path from 'node:path';
import * as semver from 'semver';
import * as context from '../../context';
import { interpolate } from '@nx-tools/core';

export async function getImageIDFile(): Promise<string> {
return path.join(context.tmpDir(), 'iidfile').split(path.sep).join(path.posix.sep);
Expand Down Expand Up @@ -65,6 +66,8 @@ export async function getSecret(kvp: string, file: boolean): Promise<string> {
throw new Error(`secret file ${value} not found`);
}
value = fs.readFileSync(value, { encoding: 'utf-8' });
} else {
value = interpolate(value);
}

const secretFile = context.tmpNameSync({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from 'node:fs';
import path from 'node:path';
import * as semver from 'semver';
import * as context from '../../context';
import { interpolate } from '@nx-tools/core';

export async function getImageIDFile(): Promise<string> {
return path.join(context.tmpDir(), 'iidfile').split(path.sep).join(path.posix.sep);
Expand Down Expand Up @@ -65,6 +66,8 @@ export async function getSecret(kvp: string, file: boolean): Promise<string> {
throw new Error(`secret file ${value} not found`);
}
value = fs.readFileSync(value, { encoding: 'utf-8' });
} else {
value = interpolate(value);
}

const secretFile = context.tmpNameSync({
Expand Down
2 changes: 1 addition & 1 deletion packages/nx-container/src/executors/build/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
"items": {
"type": "string"
},
"description": "List of secrets to expose to the build (eg. key=string, GIT_AUTH_TOKEN=mytoken)"
"description": "List of secrets to expose to the build (eg. key=string, GIT_AUTH_TOKEN=mytoken, NPM_TOKEN=${NPM_TOKEN})"
},
"secret-files": {
"type": "array",
Expand Down