Skip to content
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

Enable task authors to determine whether the task is running on a hosted agent, or not. #869

Merged
Merged
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
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to check MsHosted and SelfHosted and for any other case just return unknown

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be a good idea, but here is a little bit of another logic. "undefined" value means that the current agent version doesn't support Agent.CloudId, it's for backward compatibility. Any other empty values mean it's SelfHosted and real value means MsHosted

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