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

Provide a means for determining the result of running kubernetesDeploy.

    • Icon: Improvement Improvement
    • Resolution: Fixed
    • Icon: Minor Minor
    • kubernetes-cd-plugin
    • 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!")
      }
      

          [JENKINS-48662] Provide a means for determining the result of running kubernetesDeploy.

          Menghua Xiao added a comment -

          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.

          try {
              kubernetesDeploy configs: 'nginx1.yml',
                               kubeConfig: [path: 'kubeconfig']
          } catch (e) {
              println e
          }
          

          sorry for the late update. The issues reported here were not actively tracked previously.

          Menghua Xiao added a comment - 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. try { kubernetesDeploy configs: 'nginx1.yml' , kubeConfig: [path: 'kubeconfig' ] } catch (e) { println e } sorry for the late update. The issues reported here were not actively tracked previously.

          Code changed in jenkins
          User: Menghua Xiao
          Path:
          src/main/java/com/microsoft/jenkins/kubernetes/KubernetesDeploy.java
          src/main/resources/com/microsoft/jenkins/kubernetes/Messages.properties
          http://jenkins-ci.org/commit/kubernetes-cd-plugin/660a7d9e5eeb2714e07b1181603d0d4b547ca2dc
          Log:
          Abort build on error, addressing JENKINS-48662 (#12)

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Menghua Xiao Path: src/main/java/com/microsoft/jenkins/kubernetes/KubernetesDeploy.java src/main/resources/com/microsoft/jenkins/kubernetes/Messages.properties http://jenkins-ci.org/commit/kubernetes-cd-plugin/660a7d9e5eeb2714e07b1181603d0d4b547ca2dc Log: Abort build on error, addressing JENKINS-48662 (#12)

          Menghua Xiao added a comment -

          Fixed in release 0.1.5.

          The build aborts if the deployment failed. If you need to recover from the failure, use try...catch... in your pipeline.

          Menghua Xiao added a comment - Fixed in release 0.1.5. The build aborts if the deployment failed. If you need to recover from the failure, use try...catch... in your pipeline.

            arieshout Menghua Xiao
            tonyflint Tony Flint
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: