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

sshCommand not working in post{} of declarative pipeline

XMLWordPrintable

    • 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{}.

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

              Created:
              Updated: