How can we print the user id in the teams channel using azure devops pipeline?

117 Views Asked by At

I have a requirement to print user id (CI triggered user id) in the teams channel as soon the files updated in the GCP buckets & the azure CI triggered.

Can someone please suggest me how can I proceed with.

Please find the code below which I have tried:

def get_triggered_user_id():
    organization_url = "https://dev.azure.com/{organisation_name}/"
    project_name = os.environ.get('ComponentName')
    build_id = os.environ.get('BUILD_BUILDID')  # Assuming this environment variable is available

    api_url = f"{organization_url}{project_name}/_apis/build/builds/{build_id}?api-version=6.0"
    headers = {
        'Authorization': f'Basic {os.environ["SYSTEM_ACCESSTOKEN"]}'  ##Here the id is not generating in my logs in devops pipeline
    }

    response = requests.get(api_url, headers=headers)
    response.raise_for_status()

    triggering_user_id = response.json().get('requestedFor').get('id')
    return triggering_user_id
1

There are 1 best solutions below

6
On

Based on your requirement, you need to print the user id to the Microsoft teams channel.

To meet your requirement, you can use the Incoming Webhook in Microsoft Teams.

Here are the steps:

1.Create a webhook in Microsoft Teams -> Channel

(a) Navigate to the channel where you want to add the webhook and select (•••) Connectors.

(b) Search for Incoming Webhook, and add it.

(c) Click Configure and provide a name for your webhook.

(d) Copy the Webhook URL

2.We can install pymsteams to connect the webhook.

Install pymsteams

pip install pymsteams

3.Use the following python script sample

import pymsteams
myTeamsMessage = pymsteams.connectorcard("<Microsoft Webhook URL>")
myTeamsMessage.text("useridinfo")
myTeamsMessage.send()

For more detailed info, you can refer to this doc: Teams incoming Webhook

On the other hand, you can also consider directly using the Azure Pipelines Teams Integration to connect to Azure DevOps in MS teams.