Skip to content
Open
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
41 changes: 40 additions & 1 deletion __tests__/ut/commands/build_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import BuilderFactory, { BuildType } from '../../../src/subCommands/build';
import BuilderFactory, { BuildType, resolveEnvVariables } from '../../../src/subCommands/build';
import { ImageBuiltKitBuilder } from '../../../src/subCommands/build/impl/imageBuiltKitBuilder';
import { ImageDockerBuilder } from '../../../src/subCommands/build/impl/imageDockerBuilder';
import { ImageKanikoBuilder } from '../../../src/subCommands/build/impl/imageKanikoBuilder';
Expand Down Expand Up @@ -302,4 +302,43 @@ describe('BuilderFactory', () => {
expect(builder2).toBe(mockBuilder2);
});
});

describe('resolveEnvVariables', () => {
it('should replace env variables with single quotes', () => {
process.env.MY_VAR = 'hello';
const result = resolveEnvVariables("value: ${env('MY_VAR')}");
expect(result).toBe('value: hello');
delete process.env.MY_VAR;
});

it('should replace env variables with double quotes', () => {
process.env.MY_VAR = 'world';
const result = resolveEnvVariables('value: ${env("MY_VAR")}');
expect(result).toBe('value: world');
delete process.env.MY_VAR;
});

it('should replace multiple env variables', () => {
process.env.USERNAME = 'admin';
process.env.PASSWORD = 'secret';
const result = resolveEnvVariables(
"username: ${env('USERNAME')}\npassword: ${env('PASSWORD')}",
);
expect(result).toBe('username: admin\npassword: secret');
delete process.env.USERNAME;
delete process.env.PASSWORD;
});

it('should throw error when env variable is not found', () => {
delete process.env.NOT_EXIST;
expect(() => resolveEnvVariables("value: ${env('NOT_EXIST')}")).toThrow(
'Environment variable not found: NOT_EXIST',
);
});

it('should not modify content without env variables', () => {
const content = 'region: cn-hangzhou\nruntime: nodejs18';
expect(resolveEnvVariables(content)).toBe(content);
});
});
});
14 changes: 13 additions & 1 deletion src/subCommands/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ export enum BuildType {
Default = 'DEFAULT',
}

export function resolveEnvVariables(content: string): string {
return content.replace(/\$\{env\(['"](.+?)['"]\)\}/g, (match, envName: string) => {
const value = process.env[envName];
if (value === undefined) {
logger.error(`Environment variable not found: ${envName}`);
throw new Error(`Environment variable not found: ${envName}`);
}
return value;
});
}

export default class BuilderFactory {
private inputs: IInputs;
private debugInstance: boolean;
Expand Down Expand Up @@ -73,7 +84,8 @@ export default class BuilderFactory {

try {
const buildYamlContent = fs.readFileSync(buildYamlPath, 'utf8');
const buildConfig = yaml.load(buildYamlContent) as any;
const resolvedContent = resolveEnvVariables(buildYamlContent);
const buildConfig = yaml.load(resolvedContent) as any;

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