Code Stream Aria Automation Cloud Automation Cloud Management Platform DevOps vRealize vRealize Code Stream vRealize Suite

vRealize Automation: Posting Messages from Code Stream to MS Teams

Posting messages to services such as Slack or MS Teams, is quite easy with Custom Integrations in vRealize Automation Code Stream. With this use case, I’ll demonstrate how you can vastly improve your team’s collaboration capabilities when it comes to consuming CI/CD Pipelines for end-to-end orchestration.

For those already familiar with Code Stream, you’ll likely recall that Pipeline Stages already have a Notification capability that can send messages via email or post to a Slack channel. There is even a sample Custom Integration for posting messages to a Slack channel via Webhook.

But, what if your use case requires posting messages to another collaboration tool, such as MS Teams? This is extremely easy to accomplish with a Python module and just a few lines of code!

Requirements

The prerequisites you’ll need to set up this use case for yourself are:

  • A Deployed instance of vRA 8.1 licensed with Enterprise Edition or valid subscription to vRA Cloud.
  • A Docker Host deployed and configured as an endpoint in Code Stream.
  • An Incoming Webhook for MS Teams. Follow this link for instructions.

Docker Container

On your Docker Host, create a Builder Image for your pipeline’s Workspace in Code Stream. A Builder Image is a container image having all the necessary runtimes for building other objects. Typically, these objects are binaries compiled from specific build environments, such as Maven or Spring for Java. The best practice for these builder images is to be as small and lightweight as possible to avoid undue pipeline execution delays due to a bloated container. 

For our use case, we only need a Python runtime along with an MS Teams wrapper module.

Step 1. Create a Dockerfile for our builder image. Here’s a sample:

FROM python:3.5
ENV PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin
CMD ["python"]

Step 2. Build the Docker image with the following command:

# docker build -t setilab/pybuildr:1.1 .

Step 3. Push this new image to your Docker repo:

# docker push setilab/pybuildr:1.1

Code Stream Custom Integration

Now that we have a builder image, we need to create a Custom Integration within Code Stream.

Step 1. Login to vRealize Automation and launch the Code Stream service, and navigate to Custom Integrations.

Step 2. Click the Add Custom Integration button and enter the required information at the prompts:

Step 3. Click the Create button.

Step 4. In the editor pane, modify the example code to reflect the following:

---
runtime: "python3"
code: |
    import requests
    import json
    import pymsteams

    # context.py is automatically added.
    from context import getInput

    # Get the values from the declared inputs
    message = getInput('message')
    title = getInput('title')

    # You must create the connectorcard object with the Microsoft Webhook URL
    myTeamsMessage = pymsteams.connectorcard("<MS Teams Webhook URL>")

    # Add text to the message.
    myTeamsMessage.text(message) 

    # Add an (optional) title for this message
    myTeamsMessage.title(title)

    # Send the message.
    myTeamsMessage.send()

inputProperties:      # Enter fields for input section of a task
    - name: message
      type: text
      title: Message
      placeHolder: Message for MSTeams Channel
      defaultValue: Hello MS Teams
      bindable: true
      labelInfo: true
      labelMessage: This message would be posted to the MSTeams channel
    - name: title
      type: text
      title: Title
      placeHolder: Message Title for MSTeams Channel
      defaultValue: Codestream Notification
      bindable: true
      labelInfo: true
      labelMessage: This title would be used for the posted message

Note: Make sure you replace <MS Teams Webhook URL> with the webhook you acquired from the prerequisites.

Notice that I added an input beyond the single default input that allows for setting the message title. Additionally, you can add interactive prompts to a message card so that collaborators in MS Teams can actually take action against the message itself!

Step 5. Save the Custom Integration, and then version it. Versioning it is required to use within a pipeline.

Code Stream Pipeline – Task 1

Once we have defined our Custom Integration, we can use it within a Code Stream pipeline. Create a new Pipeline in Code Stream with a single stage.

Step 1. Select Pipelines from the navigation tree, and then click New Pipeline -> Blank Canvas.

Enter the name, a description (optional), and select the Project this pipeline should belong to.

Step 2. Select the Workspace tab from within your pipeline, and configure appropriately with your Docker Host endpoint and builder image URL.

The only properties that must be entered are the ones marked with a red asterisk. The remaining properties are optional, and more beneficial for other use cases.

Step 3. Add a single stage to your pipeline, and then add a CI task. This first task is where we install the specific Python module for MS Teams. Recall that we’re trying to keep our builder image as small and as generic-purpose as possible, so installing modules during the pipeline execution allows us this capability.

In the Steps pane of our first CI task, install the pymsteams module into our running container image using the following command:

# pip3 install pymsteams

The pymsteams python module provides a wrapper for much easier integration with the MS Teams API, and will save you considerable time. However, you can also forego the module and write direct API calls if you so choose.

Step 3. Enable the new pipeline for execution.

 

Step 4. Run the pipeline to insure it executes successfully without any errors.

Code Stream Pipeline – Task 2

The final step for this use case is to configure a task that actually posts a message to MS Teams.

Step 1. Add a second, sequential task to your pipeline.

Step 2. Configure this task type as Custom, and then choose your Custom Integration object and the appropriate version.

Configure the message-specific inputs per the above example. In the example, I also appended the pipeline variable ${name} so the title distinguishes which pipeline generated the message.

Step 3. Run your pipeline again to test posting messages to your MS Teams channel. Here’s a sample from mine:

Enjoy!

 

For more cool tips & tricks around using Code Stream in vRealize Cloud Automation Services, check out these links: