1

My Question:

I want to send email if an enduser ever wants to receive emails on a regular basis with the attached csv file, we can change the pipeline to set the "isSendEmail" to "true" and populate the default value with that user's email address and the job will automatically send emails from that point on.

but while configuring the jenkisnfile, job runs fine but there is no email notification received? what is the correct way to configure parameter for Email Id's in jenkinsfile to send email so that doing it this way also allows me to easily run the job manually and sends the email to other people on an adhoc basis.

Jenkinsfile

 
pipeline {
  agent any
  options {
    buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '5')
  }
  stages {
       stage('Use AWS CLI to detect untagged resources') { steps { 
       script {
            def awsUtil = new AwsUtil()
            sh """#!/bin/bash
            set
            aws resourcegroupstaggingapi get-resources --tags-per-page 100 --tag-filters Key=SysName,Values=KickMe --tag-filters Key="SysName in ServiceNow",Values=False > tag-filter.json
            jq '.ResourceTagMappingList[].Tags |= map(select(.Key|IN("ManagedBy","SysName","Owner","SysName in ServiceNow","Name","TSM")))' tag-filter.json > untagged-resources.json
            ls
            echo "un tag resource.json file data"
            cat untagged-resources.json
            echo "--------------"
            cat tag-filter.json
            python untag_resources_convert_csv.py
            ls
            """
        }}}
       stage('Setup parameters') { steps {
       script {
           properties([
                parameters([
                    booleanParam(
                       defaultValue: true, 
                       description: '', 
                       name: 'isSendEmail'  
                    ),
                    string(
                       defaultValue: '', 
                       description: '', 
                       name: 'def@xxx.com,abc@xxx.com,ghi@xxx.com', 
                       trim: true
                    )
                ])
            ])
        }}}                             
    }
    post {
        always {
            archiveArtifacts artifacts: 'unTagResources_Details.csv', onlyIfSuccessful: true
            emailext (
        attachmentsPattern: 'unTagResources_Details.csv', body: '''${SCRIPT, template="groovy-html.template"}''',
                subject: '$DEFAULT_SUBJECT',
                mimeType: 'text/html',to: "email id"
            );
        }    
    }
}

1 Answers1

0

One way to achieve this is using the following technique: define an environment variable (or a global one) in you pipeline that will hold the default mailing list (DEFUALT_MAIL_LIST in the example) which can be changed in the pipeline code according to future needs.
In addition define an pipeline parameter (MailingList in the example) that will enable users that are manually building the job to pass a comma separated string of mails that will receive the mail notification.
Finally add a condition to your post block to check if one of the parameters is filled - and if so send the mails to all recipients.

This solution will allow you both a default, code configured, mailing list alongside a user controlled list.
Here is an implementation exmaple:

pipeline {
    agent any
    options {
        buildDiscarder logRotator( numToKeepStr: '5')
    }
    environment{
        DEFUALT_MAIL_LIST = 'def@xxx.com,abc@xxx.com,ghi@xxx.com'
    }
    parameters {
        string(name:'MailingList', defaultValue: '',description: 'Email mailing list', trim: true)
    }
    stages {
        stage('Use AWS CLI to detect untagged resources') {
            steps {
                ...
            }
        }
    }
    post {
        always {
            archiveArtifacts artifacts: 'unTagResources_Details.csv', onlyIfSuccessful: true
            script {
                if(params.MailingList || env.DEFUALT_MAIL_LIST){
                    emailext subject: '$DEFAULT_SUBJECT', mimeType: 'text/html', attachmentsPattern: 'unTagResources_Details.csv',
                            to: "${params.MailingList},${env.DEFUALT_MAIL_LIST}", body: '${SCRIPT, template="groovy-html.template"}'
                }
            }
        }
    }
}

If both parameters are empty, no mail will be sent.
You can also modify the logic so if the user inputed the mail list it will be sent to the user input, otherwise it will be sent to the default list.

Noam Helmer
  • 5,310
  • 1
  • 9
  • 29
  • how would you pass parameters to the template though? is that possible? or can the template reference job parameters? – mike01010 May 22 '23 at 16:36
  • Template can use job parameters, take a look at this [answer](https://stackoverflow.com/questions/17332685/jenkins-pass-build-parameters-to-email-ext-template) for example. – Noam Helmer May 23 '23 at 15:57