diff --git a/azure-pipelines-full-tests.yml b/azure-pipelines-full-tests.yml index d7566281d40..33980164884 100644 --- a/azure-pipelines-full-tests.yml +++ b/azure-pipelines-full-tests.yml @@ -140,3 +140,37 @@ jobs: instance_idx: '$(Instance_idx)' fullTest: true jobName: 'FullTest' + +- job: NotifyCIErrors + dependsOn: + - AutomationTest20200901 + - AutomationTest20190301 + - AutomationTest20180301 + - AutomationFullTestPython39ProfileLatest + - AutomationFullTestPython310ProfileLatest + condition: and(failed(), in(variables['Build.Reason'], 'BatchedCI')) + displayName: Notify CI Errors + pool: + name: ${{ variables.ubuntu_pool }} + steps: + - task: UsePythonVersion@0 + displayName: 'Use Python 3.10' + inputs: + versionSpec: 3.10 + - task: AzureCLI@2 + inputs: + azureSubscription: 'Azure CLI' + scriptType: 'bash' + scriptLocation: 'inlineScript' + inlineScript: | + pip install requests + teams_api_url=$(az keyvault secret show --vault-name $(TEAMS_BOT_VAULT_NAME) --name $(TEAMS_BOT_API_URL_SECRET_NAME) --query value -otsv) + teams_api_key=$(az keyvault secret show --vault-name $(TEAMS_BOT_VAULT_NAME) --name $(TEAMS_BOT_API_KEY_SECRET_NAME) --query value -otsv) + echo "If any task fails, notify to teams channel" + python scripts/ci/notify_ci_errors.py $teams_api_url $teams_api_key $(TEAMS_CHANNEL_ID) + displayName: 'Notify To Teams Channel' + env: + BASE_URI: $(System.CollectionUri) + PROJECT_TYPE: $(System.TeamProject) + BUILD_ID: $(Build.BuildId) + JOB_ID: $(System.JobId) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 9b372a3975a..4c0fd304cd7 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -1087,3 +1087,65 @@ jobs: displayName: "Install pip and wheel" - bash: ./scripts/ci/test_ref_doc.sh displayName: "Verify Sphinx Document Generator" + +- job: NotifyCIErrors + dependsOn: + - CheckPullRequest + - RejectPullRequestToMasterBranch + - CredentialScanner + - PolicyCheck + - ExtractMetadata + - VerifyLinuxRequirements + - VerifyDarwinRequirements + - VerifyWindowsRequirements + - VerifyVersions + - BuildWindowsMSI + - TestWindowsMSI + - BuildDockerImage + - TestDockerImage + - BuildPythonWheel + - TestPythonWheel + - TestCore + - TestTelemetry + - IntegrationTestAgainstProfiles + - TestExtensionsLoading + - BuildHomebrewFormula + - TestHomebrewFormula + - TestHomebrewPackage + - BuildRpmPackageMariner + - BuildRpmPackages + - TestRpmPackage + - BuildDebPackages + - TestDebPackages + - CheckStyle + - CheckHeaders + - PerformanceCheck + - CheckLinter + - CodegenCoverage + - VerifySphinxDocumentGenerator + condition: and(failed(), in(variables['Build.Reason'], 'BatchedCI')) + displayName: Notify CI Errors + pool: + name: ${{ variables.ubuntu_pool }} + steps: + - task: UsePythonVersion@0 + displayName: 'Use Python 3.10' + inputs: + versionSpec: 3.10 + - task: AzureCLI@2 + inputs: + azureSubscription: 'Azure CLI' + scriptType: 'bash' + scriptLocation: 'inlineScript' + inlineScript: | + pip install requests + teams_api_url=$(az keyvault secret show --vault-name $(TEAMS_BOT_VAULT_NAME) --name $(TEAMS_BOT_API_URL_SECRET_NAME) --query value -otsv) + teams_api_key=$(az keyvault secret show --vault-name $(TEAMS_BOT_VAULT_NAME) --name $(TEAMS_BOT_API_KEY_SECRET_NAME) --query value -otsv) + echo "If any task fails, notify to teams channel" + python scripts/ci/notify_ci_errors.py $teams_api_url $teams_api_key $(TEAMS_CHANNEL_ID) + displayName: 'Notify To Teams Channel' + env: + BASE_URI: $(System.CollectionUri) + PROJECT_TYPE: $(System.TeamProject) + BUILD_ID: $(Build.BuildId) + JOB_ID: $(System.JobId) diff --git a/scripts/ci/notify_ci_errors.py b/scripts/ci/notify_ci_errors.py new file mode 100644 index 00000000000..5cc96bf8c9d --- /dev/null +++ b/scripts/ci/notify_ci_errors.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +# -------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. +# -------------------------------------------------------------------------------------------- + +import logging +import os +import requests +import sys + + +logger = logging.getLogger(__name__) +logger.setLevel(logging.DEBUG) +ch = logging.StreamHandler() +ch.setLevel(logging.DEBUG) +logger.addHandler(ch) + + +teams_api_url = sys.argv[1] +teams_api_key = sys.argv[2] +teams_channel_id = sys.argv[3] +# https://dev.azure.com/azclitools/ +base_uri = os.environ.get('BASE_URI', False) +# public +project_type = os.environ.get('PROJECT_TYPE', False) +# 45514 +build_id = os.environ.get('BUILD_ID', False) +# 15eab87b-4a33-5480-11eb-66f5d5b3681b +job_id = os.environ.get('JOB_ID', False) + + +def notify_batch_ci_errors(): + if all([base_uri, project_type, build_id, job_id]): + # https://dev.azure.com/azclitools/public/_build/results?buildId=45514&view=logs&j=15eab87b-4a33-5480-11eb-66f5d5b3681b + url = f'{base_uri}{project_type}/_build/results?buildId={build_id}&view=logs&j={job_id}' + + data = { + "title": "Batch CI Error Appears!", + "body": "Azure cli team,\n\nPlease click to take a look at the batch CI error.", + "notificationUrl": url, + "targetType": "channel", + "recipients": teams_channel_id + } + headers = { + 'x-api-key': teams_api_key + } + + response = requests.request("POST", teams_api_url, headers=headers, data=data) + logger.debug('Status Code: %s', response.status_code) + logger.debug('Response Content: %s', response.content) + else: + logger.error('Missing variables: \nBASE_URI: %s, PROJECT_TYPE: %s, ' + 'BUILD_ID: %s, JOB_ID: %s', base_uri, project_type, build_id, job_id) + + +if __name__ == '__main__': + notify_batch_ci_errors()