问题
I'm using VSTS to automatically release my app after each commit to the specific branch. The tasks are basically:
- Recreate database
- If 1st ok -> Deploy API and Web
- If 2nd ok -> Deploy and restart some windows services
During the second step sometimes it happens that deploy fails because of some connection or IIS issue. This is not really
a big deal I do not want to be very specific about the error nature here. All I have to do when it happens is to go to the VSTS
and click Redeploy
on failed task.
But I was wondering if there is a way to automate that process. So in case of fail deployment and specific error I want to trigger
Redeploy
automatically. Is there a way to do this in VSTS? Any ideas how can I achieve this?
回答1:
There have two options which can helps you redeploy the second task/environment.
Option 1: add the same tasks as the second task and execute the following tasks when previous task is failed
If you want to reploy three times, you can add the same three tasks as the second task in second environment. For the three tasks you add, set only when a previous task has failed for Run this task option. So it can redeploy the second task/environment not bigger than three times.
Option 2: add a PowerShell task after second task in second environment, if the second task failed, deploy the second environment again
In the PowerShell task, you should do below operations.
1.Get the current release log by rest API:
GET https://account.vsrm.visualstudio.com/Git2/_apis/Release/releases/{releaseID}
For the releaseID, you can get the value by predefined variable $(Release.ReleaseId)
.
2.Get second task status
In the rest API response, you can check your second task by searching the task display name and get the status
for the task. As below example, the task display name is PowerShell Script
, and the task is failed (status
value is failed
).
{
"id": 5,
"timelineRecordId": "ae95a8be-6259-466d-ba8d-93711a922237",
"name": "PowerShell Script",
"dateStarted": "2017-10-03T02:43:25.757Z",
"dateEnded": "2017-10-03T02:43:29.073Z",
"startTime": "2017-10-03T02:43:25.757Z",
"finishTime": "2017-10-03T02:43:29.073Z",
"status": "failed",
"rank": 4,
"issues": [
{
"issueType": "Error",
"message": "agit : The term 'agit' is not recognized as the name of a cmdlet, function, script file, or operable program. Check \r\nthe spelling of the name, or if a path was included, verify that the path is correct and try again.\r\nAt D:\\a\\_temp\\0a858f5c-894b-4944-bed4-54b3cbed48bc.ps1:1 char:1\r\n+ agit\r\n+ ~~~~\r\n + CategoryInfo : ObjectNotFound: (agit:String) [], CommandNotFoundException\r\n + FullyQualifiedErrorId : CommandNotFoundException\r\n \r\n"
},
{
"issueType": "Error",
"message": "Process completed with exit code 0 and had 1 error(s) written to the error stream."
}
],
"task": {
"id": "e213ff0f-5d5c-4791-802d-52ea3e7be1f1",
"name": "PowerShell",
"version": "1.2.3"
},
"agentName": "Hosted Agent",
"logUrl": "https://account.vsrm.visualstudio.com/f7855e29-6f8d-429d-8c9b-41fd4d7e70a4/_apis/Release/releases/300/environments/374/tasks/5/logs?releaseDeployPhaseId=344"
}
3.If the second task is failed, then deploy the second environment again by rest API:
PATCH https://account.vsrm.visualstudio.com/{project}/_apis/Release/releases/{releaseID}/environments/{environmentID}?api-version=4.0-preview.4
For the environmentID, you can also get in by perdefined variable $(Release.EnvironmentId)
.
来源:https://stackoverflow.com/questions/46520427/automatic-redeploy-after-fail