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

{CI} Notify batch CI errors to teams channel #26068

Merged
merged 6 commits into from
Apr 12, 2023
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
34 changes: 34 additions & 0 deletions azure-pipelines-full-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
62 changes: 62 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
59 changes: 59 additions & 0 deletions scripts/ci/notify_ci_errors.py
Original file line number Diff line number Diff line change
@@ -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()