Skip to content

Commit

Permalink
fix: override action variables via --var cli flag
Browse files Browse the repository at this point in the history
allow overriding action variables via --var cli flag. Previously, variables set
using the --var cli flag only had impact on the
project level variables.
  • Loading branch information
shumailxyz committed Jul 10, 2023
1 parent e059481 commit 6b0b9a6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
2 changes: 1 addition & 1 deletion core/src/garden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1996,7 +1996,7 @@ export const resolveGardenParams = profileAsync(async function _resolveGardenPar
})

// Override variables, also allows to override nested variables using dot notation
function overrideVariables(variables: DeepPrimitiveMap, overrideVariables: DeepPrimitiveMap): DeepPrimitiveMap {
export function overrideVariables(variables: DeepPrimitiveMap, overrideVariables: DeepPrimitiveMap): DeepPrimitiveMap {
let objNew = cloneDeep(variables)
Object.keys(overrideVariables).forEach((key) => {
if (objNew.hasOwnProperty(key)) {
Expand Down
9 changes: 7 additions & 2 deletions core/src/graph/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import type { GroupConfig } from "../config/group"
import { ActionConfigContext } from "../config/template-contexts/actions"
import { validateWithPath } from "../config/validation"
import { ConfigurationError, InternalError, PluginError, ValidationError } from "../exceptions"
import type { Garden } from "../garden"
import { overrideVariables, type Garden } from "../garden"
import type { Log } from "../logger/log-entry"
import type { ActionTypeDefinition } from "../plugin/action-types"
import { getActionTypeBases } from "../plugins"
Expand Down Expand Up @@ -299,12 +299,17 @@ export const actionFromConfig = profileAsync(async function actionFromConfig({
config.internal.treeVersion ||
(await garden.vcs.getTreeVersion({ log, projectName: garden.projectName, config, scanRoot }))

const variables = await mergeVariables({
let variables = await mergeVariables({
basePath: config.internal.basePath,
variables: config.variables,
varfiles: config.varfiles,
})

// override the variables if there's any matching variables in variable overrides
// passed via --var cli flag. variables passed via --var cli flag have highest precedence
const variableOverrides = garden.variableOverrides || {}
variables = overrideVariables(variables ?? {}, variableOverrides)

const params: ActionWrapperParams<any> = {
baseBuildDirectory: garden.buildStaging.buildDirPath,
compatibleTypes,
Expand Down
67 changes: 66 additions & 1 deletion core/test/unit/src/actions/action-configs-to-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { actionConfigsToGraph } from "../../../../src/graph/actions"
import { ModuleGraph } from "../../../../src/graph/modules"
import { Log } from "../../../../src/logger/log-entry"
import { dumpYaml } from "../../../../src/util/serialization"
import { expectError, makeTempGarden, TempDirectory, TestGarden } from "../../../helpers"
import { createProjectConfig, expectError, makeTempGarden, TempDirectory, TestGarden } from "../../../helpers"
import {
DEFAULT_BUILD_TIMEOUT_SEC,
DEFAULT_DEPLOY_TIMEOUT_SEC,
Expand Down Expand Up @@ -545,6 +545,71 @@ describe("actionConfigsToGraph", () => {
expect(vars).to.eql({ foo: "FOO", bar: "BAR", baz: "baz" })
})

it("correctly merges varfile with variables when some variables are overridden with --var cli flag", async () => {
const dummyGardenInstance = await makeTempGarden({
config: createProjectConfig({
name: "test",
environments: [{ name: "default", defaultNamespace: "foo", variables: {} }],
}),
variableOverrides: { "foo": "NEW_FOO", "nested.key1": "NEW_KEY_1_VALUE" },
})

let tmpDir = dummyGardenInstance.tmpDir
let garden = dummyGardenInstance.garden
let log = garden.log

try {
const varfilePath = join(tmpDir.path, "varfile.yml")
await dumpYaml(varfilePath, {
foo: "FOO",
bar: "BAR",
nested: {
key1: "SOME_VALUE",
},
})

const graph = await actionConfigsToGraph({
garden,
log,
groupConfigs: [],
configs: [
{
kind: "Build",
type: "test",
name: "foo",
timeout: DEFAULT_BUILD_TIMEOUT_SEC,
variables: {
foo: "foo",
baz: "baz",
},
varfiles: [varfilePath],
internal: {
basePath: tmpDir.path,
},
spec: {},
},
],
moduleGraph: new ModuleGraph([], {}),
actionModes: {},
linkedSources: {},
})

const action = graph.getBuild("foo")
const vars = action["variables"]

expect(vars).to.eql({
foo: "NEW_FOO",
bar: "BAR",
baz: "baz",
nested: {
key1: "NEW_KEY_1_VALUE",
},
})
} finally {
await tmpDir.cleanup()
}
})

it("sets sync mode correctly if explicitly set in actionModes", async () => {
const graph = await actionConfigsToGraph({
garden,
Expand Down

0 comments on commit 6b0b9a6

Please sign in to comment.