Skip to content

Commit

Permalink
Enable task authors to determine whether the task is running on a hos…
Browse files Browse the repository at this point in the history
…ted agent, or not. (#869)

* Added function getAgentMode

* Function getAgentMode added to documentation

* Fix of documentation
  • Loading branch information
Roman-Shchukin committed Oct 12, 2022
1 parent ee582bc commit 5adadae
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 1 deletion.
14 changes: 14 additions & 0 deletions node/docs/azure-pipelines-task-lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,20 @@
}
]
},
"getAgentMode": {
"name": "getAgentMode",
"members": {},
"documentation": "Gets a agent hosted mode: Unknown, SelfHosted or MsHosted.\nRequires a 2.212.0 agent or higher for full functionality. With lower version returns AgentHostedMode.Unknown value. \n\n@returns AgentHostedMode",
"kind": "function",
"signatures": [
{
"parameters": [],
"members": {},
"return": "AgentHostedMode",
"documentation": "Gets a agent hosted mode: Unknown, SelfHosted or MsHosted.\nRequires a 2.212.0 agent or higher for full functionality. With lower version returns AgentHostedMode.Unknown value. \n\n@returns AgentHostedMode"
}
]
},
"setVariable": {
"name": "setVariable",
"members": {},
Expand Down
13 changes: 13 additions & 0 deletions node/docs/azure-pipelines-task-lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import tl = require('azure-pipelines-task-lib/task')
<a href="#tasksetVariable">setVariable</a> <br/>
<a href="#taskgetTaskVariable">getTaskVariable</a> <br/>
<a href="#tasksetTaskVariable">setTaskVariable</a> <br/>
<a href="#taskgetAgentMode">getAgentMode</a> <br/>

### Execution <a href="#Execution">(v)</a>

Expand Down Expand Up @@ -290,6 +291,18 @@ Limitations on an agent prior to 2.104.1:
getVariables():VariableInfo[]
```

<br/>
<div id="taskgetAgentMode">

### task.getAgentMode <a href="#index">(^)</a>
Gets a agent hosted mode: Unknown, SelfHosted or MsHosted.
Requires a 2.212.0 agent or higher for full functionality. With lower version returns AgentHostedMode.Unknown value.

@returns AgentHostedMode
```javascript
getAgentMode():AgentHostedMode
```

<br/>
<div id="tasksetVariable">

Expand Down
3 changes: 2 additions & 1 deletion node/docs/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"task.getVariables",
"task.setVariable",
"task.getTaskVariable",
"task.setTaskVariable"
"task.setTaskVariable",
"task.getAgentMode"
]
},
"Execution": {
Expand Down
1 change: 1 addition & 0 deletions node/mock-answer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface TaskLibAnswers {
find?: { [key: string]: string[] },
findMatch?: { [key: string]: string[] },
getPlatform?: { [key: string]: task.Platform },
getAgentMode?: { [key: string]: task.AgentHostedMode },
legacyFindFiles?: { [key: string]: string[] },
ls?: { [key: string]: string },
osType?: { [key: string]: string },
Expand Down
4 changes: 4 additions & 0 deletions node/mock-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ export function getPlatform(): task.Platform {
return mock.getResponse('getPlatform', 'getPlatform', module.exports.debug);
}

export function getAgentMode(): task.AgentHostedMode {
return mock.getResponse('getAgentMode', 'getAgentMode', module.exports.debug);
}

export function cwd(): string {
return mock.getResponse('cwd', 'cwd', module.exports.debug);
}
Expand Down
22 changes: 22 additions & 0 deletions node/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ export enum Platform {
Linux
}

export enum AgentHostedMode {
Unknown,
SelfHosted,
MsHosted
}

//-----------------------------------------------------
// General Helpers
//-----------------------------------------------------
Expand Down Expand Up @@ -659,6 +665,22 @@ export function getPlatform(): Platform {
}
}

/**
* Return hosted type of Agent
* @returns {AgentHostedMode}
*/
export function getAgentMode(): AgentHostedMode {
let agentCloudId = getVariable('Agent.CloudId');

if (agentCloudId === undefined)
return AgentHostedMode.Unknown;

if (agentCloudId)
return AgentHostedMode.MsHosted;

return AgentHostedMode.SelfHosted;
}

/**
* Returns the process's current working directory.
* see [process.cwd](https://nodejs.org/api/process.html#process_process_cwd)
Expand Down

0 comments on commit 5adadae

Please sign in to comment.