Creating HTML email templates for vRealize Automation Code Stream

How to create an HTML formatted email template for use in a vRealize Automation Code Stream pipeline

Creating HTML email templates for vRealize Automation Code Stream
Photo by Brett Jordan / Unsplash

I have written several times before about the Packer process that I use to create VM images for my homelab. What I haven't spent as much time on is the pipeline that I use to execute that process. Whilst I'm not going to dig deep in to that now, there was one aspect that I wanted to touch on as I think that it could be useful.

One of the drawbacks to my pipeline and the process in general is that if it fails for some reason it can take some time to diagnose and correct the issue. Partly that is because the pipeline cleans up after itself so that there aren't lots of files left lying around taking up space. To make my life a little easier, I decided to experiment with adding an email notification to my pipeline.

Too many emails can be the bane of your life. But messages that convey information that is important to the reader in a succinct manner are always welcome. Even if they're handled with a filter or rule that archives them, they are at least present somewhere for easy retrieval. In this post I look at how to create a well formatted email to help me understand the result of my Packer pipeline.

BEE Free

BEE Free is an online tool for creating emails and landing pages using a drag and drop editor. I came across it while I was searching for information about creating HTML emails.

BEE Free homepage

Unlike the modern web browser, many email clients still rely on antiquated rendering methods like HTML tables!

surprise!
Photo by Xavi Cabrera / Unsplash

I'd rather spend my time on something more valuable, so having a tool to create a simple framework for an email suits me just fine. The added bonus with BEE Free is that as well as a Pro plan, they let you use the tools for free although anything produced without registering will carry a footer containing some of their branding. For the ease of use however that is a price worth paying.

Of course there are some templates that you can start with, but I chose to use a blank canvas:

Blank email template canvas in BEE Free

Here you can see the "Designed with BEE" footer already present along with drag and drop tools to add sections and content. This post isn't going to be a detailed tutorial on how to build an email in this tool, it's just the one that I chose to use and after a short time I was able to craft what I wanted.

Packer pipeline result email template

It doesn't look like much now, but much of the text will be replaced by Code Stream. Anything starting with a '$' will be replaced by the value in curly braces after it.

Placeholders

You can work out what these placeholders will be based on the names of your pipeline's stages and tasks and the value that you need. For example, let's look at how I get the Packer log file content in the pipeline.

Packer is executed on a remote Linux host and Code Stream uses SSH to connect to it. There is a simple set of commands that the Read Logfile task executes to echo the logfile's contents back to Code Stream.

cd ${executionIndex}/builds/${input.packer_build}
while IFS= read -r line || [ -n "$line" ]
do
  echo "$line<br />" >> $SCRIPT_RESPONSE_FILE
done < packer.log

Effectively the commands echo the logfile to the task's output.response variable. We can then construct the placeholder using our knowledge of the pipeline.

Pipeline elements used to create a placeholder in the email template

In this case, our placeholder text becomes:

${Build.Read Logfile.output.response}

We can just place that where we want the logfile text to be placed in the email template.

Export

Once you have a template that you like the look of, you can export it and download the HTML source. The zip file that you get will include the HTML and any images or assets you've added to your template.

HTML source for the Packer pipeline email

I'd hate to have written that by hand!

Code Stream Notification Setup

Now that we have our email source, we can add it in to Code Stream. Once you have the pipeline open for editing, it's just a matter of configuring notifications for it.

⚠️
For an email notification to work, you will have had to configure an email endpoint in Code Stream. For more information about how to do this, see the Email page on the Learn Code Stream site.

When adding a notification, you can then just copy the HTML source from your template in to the configuration window, as shown below.

Notification configuration in Code Stream

Once the changes are saved you're all good to go. Having kicked off a pipeline execution, I went off and did something else and was notified once it was done. Those placeholders in the email template were replaced by Code Stream using outputs from each of the stages of my pipeline. Here's an example from my Windows 2022 build:

Sample email sent by Code Stream

At a glance I can see that the build appeared to be successful. Had there been an error, particularly in the Packer task, I have the full log present to review with ease.