Ready2work
Ready2work

Reputation: 35

Need to get user email id as input and send logs in jenkins

I have a shell script wil runs on taking user inputs and send logs to users when fails syntax I use: ./script.sh env usecase emailid Now am doing a jenkins build and not sure on how to get user input for email id . I am currently getting 2 inputs using choice parameter. I want user to give email id and its passed as a parameter .

@Library('Shared@release/v1')
import jenkins.BuildSupport

properties([parameters([choice(choices: ['dev''uat', 'prod'], description: 'Select the Environment', name: 'ENVIRONMENT'), choice(choices: ['a1','a2','all'], description: 'Select the Service', name: 'SERVICENAME')])])

node{  
    WORKSPACE = pwd()

    //checkout code from shared library 
    stage ('Code Checkout'){
    codeCheckout
    }   

    //post build work
    stage('Executing Health Check') {
    withEnv(["WORKSPACE=${WORKSPACE}","ENVIRONMENT=${params.ENVIRONMENT}","SERVICENAME=${params.SERVICENAME}",]) {
            sh '''
            set +x  
            ls -l
            ./script.sh ${ENVIRONMENT} ${SERVICENAME} 
            '''
        }  
   }
}

I need the script.sh to take 3rd parameter which will be the email id entered by user

Upvotes: 0

Views: 1071

Answers (2)

Adam vonNieda
Adam vonNieda

Reputation: 1745

Example of sending email from Jenkins scripted pipeline/ Groovy

    stage('Email the results') {
        emailext attachLog: true,
            attachmentsPattern: '*',
            to: "${EMAIL_ADDRESS}",
            subject: "${currentBuild.currentResult} - ${ENVIRONMENT} ${SERVICE}",
            body: """
Blah blah blah
"""
    }

Upvotes: 0

Adam vonNieda
Adam vonNieda

Reputation: 1745

So couple of things going on here. First, you need to add a string parameter to ask the user for input, then you need to pass that to the shell script, and then you need to make sure the shell script can use it.

I don't see the need for withEnv, you can pass variables to a script without that.

Just make sure your shell script is getting the EMAIL_ADDRESS from $3

#!groovy

@Library('Shared@release/v1')
import jenkins.BuildSupport

properties([parameters([string(name: 'EMAIL_ADDRESS', description: 'Enter the email address'), choice(choices: ['dev','uat','prod'], description: 'Select the Environment', name: 'ENVIRONMENT'), choice(choices: ['a1','a2','all'], description: 'Select the Service', name: 'SERVICENAME')])])

node{  
    WORKSPACE = pwd()

    //checkout code from shared library 
    stage ('Code Checkout'){
    codeCheckout
    }   

    //post build work
    stage('Executing Health Check') { 
       sh '''
          set +x  
          ls -l
          ./script.sh $ENVIRONMENT $SERVICENAME $EMAIL_ADDRESS
       '''
    }
}

Upvotes: 1

Related Questions