diff --git a/docs/commands/data.json b/docs/commands/data.json index 1e3c26ee6..02a6e1f7a 100644 --- a/docs/commands/data.json +++ b/docs/commands/data.json @@ -236,7 +236,8 @@ "hld append-variable-group": { "command": "append-variable-group ", "alias": "avg", - "description": "Appends the name of an existing variable group to the current manifest-generation.yaml file." + "description": "Appends the name of an existing variable group to the current manifest-generation.yaml file.", + "markdown": "## Description\n\nAppend a variable group name to the current `manifest-generation.yaml` of an\ninitialized hld repository.\n\n## Example\n\nWhen an HLD repository is first initialized with `spk hld init`, the top portion\nof the `manifest-generation.yaml` looks like this:\n\n```yaml\n# GENERATED WITH SPK VERSION 0.5.8\ntrigger:\n branches:\n include:\n - master\nvariables: []\npool:\n vmImage: ubuntu-latest\nsteps:\n.\n.\n.\n```\n\nrunning `spk hld append-variable-group my-vg` with a variable group name, in\nthis case `my-vg`, will add it under the `variables` section if it does not\nalready exist:\n\n```yaml\n# GENERATED WITH SPK VERSION 0.5.8\ntrigger:\n branches:\n include:\n - master\nvariables:\n - group: my-variable-group\npool:\n vmImage: ubuntu-latest\nsteps:\n.\n.\n.\n```\n" }, "hld init": { "command": "init", diff --git a/src/commands/hld/append-variable-group.md b/src/commands/hld/append-variable-group.md new file mode 100644 index 000000000..45fcaa09c --- /dev/null +++ b/src/commands/hld/append-variable-group.md @@ -0,0 +1,44 @@ +## Description + +Append a variable group name to the current `manifest-generation.yaml` of an +initialized hld repository. + +## Example + +When an HLD repository is first initialized with `spk hld init`, the top portion +of the `manifest-generation.yaml` looks like this: + +```yaml +# GENERATED WITH SPK VERSION 0.5.8 +trigger: + branches: + include: + - master +variables: [] +pool: + vmImage: ubuntu-latest +steps: +. +. +. +``` + +running `spk hld append-variable-group my-vg` with a variable group name, in +this case `my-vg`, will add it under the `variables` section if it does not +already exist: + +```yaml +# GENERATED WITH SPK VERSION 0.5.8 +trigger: + branches: + include: + - master +variables: + - group: my-variable-group +pool: + vmImage: ubuntu-latest +steps: +. +. +. +``` diff --git a/src/lib/fileutils.ts b/src/lib/fileutils.ts index 17b299c59..4d3c1176f 100644 --- a/src/lib/fileutils.ts +++ b/src/lib/fileutils.ts @@ -496,11 +496,23 @@ export const appendVariableGroupToPipelineYaml = ( path.join(dir, fileName) ) as AzurePipelinesYaml; pipelineFile.variables = pipelineFile.variables || []; + let variableGroupExists = false; + + pipelineFile.variables.forEach((variable) => { + if ("group" in variable && variable.group === variableGroupName) { + variableGroupExists = true; + logger.info( + `Variable group '${variableGroupName}' already exits in '${dir}/${fileName}'.` + ); + } + }); - pipelineFile.variables.push({ group: variableGroupName }); + if (!variableGroupExists) { + pipelineFile.variables.push({ group: variableGroupName }); - logger.info(`Updating '${dir}/${fileName}'.`); - write(pipelineFile, dir, fileName); + logger.info(`Updating '${dir}/${fileName}'.`); + write(pipelineFile, dir, fileName); + } } catch (err) { throw buildError( errorStatusCode.FILE_IO_ERR,