Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-67402

Conditional post action depending on which trigger

      as Developer

      I want to be able to execute a post action depending on how a pipeline-job is triggered

      so that I can e.q. send a different slack message if the executing is based on a cron trigger or a upstream project.

       

      In a declarative jenkinsfile I would like to be able to have the knowledge which trigger triggered the execution.

      Possible options:

      • each trigger gets a unique name which can be queried in the environment
      • a trigger can have an 'expression' block which can be used to set a global boolean

       

      Thank in advance for considering

       

       

      Is this possible?

          [JENKINS-67402] Conditional post action depending on which trigger

          Casper Roubos added a comment -

          I already found an other way to get this done, but additional functionality in the post-section would still be advisible, because it would improve the readability.

           

          Workaround:

          def getSlackMessage() {
          	
          	def myCause = currentBuild.getBuildCauses()[0];
          	
          	if (myCause.toString().matches(".*BranchEventCause.*")) {
          		String message = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%s'").trim()
          		return "Event ${message}"
          	} 
          	else if (myCause.toString().matches(".*TimerTrigger.*")) {
          		return "Timer Event ${env.JOB_NAME} ${env.BUILD_NUMBER}"
          	}
          		else if (myCause.toString().matches(".*UserIdCause.*")) {
          		return "Manually started ${env.JOB_NAME} ${env.BUILD_NUMBER}"
          	}
          		else if (myCause.toString().matches(".*UpstreamCause.*")) {
          		return "Upstream started ${env.JOB_NAME} ${env.BUILD_NUMBER}"
          	}
          	return "UnknownEvent: (${myCause.toString()}) ${env.JOB_NAME} ${env.BUILD_NUMBER}"
          }
          pipeline {
          	...  post {
              success {
                  slackSend(channel: slackChannel,
                          message: "Build Success - ${getSlackMessage()}",
                          color: '#00FF00')
                  deleteDir()
              }
              unstable {
                  slackSend(channel: slackChannel,
                          message: "Build Unstable - ${getSlackMessage()}",
                          color: '#FFA500')
              }
              failure {
                  slackSend(channel: slackChannel,
                          message: "Build Failure - ${getSlackMessage()}",
                          color: '#FF0000')
              }
            }
          

          Casper Roubos added a comment - I already found an other way to get this done, but additional functionality in the post-section would still be advisible, because it would improve the readability.   Workaround: def getSlackMessage() { def myCause = currentBuild.getBuildCauses()[0]; if (myCause.toString().matches( ".*BranchEventCause.*" )) { String message = sh(returnStdout: true , script: "git log -n 1 --pretty=format: '%s' " ).trim() return "Event ${message}" } else if (myCause.toString().matches( ".*TimerTrigger.*" )) { return "Timer Event ${env.JOB_NAME} ${env.BUILD_NUMBER}" } else if (myCause.toString().matches( ".*UserIdCause.*" )) { return "Manually started ${env.JOB_NAME} ${env.BUILD_NUMBER}" } else if (myCause.toString().matches( ".*UpstreamCause.*" )) { return "Upstream started ${env.JOB_NAME} ${env.BUILD_NUMBER}" } return "UnknownEvent: (${myCause.toString()}) ${env.JOB_NAME} ${env.BUILD_NUMBER}" } pipeline { ... post { success { slackSend(channel: slackChannel, message: "Build Success - ${getSlackMessage()}" , color: '#00FF00' ) deleteDir() } unstable { slackSend(channel: slackChannel, message: "Build Unstable - ${getSlackMessage()}" , color: '#FFA500' ) } failure { slackSend(channel: slackChannel, message: "Build Failure - ${getSlackMessage()}" , color: '#FF0000' ) } }

            Unassigned Unassigned
            c_roubos Casper Roubos
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: