3

I'm trying to write a JenkinsFile that automatically will reach to a git repo via ssh and perform some actions but I want to make the repo and ssh key use variables with the ssh id stored in Jenkins but I seem to be missing the Jenkins documentation for how to pass down variables to Jenkins Files as I'm not able to pass values down into the credentials key. The variables being passed down to the sh commands resolve perfectly fine though...

Example Pipeline Below:

pipeline {
  parameters {
    string(name: 'SSH_priv', defaultValue: 'd4f19e34-7828-4215-8304-a2d1f87a2fba', description: 'SSH Credential with the private key added to Jenkins and the public key to the username stored in Git Server, this id can be found in the credential section of Jenkins post its creation.')
    string(name: 'REPO', defaultValue: 'git@--------------------')
  }
  stages {
    stage ('Output Variables'){
      // checks I can get these variables
      steps{
        sh("echo ${params.SSH_priv}")
        sh("echo ${params.REPO}")
      }
    }

stage('Do Something') {
      steps {
        // this below commented line, does not work.  
        // sshagent (credentials: ['${params.SSH_priv}']){

        // this line does work
        sshagent (credentials: ['d4f19e34-7828-4215-8304-a2d1f87a2fba']){
          sh("git clone --mirror ${params.REPO} temp")
          dir("temp"){
            // start doing fancy stuff ...
            ....
            ....
          }
        }
      }
    }

The aim is a Pipeline that my fellow developers could call and will work with their own repos and own ssh id's that I'm not using. When I try to run this with the SSH_priv parameter passing down the value I get the below failure in Jenkins.

Failure Output

The JenkinsFile works perfectly fine with the credential id hard-coded- as shown below: Success Output

Varanus
  • 186
  • 2
  • 8

2 Answers2

4

So after testing different things a friend solved this in sub 5 minutes. Quotation mark types matter in Groovy Script

Changing

sshagent (credentials: ['${params.SSH_lower}']){

To

sshagent (credentials: ["${params.SSH_lower}"]){

Solved the issue.

Varanus
  • 186
  • 2
  • 8
  • To add to this, I wanted also to be able to change what those default were after first generating the right fields from the JenkinsFile, That solution was found here: https://stackoverflow.com/questions/57745451/how-can-i-override-a-jenkinsfiles-default-parameters – Varanus Jul 14 '20 at 07:36
1

Better to use environment step in pipeline.

pipeline {
    agent any
    environment { 
                AN_ACCESS_KEY = credentials('an_access_key_id') 
            }
    stages {
        stage('Example') {
            steps {
                sh 'printenv'
            }
        }
    }
}

And credentials should exist in jenkins with id an_access_key_id

Take a look at official documentation here

Dmitriy Tarasevich
  • 1,082
  • 5
  • 6
  • I've tried to implement this suggestion but this still has the same issue. My parameter isn't handed down to the environment. Which means I still need to hard code the id into the environment. – Varanus Jul 14 '20 at 01:13