Skip to content

Commit

Permalink
refactor: more explicit code/comments for environment variable consol…
Browse files Browse the repository at this point in the history
…idation
  • Loading branch information
rocketeerbkw committed Feb 20, 2025
1 parent f92dcac commit 90724ff
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions node-packages/commons/src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,10 +668,13 @@ export const getEnvironmentsRouterPatternAndVariables = async function(
scope: EnvVariableScope | InternalEnvVariableScope;
}

// Single list of env vars that apply to an environment. Env vars from multiple
// sources (organization, project, enviornment, build vars, etc) are
// consolidated based on precedence.
let appliedEnvVars: EnvKeyValueInternal[] = [];

// Adds newVar only if it's unique (by name).
const addApplied = (newVar: EnvKeyValueInternal): void => {
// Appends newVar to appliedEnvVars only if newVar is unique by name.
const applyIfNotExists = (newVar: EnvKeyValueInternal): void => {
const index = appliedEnvVars.findIndex((searchVar) => searchVar.name === newVar.name)

if (index === -1) {
Expand All @@ -698,31 +701,34 @@ export const getEnvironmentsRouterPatternAndVariables = async function(

/*
* Internal scoped env vars.
*
* Uses the env vars system to send data to lagoon-remote but should not be
* overrideable by Lagoon API env vars.
*/

addApplied({
applyIfNotExists({
name: "LAGOON_SYSTEM_CORE_VERSION",
value: getConfigFromEnv('LAGOON_VERSION', 'unknown'),
scope: InternalEnvVariableScope.INTERNAL_SYSTEM
});

addApplied({
applyIfNotExists({
name: 'LAGOON_SYSTEM_ROUTER_PATTERN',
value: routerPattern,
scope: InternalEnvVariableScope.INTERNAL_SYSTEM
});

const [bucketName, isSharedBucket] = await getBaasBucketName(project, deployTarget)
if (isSharedBucket) {
addApplied({
applyIfNotExists({
name: "LAGOON_SYSTEM_PROJECT_SHARED_BUCKET",
value: bucketName,
scope: InternalEnvVariableScope.INTERNAL_SYSTEM
});
}

if (org) {
addApplied({
applyIfNotExists({
name: "LAGOON_ROUTE_QUOTA",
value: `${org.quotaRoute}`,
scope: InternalEnvVariableScope.INTERNAL_SYSTEM
Expand All @@ -731,11 +737,13 @@ export const getEnvironmentsRouterPatternAndVariables = async function(

/*
* Normally scoped env vars.
*
* Env vars that are set by users, or derived from them.
*/

// Build env vars passed to the API.
for (const buildVar of buildVariables) {
addApplied({
applyIfNotExists({
name: buildVar.name,
value: buildVar.value,
scope: EnvVariableScope.BUILD
Expand All @@ -744,53 +752,53 @@ export const getEnvironmentsRouterPatternAndVariables = async function(

// Bulk deployment env vars.
if (bulkTask === bulkType.Deploy) {
addApplied({
applyIfNotExists({
name: "LAGOON_BUILD_PRIORITY",
value: buildPriority.toString(),
scope: EnvVariableScope.BUILD
});

if (bulkId) {
addApplied({
applyIfNotExists({
name: "LAGOON_BULK_DEPLOY",
value: "true",
scope: EnvVariableScope.BUILD
});
addApplied({
applyIfNotExists({
name: "LAGOON_BULK_DEPLOY_ID",
value: bulkId,
scope: EnvVariableScope.BUILD
});

if (bulkName) {
addApplied({
applyIfNotExists({
name: "LAGOON_BULK_DEPLOY_NAME",
value: bulkName,
scope: EnvVariableScope.BUILD
});
}
}
} else if (bulkTask === bulkType.Task) {
addApplied({
applyIfNotExists({
name: "LAGOON_TASK_PRIORITY",
value: buildPriority.toString(),
scope: EnvVariableScope.BUILD
})

if (bulkId) {
addApplied({
applyIfNotExists({
name: "LAGOON_BULK_TASK",
value: "true",
scope: EnvVariableScope.BUILD
});
addApplied({
applyIfNotExists({
name: "LAGOON_BULK_TASK_ID",
value: bulkId,
scope: EnvVariableScope.BUILD
});

if (bulkName) {
addApplied({
applyIfNotExists({
name: "LAGOON_BULK_TASK_NAME",
value: bulkName,
scope: EnvVariableScope.BUILD
Expand All @@ -801,18 +809,18 @@ export const getEnvironmentsRouterPatternAndVariables = async function(

// Environment env vars.
for (const envVar of environment.envVariables) {
addApplied(envVar)
applyIfNotExists(envVar)
}

// Project env vars.
for (const projVar of project.envVariables) {
addApplied(projVar)
applyIfNotExists(projVar)
}

if (org) {
// Organization env vars.
for (const orgVar of org.envVariables) {
addApplied(orgVar)
applyIfNotExists(orgVar)
}
}

Expand Down

0 comments on commit 90724ff

Please sign in to comment.