Jenkins declarative pipeline failing when using try catch finally blocks in script tags also at post

Viewed 210

I have been trying using try catch blocks in my declarative jenkins pipeline but it keeps on failing while executing post statement but no executions appears of try catch block.

Error when executing always post condition:

*Error when executing always post condition:
00:44:07  groovy.lang.MissingPropertyException: No such property: subject for class: groovy.lang.Binding
00:44:07    at groovy.lang.Binding.getVariable(Binding.java:63)
00:44:07    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:270)
00:44:07    at org.kohsuke.groovy.sandbox.impl.Checker$7.call(Checker.java:353)
00:44:07    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:357)
00:44:07    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:333)
00:44:07    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:333)
00:44:07    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:333)
00:44:07    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
00:44:07    at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
00:44:07    at WorkflowScript.run(WorkflowScript:91)
00:44:07    at ___cps.transform___(Native Method)*

Here's my pipeline code

 @Library('akci@feature/TRE-168-notify-build-status-from-jenkins-to-gchat')

import groovy.transform.Field
import com.kaercher.pre.Definitions

def git_commit = ""
def host_name = "localhost"
def docker_vm_name = "terraform-mce-vm"
def docker_image_name = "terraform-mce-iac"
def virtual_nework = "192.168.61.3/24"
def docker_image = ""
def docker_image_version_tag = ""

addCiParameter(
    logRotator(artifactDaysToKeepStr: '8', artifactNumToKeepStr: '3', daysToKeepStr: '8', numToKeepStr: '3'),
    [booleanParam(defaultValue: false, description: 'Deploy the Docker container', name: 'deploy')]
)

pipeline {
    agent {label 'linux'}
    stages {
        stage('Prepare Build'){
            steps {
                sh "git clean -fdx"
                script {
                    git_commit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
                    host_name = sh(returnStdout: true, script: 'hostname').trim()
                    
                    echo "Build Prepared"
                }
            }
        }
         stage('Build') {
            steps {
                 
                script {
                
                try {
                echo "Build-Started"
                
                dir ('docker') 
                
                        { 
                        def docker_image_version = (readFile("Dockerfile") =~ ~/version=\"(\d+\.\d+\.\d+)/)[0][1]
                        docker_image_version_tag = (params.revision == "master") ? docker_image_version : "${docker_image_version}-${params.revision.replaceAll("-","_").replaceAll("/","_").replaceAll("[^A-Za-z0-9_]", "")}"
                        docker_image = docker.build("iac-docker.artifactory.kws.kaercher.com/$docker_image_name:${docker_image_version_tag}", "--build-arg http_proxy='http://$host_name:3128' --build-arg https_proxy='http://$host_name:3128' -f Dockerfile .")
                        }
                        
                        notifyBuild('STARTED')
                        
                        }
                    catch (e) {
    // If there was an exception thrown, the build failed
    currentBuild.result = "FAILED"
    throw e
  }
    
    finally {
    notifyBuild(currentBuild.result)
    }
    
                        
           }
        }
    
     }
  }


post {
        always {
            script {notifyBuildEnded(git_commit)}
          script {
          googlechatnotification(
          url: 'https://chat.googleapis.com/v1/spaces/AAAAjU4cyQU/messages?key=MY_API_KEY&token=MY_TOKEN',
          message: "*${subject}* ${summary} *${commitRef}* ${details}* ${env.BUILD_URL}",
          sameThreadNotification: true
        )
        }
      }
    }
    
}

also I have created a function as shared library as notifyBuild.groovy

void call(String buildStatus = 'STARTED') {
  // build status of null means successful
  buildStatus = buildStatus ?: 'SUCCESS'

  // Default values
  def colorName = 'RED'
  def colorCode = '#FF0000'
  def subject = "${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
  def summary = "${subject} (${env.BUILD_URL})"
  def details = """<p>STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
    <p>Check console output at &QUOT;<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>&QUOT;</p>"""

  // Override default values based on build status
  if (buildStatus == 'STARTED') {
    color = 'YELLOW'
    colorCode = '#FFFF00'
  } else if (buildStatus == 'SUCCESS') {
    color = 'GREEN'
    colorCode = '#00FF00'
  } else {
    color = 'RED'
    colorCode = '#FF0000'
  }
  }
1 Answers

Your pipeline is failing because of the following line :

   message: "*${subject}* ${summary} *${commitRef}* ${details}* ${env.BUILD_URL}",

The subject parameter is not defined anywhere and therefore you receive the exception:

groovy.lang.MissingPropertyException: No such property: subject for class: groovy.lang.Binding`

Although subject and summary are defined in the shared library, it is an entire different context which is not relevant to the main declarative pipeline. In addition commitRef and details are not defined at all anywhere. Define these parameters or replace them with values and it should work.

Related