0

I am very new to jenkins pipeline and groovy . Please ignore environment variable i will use it in future

Here i want to send start to slack and upon git pull if have to send success or failure based on project status. I am getting error at try for something

pipeline {
    agent any
    environment { 
        val = 1
    }
    stages {
        stage('Started') {
            steps{
                slackSend(message: "STARTED")
            }
        }

        try {
            stage('SCM Checkout') {
                steps {
                    git url:'https://github.com/Abhishek24094/dev.git'
                }
            }
        }
        catch(Exception e) {
            slackSend(message: "Failure")
        }

        stage('finished') {
            steps{
                slackSend(message: "STARTED")
            }
        }
    }
}
towel
  • 2,094
  • 1
  • 20
  • 30

1 Answers1

2

You are using a declarative pipeline (as indicated by the pipeline step in line 1).

This does not allow you to run plain Groovy code (such as try), but builds around pre-defined steps (agent,environment, stage etc.).

The good news for you is that you don't need this error handling. Instead, post allows you to run steps after the the build based on success or failure:

pipeline {
    // ..
    post {
        success {
            slackSend ( message: "STARTED")
        }
        failure { 
            slackSend ( message: "Failure")
        }
    }
}
StephenKing
  • 36,187
  • 11
  • 83
  • 112