jenkinsgithubjenkins-pipelinejenkins-blueocean

How can i find username/details of people who newly pushed code to github? I am using jenkins pipeline to build and deploy


I have created job in jenkins to trigger a build whenever changes happen in github. But i want to know how to get the details of the user who made the changes to configure email notification.

Could anyone help me with the solution please?


Solution

  • You can use git show command to fetch the author ID and email using below command. Assign this to a variable and use it anywhere in the pipeline.

    To Fetch Author ID: git show -s --pretty=%an To Fetch Author email: git show -s --pretty=%ae

    Pipeline script can be written as like below.

    pipeline {
        agent any
        options {
            timestamps()
        }
        stages {
            stage('Test Stage') {
                steps {
                    checkout changelog: true, poll: false, scm: [$class: 'GitSCM', branches: [[name: '*/Sample_branch']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'TestCredentials', url: '']]]
                    script {
                         Author_ID=sh(script: "git show -s --pretty=%an", returnStdout: true).trim()
                         Author_Name=sh(script: "git show -s --pretty=%ae", returnStdout: true).trim()
                    }
                    echo "${Author_ID} and ${Author_Name}"
                }
            }
        }
    }