Skip to content

Commit c56b0ed

Browse files
authored
Merge pull request #155 from devsapp/build-runit
feat: resolve ${env('...')} variables in cloud build yaml before parsing
2 parents 6e5c531 + 1639d2d commit c56b0ed

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

__tests__/ut/commands/build_test.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import BuilderFactory, { BuildType } from '../../../src/subCommands/build';
1+
import BuilderFactory, { BuildType, resolveEnvVariables } from '../../../src/subCommands/build';
22
import { ImageBuiltKitBuilder } from '../../../src/subCommands/build/impl/imageBuiltKitBuilder';
33
import { ImageDockerBuilder } from '../../../src/subCommands/build/impl/imageDockerBuilder';
44
import { ImageKanikoBuilder } from '../../../src/subCommands/build/impl/imageKanikoBuilder';
@@ -302,4 +302,43 @@ describe('BuilderFactory', () => {
302302
expect(builder2).toBe(mockBuilder2);
303303
});
304304
});
305+
306+
describe('resolveEnvVariables', () => {
307+
it('should replace env variables with single quotes', () => {
308+
process.env.MY_VAR = 'hello';
309+
const result = resolveEnvVariables("value: ${env('MY_VAR')}");
310+
expect(result).toBe('value: hello');
311+
delete process.env.MY_VAR;
312+
});
313+
314+
it('should replace env variables with double quotes', () => {
315+
process.env.MY_VAR = 'world';
316+
const result = resolveEnvVariables('value: ${env("MY_VAR")}');
317+
expect(result).toBe('value: world');
318+
delete process.env.MY_VAR;
319+
});
320+
321+
it('should replace multiple env variables', () => {
322+
process.env.USERNAME = 'admin';
323+
process.env.PASSWORD = 'secret';
324+
const result = resolveEnvVariables(
325+
"username: ${env('USERNAME')}\npassword: ${env('PASSWORD')}",
326+
);
327+
expect(result).toBe('username: admin\npassword: secret');
328+
delete process.env.USERNAME;
329+
delete process.env.PASSWORD;
330+
});
331+
332+
it('should throw error when env variable is not found', () => {
333+
delete process.env.NOT_EXIST;
334+
expect(() => resolveEnvVariables("value: ${env('NOT_EXIST')}")).toThrow(
335+
'Environment variable not found: NOT_EXIST',
336+
);
337+
});
338+
339+
it('should not modify content without env variables', () => {
340+
const content = 'region: cn-hangzhou\nruntime: nodejs18';
341+
expect(resolveEnvVariables(content)).toBe(content);
342+
});
343+
});
305344
});

src/subCommands/build/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ export enum BuildType {
2222
Default = 'DEFAULT',
2323
}
2424

25+
export function resolveEnvVariables(content: string): string {
26+
return content.replace(/\$\{env\(['"](.+?)['"]\)\}/g, (match, envName: string) => {
27+
const value = process.env[envName];
28+
if (value === undefined) {
29+
logger.error(`Environment variable not found: ${envName}`);
30+
throw new Error(`Environment variable not found: ${envName}`);
31+
}
32+
return value;
33+
});
34+
}
35+
2536
export default class BuilderFactory {
2637
private inputs: IInputs;
2738
private debugInstance: boolean;
@@ -73,7 +84,8 @@ export default class BuilderFactory {
7384

7485
try {
7586
const buildYamlContent = fs.readFileSync(buildYamlPath, 'utf8');
76-
const buildConfig = yaml.load(buildYamlContent) as any;
87+
const resolvedContent = resolveEnvVariables(buildYamlContent);
88+
const buildConfig = yaml.load(resolvedContent) as any;
7789

7890
// 从build.yaml中提取配置,支持多种格式
7991
let config = buildConfig;

0 commit comments

Comments
 (0)