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

sshCommand not working in post{} of declarative pipeline

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • ssh-steps-plugin
    • None

      I've the following declarative pipeline:

      #!groovy
      
      pipeline {
        agent any
      
        parameters {
          string(name: 'DEPLOY_USER', defaultValue: '', description: 'User of deploy server')
          string(name: 'DEPLOY_SERVER', defaultValue: 'deploy.invitel-ug.de', description: 'Deploy server')
        }
      
        environment {
          SSH_IDENTITY_FILE_PRIVATE = '/var/jenkins_home/.ssh/id_rsa'
          SSH_KNOWN_HOSTS_FILE = '/var/jenkins_home/.ssh/known_hosts'
        }
      
        stages {
      
          stage('Run') {
            steps {
              script {
                remote = createDeployRemote()
      
                sshCommand remote: remote, command: "echo 1"
              }
      
            }
            post {
              always {
                script {
                  remote = createDeployRemote()
      
                  sshCommand remote: remote, command: "echo 2"
                }
              }
            }
          }
        }
      
      }
      
      def createDeployRemote() {
        def remote = [: ]
        remote.name = "${params.DEPLOY_SERVER}"
        remote.host = "${params.DEPLOY_SERVER}"
        remote.user = "${params.DEPLOY_USER}"
        remote.knownHosts = "${SSH_KNOWN_HOSTS_FILE}"
        remote.allowAnyHosts = true
        remote.identityFile = "${SSH_IDENTITY_FILE_PRIVATE}"
        remote.timeoutSec = 10
        remote.retryCount = 3
        remote.retryWaitSec = 3
        return remote
      }
      

      The first command within step is executed and I can see it's output, but within post{} it's not executed at all, but marked as successful. Same goes for putting post{} after stages{}.

          [JENKINS-68763] sshCommand not working in post{} of declarative pipeline

          Tried putting the script into a .sh file and executing it with sshScript, same result:

          post {
              cleanup { // changed to cleanup here
                  script {
                      remote = createDeployRemote()
          
                      writeFile file: 'cleanup.sh', text: """
          echo 1
          """
                      sshPut remote: remote, from: 'cleanup.sh', into: '.'
                      sshScript remote: remote, script: "cleanup.sh"
                  }
              }
          }
          

          Fabian Grutschus added a comment - Tried putting the script into a .sh file and executing it with sshScript, same result: post { cleanup { // changed to cleanup here script { remote = createDeployRemote() writeFile file: 'cleanup.sh' , text: """ echo 1 """ sshPut remote: remote, from: 'cleanup.sh' , into: '.' sshScript remote: remote, script: "cleanup.sh" } } }

          Ok it seems the real command I wanted to execute failed. After fixing this, adding`set -e` to the command and setting pty = true to the remote config, I can see the output of the commands and they are really executed. Without pty = true you can't see the output, that's what caused the confusion. So imho only the output is somehow broken.

          Fabian Grutschus added a comment - Ok it seems the real command I wanted to execute failed. After fixing this, adding`set -e` to the command and setting pty = true to the remote config, I can see the output of the commands and they are really executed. Without pty = true you can't see the output, that's what caused the confusion. So imho only the output is somehow broken.

            nrayapati Naresh Rayapati
            fabiang Fabian Grutschus
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: