Atique Ahmed
Atique Ahmed

Reputation: 416

How to passed the ssh credential in Jenkins Pipeline while deploying to another server

I want to deploy my code as a pipeline, once my testing job is done. How to make the login into another instance from jenkins pipeline.

First I have saved my credentials in Jenkins -

Jenkins Credentials

Also You can check the connection -

Jenkins SSH Connection Success

In pipeline script I am passing -

!/usr/bin/groovy

pipeline { agent any

stages {

    stage('Build') {
        steps {
            echo 'Building..'
        }
    }

    stage('Test') {
        steps {
            echo 'Testing..'
        }
    }

    stage('Deploy') {
        steps {
            echo 'Deploying....'
            withCredentials([string(credentialsId: '28ebc607-22f5-4fad-91f2-97de971512d3', variable: 'NUSER'),string(credentialsId: '', variable: '')]) {
              sh 'pwd'
              sh 'ls -l'
           }
        }
    }

}

}

I am getting the output -

Jenkins credentials was excepted

Error -

ERROR: Credentials '28ebc607-22f5-4fad-91f2-97de971512d3' is of type 'SSH Username with private key' where 'org.jenkinsci.plugins.plaincredentials.StringCredentials' was expected Finished: FAILURE

What I am doing wrong ? What is the best way to do it ? Any suggestions ?

Upvotes: 1

Views: 7946

Answers (2)

Pankaj Saini
Pankaj Saini

Reputation: 1648

I got a similar use case and I am using the ssh-agent plugin for it. You need to create a credential of type SSH and then wrap your code with it.

sshagent(credentials: ['jenkins']) {
sh ''
}

I have created my credential as example below (using private key)

jenkins=[
  username: 'test',
  passphrase: '',
  description : 'SSH credentials',
  type: 'SSH',
  privateKey: ''
]

Upvotes: 0

Gavin Mogan
Gavin Mogan

Reputation: 1517

You probably need to use the ssh-agent plugin and wrap your ssh line with sshagent (credentials: ['deploy-dev']) { sh 'ssh server do thing' }

Upvotes: 2

Related Questions