Skip to content

Commit a1ffbe9

Browse files
committed
✨ Add support to secret env
1 parent fdd740d commit a1ffbe9

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

__tests__/buildx/inputs.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,21 @@ describe('resolveBuildSecret', () => {
177177
expect(e.message).toEqual(error?.message);
178178
}
179179
});
180+
181+
test.each([
182+
['FOO=bar', 'FOO', 'bar', null],
183+
['FOO=', 'FOO', '', new Error('FOO= is not a valid secret')],
184+
['=bar', '', '', new Error('=bar is not a valid secret')],
185+
['FOO=bar=baz', 'FOO', 'bar=baz', null]
186+
])('given %p key and %p env', async (kvp: string, exKey: string, exValue: string, error: Error | null) => {
187+
try {
188+
const secret = Inputs.resolveBuildSecretEnv(kvp);
189+
expect(secret).toEqual(`id=${exKey},env="${exValue}"`);
190+
} catch (e) {
191+
// eslint-disable-next-line jest/no-conditional-expect
192+
expect(e.message).toEqual(error?.message);
193+
}
194+
});
180195
});
181196

182197
describe('hasLocalExporter', () => {

src/buildx/inputs.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ import {parse} from 'csv-parse/sync';
2121

2222
import {Context} from '../context';
2323

24+
const parseKvp = (kvp: string): [string, string] => {
25+
const delimiterIndex = kvp.indexOf('=');
26+
const key = kvp.substring(0, delimiterIndex);
27+
const value = kvp.substring(delimiterIndex + 1);
28+
29+
if (key.length == 0 || value.length == 0) {
30+
throw new Error(`${kvp} is not a valid secret`);
31+
}
32+
33+
return [key, value];
34+
};
35+
2436
export class Inputs {
2537
public static getBuildImageIDFilePath(): string {
2638
return path.join(Context.tmpDir(), 'iidfile');
@@ -70,13 +82,17 @@ export class Inputs {
7082
return Inputs.resolveBuildSecret(kvp, true);
7183
}
7284

85+
public static resolveBuildSecretEnv(kvp: string): string {
86+
const [key, value] = parseKvp(kvp);
87+
88+
return `id=${key},env="${value}"`;
89+
}
90+
7391
public static resolveBuildSecret(kvp: string, file: boolean): string {
74-
const delimiterIndex = kvp.indexOf('=');
75-
const key = kvp.substring(0, delimiterIndex);
76-
let value = kvp.substring(delimiterIndex + 1);
77-
if (key.length == 0 || value.length == 0) {
78-
throw new Error(`${kvp} is not a valid secret`);
79-
}
92+
const [key, _value] = parseKvp(kvp);
93+
94+
let value = _value;
95+
8096
if (file) {
8197
if (!fs.existsSync(value)) {
8298
throw new Error(`secret file ${value} not found`);

0 commit comments

Comments
 (0)