|
| 1 | +parameters: |
| 2 | + helixSource: 'undefined_defaulted_in_telemetry.yml' |
| 3 | + helixType: 'undefined_defaulted_in_telemetry.yml' |
| 4 | + buildConfig: '' |
| 5 | + runAsPublic: false |
| 6 | + maxRetries: 5 |
| 7 | + retryDelay: 10 # in seconds |
| 8 | + |
| 9 | +steps: |
| 10 | +- ${{ if and(eq(parameters.runAsPublic, 'false'), not(eq(variables['System.TeamProject'], 'public'))) }}: |
| 11 | + - task: AzureKeyVault@2 |
| 12 | + inputs: |
| 13 | + azureSubscription: 'HelixProd_KeyVault' |
| 14 | + KeyVaultName: HelixProdKV |
| 15 | + SecretsFilter: 'HelixApiAccessToken' |
| 16 | + condition: always() |
| 17 | +- bash: | |
| 18 | + # create a temporary file |
| 19 | + jobInfo=`mktemp` |
| 20 | +
|
| 21 | + # write job info content to temporary file |
| 22 | + cat > $jobInfo <<JobListStuff |
| 23 | + { |
| 24 | + "QueueId": "$QueueId", |
| 25 | + "Source": "$Source", |
| 26 | + "Type": "$Type", |
| 27 | + "Build": "$Build", |
| 28 | + "Attempt": "$Attempt", |
| 29 | + "Properties": { |
| 30 | + "operatingSystem": "$OperatingSystem", |
| 31 | + "configuration": "$Configuration" |
| 32 | + } |
| 33 | + } |
| 34 | + JobListStuff |
| 35 | +
|
| 36 | + cat $jobInfo |
| 37 | +
|
| 38 | + # create a temporary file for curl output |
| 39 | + res=`mktemp` |
| 40 | +
|
| 41 | + accessTokenParameter="?access_token=$HelixApiAccessToken" |
| 42 | +
|
| 43 | + curlStatus=1 |
| 44 | + retryCount=0 |
| 45 | + # retry loop to harden against spotty telemetry connections |
| 46 | + # we don't retry successes and 4xx client errors |
| 47 | + until [[ $curlStatus -eq 0 || ( $curlStatus -ge 400 && $curlStatus -le 499 ) || $retryCount -ge $MaxRetries ]] |
| 48 | + do |
| 49 | + if [ $retryCount -gt 0 ]; then |
| 50 | + echo "Failed to send telemetry to Helix; waiting $RetryDelay seconds before retrying..." |
| 51 | + sleep $RetryDelay |
| 52 | + fi |
| 53 | +
|
| 54 | + curlResult=` |
| 55 | + cat $jobInfo |\ |
| 56 | + curl --trace - --verbose --output $res --write-out "%{http_code}" \ |
| 57 | + -H 'Content-Type: application/json' \ |
| 58 | + -X POST "https://helix.dot.net/api/2018-03-14/telemetry/job$accessTokenParameter" -d @-` |
| 59 | + curlStatus=$? |
| 60 | +
|
| 61 | + if [ $curlStatus -eq 0 ]; then |
| 62 | + if [ $curlResult -gt 299 ] || [ $curlResult -lt 200 ]; then |
| 63 | + curlStatus=$curlResult |
| 64 | + fi |
| 65 | + fi |
| 66 | +
|
| 67 | + let retryCount++ |
| 68 | + done |
| 69 | +
|
| 70 | + curlResult=`cat $res` |
| 71 | +
|
| 72 | + # validate status of curl command |
| 73 | + if [ $curlStatus -ne 0 ]; then |
| 74 | + echo "Failed To Send Job Start information after $retryCount retries" |
| 75 | + # We have to append the ## vso prefix or vso will pick up the command when it dumps the inline script into the shell |
| 76 | + vstsLogOutput="vso[task.logissue type=error;sourcepath=telemetry/start-job.sh;code=1;]Failed to Send Job Start information: $curlStatus" |
| 77 | + echo "##$vstsLogOutput" |
| 78 | + exit 1 |
| 79 | + fi |
| 80 | +
|
| 81 | + # Set the Helix_JobToken variable |
| 82 | + export Helix_JobToken=`echo $curlResult | xargs echo` # Strip Quotes |
| 83 | + echo "##vso[task.setvariable variable=Helix_JobToken;issecret=true;]$Helix_JobToken" |
| 84 | + displayName: Send Unix Job Start Telemetry |
| 85 | + env: |
| 86 | + HelixApiAccessToken: $(HelixApiAccessToken) |
| 87 | + Source: ${{ parameters.helixSource }} |
| 88 | + Type: ${{ parameters.helixType }} |
| 89 | + Build: $(Build.BuildNumber) |
| 90 | + QueueId: $(Agent.Os) |
| 91 | + Attempt: 1 |
| 92 | + OperatingSystem: $(Agent.Os) |
| 93 | + Configuration: ${{ parameters.buildConfig }} |
| 94 | + MaxRetries: ${{ parameters.maxRetries }} |
| 95 | + RetryDelay: ${{ parameters.retryDelay }} |
| 96 | + condition: and(always(), ne(variables['Agent.Os'], 'Windows_NT')) |
| 97 | +- bash: | |
| 98 | + curlStatus=1 |
| 99 | + retryCount=0 |
| 100 | + # retry loop to harden against spotty telemetry connections |
| 101 | + # we don't retry successes and 4xx client errors |
| 102 | + until [[ $curlStatus -eq 0 || ( $curlStatus -ge 400 && $curlStatus -le 499 ) || $retryCount -ge $MaxRetries ]] |
| 103 | + do |
| 104 | + if [ $retryCount -gt 0 ]; then |
| 105 | + echo "Failed to send telemetry to Helix; waiting $RetryDelay seconds before retrying..." |
| 106 | + sleep $RetryDelay |
| 107 | + fi |
| 108 | +
|
| 109 | + res=`mktemp` |
| 110 | + curlResult=` |
| 111 | + curl --verbose --output $res --write-out "%{http_code}"\ |
| 112 | + -H 'Content-Type: application/json' \ |
| 113 | + -H "X-Helix-Job-Token: $Helix_JobToken" \ |
| 114 | + -H 'Content-Length: 0' \ |
| 115 | + -X POST -G "https://helix.dot.net/api/2018-03-14/telemetry/job/build" \ |
| 116 | + --data-urlencode "buildUri=$BuildUri"` |
| 117 | + curlStatus=$? |
| 118 | +
|
| 119 | + if [ $curlStatus -eq 0 ]; then |
| 120 | + if [ $curlResult -gt 299 ] || [ $curlResult -lt 200 ]; then |
| 121 | + curlStatus=$curlResult |
| 122 | + fi |
| 123 | + fi |
| 124 | +
|
| 125 | + curlResult=`cat $res` |
| 126 | + let retryCount++ |
| 127 | + done |
| 128 | +
|
| 129 | + # validate status of curl command |
| 130 | + if [ $curlStatus -ne 0 ]; then |
| 131 | + echo "Failed to Send Build Start information after $retryCount retries" |
| 132 | + vstsLogOutput="vso[task.logissue type=error;sourcepath=telemetry/build/start.sh;code=1;]Failed to Send Build Start information: $curlStatus" |
| 133 | + echo "##$vstsLogOutput" |
| 134 | + exit 1 |
| 135 | + fi |
| 136 | +
|
| 137 | + export Helix_WorkItemId=`echo $curlResult | xargs echo` # Strip Quotes |
| 138 | + echo "##vso[task.setvariable variable=Helix_WorkItemId]$Helix_WorkItemId" |
| 139 | + displayName: Send Unix Build Start Telemetry |
| 140 | + env: |
| 141 | + BuildUri: $(System.TaskDefinitionsUri)$(System.TeamProject)/_build/index?buildId=$(Build.BuildId)&_a=summary |
| 142 | + Helix_JobToken: $(Helix_JobToken) |
| 143 | + MaxRetries: ${{ parameters.maxRetries }} |
| 144 | + RetryDelay: ${{ parameters.retryDelay }} |
| 145 | + condition: and(always(), ne(variables['Agent.Os'], 'Windows_NT')) |
| 146 | + |
| 147 | +- powershell: | |
| 148 | + $jobInfo = [pscustomobject]@{ |
| 149 | + QueueId=$env:QueueId; |
| 150 | + Source=$env:Source; |
| 151 | + Type=$env:Type; |
| 152 | + Build=$env:Build; |
| 153 | + Attempt=$env:Attempt; |
| 154 | + Properties=[pscustomobject]@{ operatingSystem=$env:OperatingSystem; configuration=$env:Configuration }; |
| 155 | + } |
| 156 | +
|
| 157 | + $jobInfoJson = $jobInfo | ConvertTo-Json |
| 158 | +
|
| 159 | + if ($env:HelixApiAccessToken) { |
| 160 | + $accessTokenParameter="?access_token=$($env:HelixApiAccessToken)" |
| 161 | + } |
| 162 | + Write-Host "Job Info: $jobInfoJson" |
| 163 | +
|
| 164 | + # Basic retry loop to harden against server flakiness |
| 165 | + $retryCount = 0 |
| 166 | + while ($retryCount -lt $env:MaxRetries) { |
| 167 | + try { |
| 168 | + $jobToken = Invoke-RestMethod -Uri "https://helix.dot.net/api/2018-03-14/telemetry/job$($accessTokenParameter)" -Method Post -ContentType "application/json" -Body $jobInfoJson |
| 169 | + break |
| 170 | + } |
| 171 | + catch { |
| 172 | + $statusCode = $_.Exception.Response.StatusCode.value__ |
| 173 | + if ($statusCode -ge 400 -and $statusCode -le 499) { |
| 174 | + Write-Host "##vso[task.logissue]error Failed to send telemetry to Helix (status code $statusCode); not retrying (4xx client error)" |
| 175 | + Write-Host "##vso[task.logissue]error ", $_.Exception.GetType().FullName, $_.Exception.Message |
| 176 | + exit 1 |
| 177 | + } |
| 178 | + Write-Host "Failed to send telemetry to Helix (status code $statusCode); waiting $env:RetryDelay seconds before retrying..." |
| 179 | + $retryCount++ |
| 180 | + sleep $env:RetryDelay |
| 181 | + continue |
| 182 | + } |
| 183 | + } |
| 184 | +
|
| 185 | + if ($retryCount -ge $env:MaxRetries) { |
| 186 | + Write-Host "##vso[task.logissue]error Failed to send telemetry to Helix after $retryCount retries." |
| 187 | + exit 1 |
| 188 | + } |
| 189 | +
|
| 190 | + $env:Helix_JobToken = $jobToken |
| 191 | + Write-Host "##vso[task.setvariable variable=Helix_JobToken;issecret=true;]$env:Helix_JobToken" |
| 192 | + env: |
| 193 | + HelixApiAccessToken: $(HelixApiAccessToken) |
| 194 | + Source: ${{ parameters.helixSource }} |
| 195 | + Type: ${{ parameters.helixType }} |
| 196 | + Build: $(Build.BuildNumber) |
| 197 | + QueueId: $(Agent.Os) |
| 198 | + Attempt: 1 |
| 199 | + OperatingSystem: $(Agent.Os) |
| 200 | + Configuration: ${{ parameters.buildConfig }} |
| 201 | + MaxRetries: ${{ parameters.maxRetries }} |
| 202 | + RetryDelay: ${{ parameters.retryDelay }} |
| 203 | + condition: and(always(), eq(variables['Agent.Os'], 'Windows_NT')) |
| 204 | + displayName: Send Windows Job Start Telemetry |
| 205 | +- powershell: | |
| 206 | + # Basic retry loop to harden against server flakiness |
| 207 | + $retryCount = 0 |
| 208 | + while ($retryCount -lt $env:MaxRetries) { |
| 209 | + try { |
| 210 | + $workItemId = Invoke-RestMethod -Uri "https://helix.dot.net/api/2018-03-14/telemetry/job/build?buildUri=$([Net.WebUtility]::UrlEncode($env:BuildUri))" -Method Post -ContentType "application/json" -Body "" ` |
| 211 | + -Headers @{ 'X-Helix-Job-Token'=$env:Helix_JobToken } |
| 212 | + break |
| 213 | + } |
| 214 | + catch { |
| 215 | + $statusCode = $_.Exception.Response.StatusCode.value__ |
| 216 | + if ($statusCode -ge 400 -and $statusCode -le 499) { |
| 217 | + Write-Host "##vso[task.logissue]error Failed to send telemetry to Helix (status code $statusCode); not retrying (4xx client error)" |
| 218 | + Write-Host "##vso[task.logissue]error ", $_.Exception.GetType().FullName, $_.Exception.Message |
| 219 | + exit 1 |
| 220 | + } |
| 221 | + Write-Host "Failed to send telemetry to Helix (status code $statusCode); waiting $env:RetryDelay seconds before retrying..." |
| 222 | + $retryCount++ |
| 223 | + sleep $env:RetryDelay |
| 224 | + continue |
| 225 | + } |
| 226 | + } |
| 227 | +
|
| 228 | + if ($retryCount -ge $env:MaxRetries) { |
| 229 | + Write-Host "##vso[task.logissue]error Failed to send telemetry to Helix after $retryCount retries." |
| 230 | + exit 1 |
| 231 | + } |
| 232 | +
|
| 233 | + $env:Helix_WorkItemId = $workItemId |
| 234 | + Write-Host "##vso[task.setvariable variable=Helix_WorkItemId]$env:Helix_WorkItemId" |
| 235 | + displayName: Send Windows Build Start Telemetry |
| 236 | + env: |
| 237 | + BuildUri: $(System.TaskDefinitionsUri)$(System.TeamProject)/_build/index?buildId=$(Build.BuildId)&_a=summary |
| 238 | + Helix_JobToken: $(Helix_JobToken) |
| 239 | + MaxRetries: ${{ parameters.maxRetries }} |
| 240 | + RetryDelay: ${{ parameters.retryDelay }} |
| 241 | + condition: and(always(), eq(variables['Agent.Os'], 'Windows_NT')) |
0 commit comments