Trigger Jenkins Job from Outlook

Heya!

The unattended Windows installation project was dropped due to some external constrain so I can't give you update on that yet. Hope we can return on it soon.

A small tip for today research, how to trigger Jenkins job with very limited access to the infrastructure environment. 

Use Case:

I have to trigger a Jenkins job to do some tasks when I have an email notification from the release team, however, the process to submit my request to the release team is complicated and my request is not in a "considerable" priority, so how I can "listen" to their broadcast then trigger my job. There are some ideas out there.

Solution:

I found this thread to discuss on my issue, so they have some solutions such as:
  • qmail agent on Linux
  • Free email client and FSTrigger plugin
  • Poll Mailbox Trigger plugin
So on, however, I don't have luxury permission to touch into Jenkins server either email server information. So, a simple workaround (tweaky way) that I will implement an email rule from Outlook that will trigger a script then this script will call the http request to Jenkins REST API to trigger my job. Simple enough right?
I found this article to create an email rule to trigger VBA script from Outlook. First of all, if your Outlook doesn't have the "run script" rule, please enable in registry via this guide. Then, open the VBA development (by Atl+F11) then you will implement some functions there to trigger Jenkins REST API. 

Sub TriggerJob(buildType As String, buildID As String)
    Dim httpRequest As Object
    Dim jobURL As String
    Dim authentication As String
    
    jobURL = "[JenkinsJobURL]/buildWithParameters?KitType=" + buildType + "&KitVersion=" + buildID
    Set httpRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
    httpRequest.Open "POST", jobURL
    
    authentication = "Basic " + EncodeBase64("JenkinsUser:JenkinsPwd")
    httpRequest.SetRequestHeader "Authorization", authentication
    httpRequest.Send
    Set httpRequest = Nothing
End Sub

Function EncodeBase64(text As String) As String
  Dim arrData() As Byte
  arrData = StrConv(text, vbFromUnicode)

  Dim objXML As Object
  Dim objNode As Object

  Set objXML = CreateObject("MSXML2.DOMDocument")
  Set objNode = objXML.createElement("b64")

  objNode.dataType = "bin.base64"
  objNode.nodeTypedValue = arrData
  EncodeBase64 = objNode.text

  Set objNode = Nothing
  Set objXML = Nothing
End Function

To get the function display from email rule, you have to create a Sub with input is the Outlook.MailItem object (base on the guideline above)

Sub MailTrigger(oMail As Outlook.MailItem)
    Dim buildType As String
    Dim buildID As String
    
    'Do your logic here to get build information from email
    TriggerJob buildType, buildID
End Sub


Another note, you have to put those functions into a Module, do not use ThisOutlookSession module or else it won't trigger your script (I don't know why)
Alright, now, you will create an email rule by your need, then in the action, select "run script" and select your MailTrigger script. 
This will be a client rule so only when the Outlook client at your machine receive email then it will trigger this script. You better have a desktop to let it run to trigger the job at the time your Outlook receive the email.

PS: another way is you can call a ps1 script from this VBA script, but we have to deal with execution permission to run the ps1 file, so I decide to call from VBA.

A small tip for today! Have a good weekend, everyone!

Comments