-1

My credentails which I configure in Jenkins as global credentails chaged during the copy process to remote server. Why? [The 'RSA KEY' changed]

dave vedant
  • 329
  • 2
  • 4
  • 11

1 Answers1

0

I found out that I was using the withCredentials() only and during the process of copy it process the key information with base64 encoding.

The one probable solution for this is to use the sshagent to copy directly. the script for that is as follow.

pipeline {
    agent any

    environment {
        REMOTE_HOST = 'example.com'
        REMOTE_USER = 'remote_user'
        REMOTE_DIR = '/path/to/remote/directory'
        SECRET_KEY_NAME = 'my_rsa_key'
    }

    stages {
        stage('Copy RSA key') {
            steps {
                withCredentials([sshUserPrivateKey(credentialsId: 'ssh-credentials', keyFileVariable: 'SSH_KEY_FILE', passphraseVariable: '', usernameVariable: 'SSH_USERNAME')]) {
                    sshagent(['ssh-credentials']) {
                        sh """
                            set -x
                            scp -i ${SSH_KEY_FILE} ${SECRET_KEY_NAME} ${SSH_USERNAME}@${REMOTE_HOST}:${REMOTE_DIR}
                            ssh -i ${SSH_KEY_FILE} ${SSH_USERNAME}@${REMOTE_HOST} "chmod 600 ${REMOTE_DIR}/${SECRET_KEY_NAME}"
                        """
                    }
                }
            }
        }
    }
}

This solved my problem. Interesting fact is I try to go though various resources and confused with paraphrase argument as well. But finally I found out the solution with 'ChatGPT'.

Everyone are welcome for the alternative methods and solution. Thank you for your valuable time.

yong
  • 13,357
  • 1
  • 16
  • 27
dave vedant
  • 329
  • 2
  • 4
  • 11