mirror of
https://github.com/Dokploy/dokploy.git
synced 2026-06-27 10:05:32 +02:00
feat: implement comprehensive environment variable resolution in preparation functions, enhancing flexibility and support for nested references across services and environments
This commit is contained in:
@@ -259,21 +259,44 @@ export const removeService = async (
|
||||
export const prepareEnvironmentVariables = (
|
||||
serviceEnv: string | null,
|
||||
projectEnv?: string | null,
|
||||
environmentEnv?: string | null,
|
||||
) => {
|
||||
const projectVars = parse(projectEnv ?? "");
|
||||
const environmentVars = parse(environmentEnv ?? "");
|
||||
const serviceVars = parse(serviceEnv ?? "");
|
||||
|
||||
const resolvedVars = Object.entries(serviceVars).map(([key, value]) => {
|
||||
let resolvedValue = value;
|
||||
|
||||
// Replace project variables
|
||||
if (projectVars) {
|
||||
resolvedValue = value.replace(/\$\{\{project\.(.*?)\}\}/g, (_, ref) => {
|
||||
if (projectVars[ref] !== undefined) {
|
||||
return projectVars[ref];
|
||||
}
|
||||
throw new Error(`Invalid project environment variable: project.${ref}`);
|
||||
});
|
||||
resolvedValue = resolvedValue.replace(
|
||||
/\$\{\{project\.(.*?)\}\}/g,
|
||||
(_, ref) => {
|
||||
if (projectVars[ref] !== undefined) {
|
||||
return projectVars[ref];
|
||||
}
|
||||
throw new Error(
|
||||
`Invalid project environment variable: project.${ref}`,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// Replace environment variables
|
||||
if (environmentVars) {
|
||||
resolvedValue = resolvedValue.replace(
|
||||
/\$\{\{environment\.(.*?)\}\}/g,
|
||||
(_, ref) => {
|
||||
if (environmentVars[ref] !== undefined) {
|
||||
return environmentVars[ref];
|
||||
}
|
||||
throw new Error(`Invalid environment variable: environment.${ref}`);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// Replace self-references (service variables)
|
||||
resolvedValue = resolvedValue.replace(/\$\{\{(.*?)\}\}/g, (_, ref) => {
|
||||
if (serviceVars[ref] !== undefined) {
|
||||
return serviceVars[ref];
|
||||
@@ -301,8 +324,9 @@ export const parseEnvironmentKeyValuePair = (
|
||||
export const getEnviromentVariablesObject = (
|
||||
input: string | null,
|
||||
projectEnv?: string | null,
|
||||
environmentEnv?: string | null,
|
||||
) => {
|
||||
const envs = prepareEnvironmentVariables(input, projectEnv);
|
||||
const envs = prepareEnvironmentVariables(input, projectEnv, environmentEnv);
|
||||
|
||||
const jsonObject: Record<string, string> = {};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user