Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Commit

Permalink
Add support for YAML variable files (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
qetza committed Feb 18, 2021
1 parent 7f8c519 commit 16df2cc
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The parameters of the task are described bellow, in parenthesis is the YAML name
- _noescape_: disable variable value escaping. (this can be used if you want to inject raw JSON or XML for example). Example: `#{noescape(MyVar)}#`
- **Transform prefix** (transformPrefix): The prefix between transform name and token name. Default: `(`.
- **Transform suffix** (transformSuffix): The suffix after the token name. Default: `)`.
- **Variable files (JSON)** (variableFiles): the absolute or relative comma or newline-separated paths to the files containing additional variables. Wildcards can be used (eg: `vars\**\*.json` for all _.json_ files in all sub folders of _vars_). Variables declared in files overrides variables defined in the pipeline.
- **Variable files (JSON or YAML)** (variableFiles): the absolute or relative comma or newline-separated paths to the files containing additional variables. Wildcards can be used (eg: `vars\**\*.json` for all _.json_ files in all sub folders of _vars_). YAML files **must have** the `.yml`or `.yaml` extension otherwise the file is treated as JSON. Variables declared in files overrides variables defined in the pipeline.
- **Variable separator** (variableSeparator): the separtor to use in variable names for nested objects and arrays in variable files. Example: `{ 'My': { 'Value': ['Hello World!'] } }` will create a variable _My.Value.0_ with the value _Hello World!_.
- **Send anonymous usage telemetry** (enableTelemetry): if checked anonymous usage data (hashed collection and pipeline id, no file parameter values, no variable values) will be sent to the task author only to analyze task usage.

Expand Down Expand Up @@ -91,6 +91,7 @@ If you want to use tokens in XML based configuration files to be replaced during
- Add support for variable transformations with _Enable tranformations_ ([#96](https://github.com/qetza/vsts-replacetokens-task/issues/96)).
- Add default value for tokens not found with _Default value_ (contribution from [ClemensSutor](https://github.com/ClemensSutor)).
- Group log outputs in Azure Pipelines output.
- Add support for variables in external YAML files with `.yml` or `.yaml` extension ([#177](https://github.com/qetza/vsts-replacetokens-task/issues/177)).

**New in 3.6.0**
- Add parameter _Use legacy pattern_ with default value to `false`.
Expand Down
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"devDependencies": {
"@types/iconv-lite": "0.0.1",
"@types/js-yaml": "^4.0.0",
"@types/node": "^6.14.13",
"@types/q": "0.0.32",
"minimist": "~1.2.5",
Expand All @@ -34,6 +35,7 @@
"dependencies": {
"azure-pipelines-task-lib": "^2.12.2",
"iconv-lite": "^0.4.15",
"js-yaml": "^4.0.0",
"jschardet": "^2.2.1"
}
}
13 changes: 9 additions & 4 deletions task/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import path = require('path');
import os = require('os');
import crypto = require('crypto');
import trackEvent, { TelemetryEvent } from './telemetry';
import yaml = require('js-yaml');

const ENCODING_AUTO: string = 'auto';
const ENCODING_ASCII: string = 'ascii';
Expand Down Expand Up @@ -539,10 +540,10 @@ async function run() {

tl.getDelimitedInput('variableFiles', '\n', false).forEach((l: string) => {
if (l)
l.split(',').forEach((path: string) => {
if (path)
l.split(',').forEach((p: string) => {
if (p)
{
tl.findMatch(root, normalize(path)).forEach(filePath => {
tl.findMatch(root, normalize(p)).forEach(filePath => {
if (tl.stats(filePath).isDirectory())
return;

Expand All @@ -556,7 +557,11 @@ async function run() {
logger.info('##[group]loading variables from: ' + filePath);

let encoding: string = getEncoding(filePath);
let variables: any = JSON.parse(iconv.decode(fs.readFileSync(filePath), encoding));
let extension: string = path.extname(filePath).toLowerCase();
let content: string = iconv.decode(fs.readFileSync(filePath), encoding);
let variables: any = extension === '.yaml' || extension === '.yml'
? yaml.load(content)
: JSON.parse(content);

let count: number = loadVariablesFromJson(variables, '', variableSeparator, fileVariables);

Expand Down
4 changes: 2 additions & 2 deletions task/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,11 @@
{
"name": "variableFiles",
"type": "multiLine",
"label": "Variable files (JSON)",
"label": "Variable files (JSON or YAML)",
"defaultValue": "",
"groupName": "advanced",
"required": false,
"helpMarkDown": "Absolute or relative comma or newline-separated paths to the files containing additional variables (wildcards can be used)."
"helpMarkDown": "Absolute or relative comma or newline-separated paths to the files containing additional variables (wildcards can be used). YAML files must have the .yml or .yaml extension otherwise the file is treated as JSON."
},
{
"name": "variableSeparator",
Expand Down

0 comments on commit 16df2cc

Please sign in to comment.