Skip to content

Add current date variable support #631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
37 changes: 35 additions & 2 deletions vsts-variable-set/v1/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -77,4 +110,4 @@
"argumentFormat": ""
}
}
}
}
31 changes: 30 additions & 1 deletion vsts-variable-set/v1/vsts-variable-set.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
35 changes: 34 additions & 1 deletion vsts-variable-set/v2/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,47 @@
"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.",
"label": "Value",
"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,
Expand Down
31 changes: 30 additions & 1 deletion vsts-variable-set/v2/vsts-variable-set.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
12 changes: 11 additions & 1 deletion vsts-variable-set/v3/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"type": "pickList",
"options": {
"value": "value",
"env": "env"
"env": "env",
"currentDate": "currentDate"
}
},
{
Expand All @@ -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.",
Expand Down
18 changes: 18 additions & 0 deletions vsts-variable-set/v3/vsts-variable-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -15,6 +29,10 @@ function getValue()
{
return process.env[tl.getInput("Env", true)];
}
case "currentDate":
{
return getCurrentDate(tl.getInput("DateFormat", true));
}
default:
{
return "";
Expand Down