diff --git a/README.md b/README.md index ec744153..37e8960f 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,15 @@ You can use the value of other build variables to setup the value. By assigning to the `Build.BuildNumber` variable, the build number of the Build will be updated/overwritten. +## Set Variable to Current Date +You can now set a variable to the current date with a specified date format. Simply add the **Set Variable** task to your workflow and select "currentDate" as the "From" option. Then specify the desired date format. + +> **Set: 'Build.CurrentDate' to the current date with format 'yyyyMMdd'** +> +> * *Variablename*: `Build.CurrentDate` +> * *From*: `currentDate` +> * *DateFormat*: `yyyyMMdd` + # Transform value and assign to Variable If you need to do more advanced transformations of your values, use the transform task. You can use it to encode/decode the value and apply a number of simpe string manipulations, including Search & Replace, Change Case, Trim, Pad etc. diff --git a/vsts-variable-set/v1/task.json b/vsts-variable-set/v1/task.json index b796b1c4..68edf05a 100644 --- a/vsts-variable-set/v1/task.json +++ b/vsts-variable-set/v1/task.json @@ -33,13 +33,46 @@ "type": "string", "aliases": ["variableName"] }, + { + "defaultValue": "value", + "helpMarkdown": "Take the value from the input or an environment variable.", + "label": "From", + "name": "From", + "required": true, + "type": "pickList", + "options": { + "value": "value", + "env": "env", + "currentDate": "currentDate" + } + }, { "defaultValue": "", "helpMarkdown": "The value to assign to the variable.", "label": "Value", "name": "Value", "required": false, - "type": "string" + "type": "string", + "visibleRule": "From=value" + }, + { + "defaultValue": "", + "helpMarkdown": "The value to assign to the variable.", + "label": "Environment Variable", + "name": "Env", + "required": true, + "type": "string", + "aliases": ["Env", "Environment"], + "visibleRule": "From=env" + }, + { + "defaultValue": "yyyy-MM-dd", + "helpMarkdown": "The date format to use when setting the variable to the current date.", + "label": "Date Format", + "name": "DateFormat", + "required": true, + "type": "string", + "visibleRule": "From=currentDate" }, { "defaultValue": false, @@ -77,4 +110,4 @@ "argumentFormat": "" } } -} \ No newline at end of file +} diff --git a/vsts-variable-set/v1/vsts-variable-set.ts b/vsts-variable-set/v1/vsts-variable-set.ts index 8ca59ea8..e2d0f719 100644 --- a/vsts-variable-set/v1/vsts-variable-set.ts +++ b/vsts-variable-set/v1/vsts-variable-set.ts @@ -1,7 +1,36 @@ import * as tl from "azure-pipelines-task-lib/task"; const variable = tl.getInput("VariableName", true); -const value = tl.getInput("Value"); + +function getCurrentDate(format: string): string { + const date = new Date(); + const options: Intl.DateTimeFormatOptions = { + year: 'numeric', + month: '2-digit', + day: '2-digit', + hour: '2-digit', + minute: '2-digit', + second: '2-digit', + hour12: false + }; + return new Intl.DateTimeFormat('default', options).format(date).replace(/\//g, '-').replace(/, /g, ' ').replace(/:/g, '-'); +} + +function getValue() { + const from = tl.getInput("From") || "value"; + switch (from) { + case "value": + return tl.getInput("Value"); + case "env": + return process.env[tl.getInput("Env", true)]; + case "currentDate": + return getCurrentDate(tl.getInput("DateFormat", true)); + default: + return ""; + } +} + +const value = getValue(); const isSecret = tl.getBoolInput("isSecret") || false; const useTaskLib = tl.getBoolInput("useTasklib") || false; diff --git a/vsts-variable-set/v2/task.json b/vsts-variable-set/v2/task.json index 60798494..116c2489 100644 --- a/vsts-variable-set/v2/task.json +++ b/vsts-variable-set/v2/task.json @@ -50,6 +50,19 @@ "required": false, "visibleRule": "VariableName=release.releasename" }, + { + "defaultValue": "value", + "helpMarkdown": "Take the value from the input or an environment variable.", + "label": "From", + "name": "From", + "required": true, + "type": "pickList", + "options": { + "value": "value", + "env": "env", + "currentDate": "currentDate" + } + }, { "defaultValue": "", "helpMarkdown": "The value to assign to the variable.", @@ -57,7 +70,27 @@ "name": "Value", "required": false, "type": "string", - "aliases": ["value"] + "aliases": ["value"], + "visibleRule": "From=value" + }, + { + "defaultValue": "", + "helpMarkdown": "The value to assign to the variable.", + "label": "Environment Variable", + "name": "Env", + "required": true, + "type": "string", + "aliases": ["Env", "Environment"], + "visibleRule": "From=env" + }, + { + "defaultValue": "yyyy-MM-dd", + "helpMarkdown": "The date format to use when setting the variable to the current date.", + "label": "Date Format", + "name": "DateFormat", + "required": true, + "type": "string", + "visibleRule": "From=currentDate" }, { "defaultValue": false, diff --git a/vsts-variable-set/v2/vsts-variable-set.ts b/vsts-variable-set/v2/vsts-variable-set.ts index 6e724b18..2928284c 100644 --- a/vsts-variable-set/v2/vsts-variable-set.ts +++ b/vsts-variable-set/v2/vsts-variable-set.ts @@ -1,7 +1,36 @@ import * as tl from "azure-pipelines-task-lib/task"; const variable = tl.getInput("VariableName", true); -const value = tl.getInput("Value"); + +function getCurrentDate(format: string): string { + const date = new Date(); + const options: Intl.DateTimeFormatOptions = { + year: 'numeric', + month: '2-digit', + day: '2-digit', + hour: '2-digit', + minute: '2-digit', + second: '2-digit', + hour12: false + }; + return new Intl.DateTimeFormat('default', options).format(date).replace(/\//g, '-').replace(/, /g, ' ').replace(/:/g, '-'); +} + +function getValue() { + const from = tl.getInput("From") || "value"; + switch (from) { + case "value": + return tl.getInput("Value"); + case "env": + return process.env[tl.getInput("Env", true)]; + case "currentDate": + return getCurrentDate(tl.getInput("DateFormat", true)); + default: + return ""; + } +} + +const value = getValue(); const isSecret = tl.getBoolInput("isSecret") || false; const useTaskLib = tl.getBoolInput("useTasklib") || false; const useSetVariableForReleaseName = tl.getBoolInput("useSetVariableForReleaseName") || false; diff --git a/vsts-variable-set/v3/task.json b/vsts-variable-set/v3/task.json index a99f5bb5..c09ae412 100644 --- a/vsts-variable-set/v3/task.json +++ b/vsts-variable-set/v3/task.json @@ -59,7 +59,8 @@ "type": "pickList", "options": { "value": "value", - "env": "env" + "env": "env", + "currentDate": "currentDate" } }, { @@ -82,6 +83,15 @@ "aliases": ["Env", "Environment"], "visibleRule": "From=env" }, + { + "defaultValue": "yyyy-MM-dd", + "helpMarkdown": "The date format to use when setting the variable to the current date.", + "label": "Date Format", + "name": "DateFormat", + "required": true, + "type": "string", + "visibleRule": "From=currentDate" + }, { "defaultValue": false, "helpMarkdown": "Save variable as a secret.", diff --git a/vsts-variable-set/v3/vsts-variable-set.ts b/vsts-variable-set/v3/vsts-variable-set.ts index 85b4a15b..0ba7321f 100644 --- a/vsts-variable-set/v3/vsts-variable-set.ts +++ b/vsts-variable-set/v3/vsts-variable-set.ts @@ -2,6 +2,20 @@ const variable = tl.getInput("VariableName", true); +function getCurrentDate(format: string): string { + const date = new Date(); + const options: Intl.DateTimeFormatOptions = { + year: 'numeric', + month: '2-digit', + day: '2-digit', + hour: '2-digit', + minute: '2-digit', + second: '2-digit', + hour12: false + }; + return new Intl.DateTimeFormat('default', options).format(date).replace(/\//g, '-').replace(/, /g, ' ').replace(/:/g, '-'); +} + function getValue() { const from = tl.getInput("From") || "value"; @@ -15,6 +29,10 @@ function getValue() { return process.env[tl.getInput("Env", true)]; } + case "currentDate": + { + return getCurrentDate(tl.getInput("DateFormat", true)); + } default: { return "";