-
Improvement
-
Resolution: Fixed
-
Minor
-
None
-
Jenkins 2.97 running on Ubuntu 16.04
First: thanks for providing this plugin! I'm successfully using kubernetes-cd-plugin to deploy my application with a Jenkinsfile using a scripted pipeline. However, I'm struggling to determine whether or not the Deployment succeeded; I want to take different actions based on the success or failure of the deployment, and to get information about what happened that I can use within emails/slack notifications. There does not seem to be a way to do this, at least not one that's documented.
Here's what the deploy looks like in my Jenkinsfile:
kubernetesDeploy( configs: 'kube/jenkins/jenkins.yaml', credentialsType: 'SSH', dockerCredentials: [[credentialsId: 'my-credentials]], ssh: [ sshCredentialsId: 'my-key', sshServer: 'localhost' ]
This results in output like this...
Successful Deployment
Starting Kubernetes deployment Prepare Docker container registry secrets with name: acs-plugin--58r4nj89f4 Created Secret: name: acs-plugin–58r4nj89f4 ... <snip> ... Finished Kubernetes deployment
Failed Deployment
I modified the configs: path to point to a non-existent file, and this shows up:
Starting Kubernetes deployment ERROR: No matching configuration files found for kube/jenkins/jenkins.yml ERROR: "ERROR: "No matching configuration files found for kube/jenkins/jenkins.yml java.lang.IllegalStateException: No matching configuration files found for kube/jenkins/jenkins.yml at com.microsoft.jenkins.kubernetes.command.DeploymentCommand$1.call(DeploymentCommand.java:83) ... <snip> ... ERROR: Kubernetes deployment ended with HasError
What I'd like is to be able to do something like assign the kubernetesDeploy object to a variable and get information about it after it executes, something like this pseudo-code:
deployment = kubernetesDeploy( configs: 'kube/jenkins/jenkins.yaml', credentialsType: 'SSH', dockerCredentials: [[credentialsId: 'my-credentials]], ssh: [ sshCredentialsId: 'my-key', sshServer: 'localhost' ] if (deployment.success?) { echo 'hooray' } else { echo "oh no! Deployment failed with ${deployment.result}" }
I also attempted to wrap this in a try catch, but it seems like the exception is being caught somewhere within the plugin code and not re-thrown (maybe here?) Anyway, while writing this bug (of course) I found a workaround, that I can use the currentBuild.result flag, which is set to FAILURE if the deploy fails, so I will use this for now:
kubernetesDeploy( configs: 'kube/jenkins/jenkins.yaml', credentialsType: 'SSH', dockerCredentials: [[credentialsId: 'my-credentials]], ssh: [ sshCredentialsId: 'my-key', sshServer: 'localhost' ] ) if (currentBuild.result == 'FAILURE') { echo "oh no! Deployment of ${env.JOB_NAME} FAILED!") } else { echo "Yippee! Deployment of ${env.JOB_NAME} succeeded!") }
Build steps in pipeline returns null. That's why we don't have meaningful return value for the kubernetesDeploy step.
Checking the build result may be a workaround, but it's still not ideal. Currently, if the Kubernetes deployment failed, the build pipeline will just continue, which is not expected.
I implemented a fix at https://github.com/jenkinsci/kubernetes-cd-plugin/pull/12 , which will abort the build on error, rather than continue silently.
With the fix, the build terminates if the Kubernetes deployment fails. If you need extra handling for the failure, you can use the {{try ... catch ... }} block.
sorry for the late update. The issues reported here were not actively tracked previously.