Skip to content

Commit

Permalink
Merge pull request #824 from veimox/feature/secrets-expand-env-vars
Browse files Browse the repository at this point in the history
Feature: Expand envrionment variables on secrets
  • Loading branch information
gperdomor authored Jul 16, 2023
2 parents 335d9ef + b1c52ae commit 1c4df14
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
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

0 comments on commit 1c4df14

Please sign in to comment.