Home > Back-end >  Prevent single quotes surrounding parameter in Jenkinsfile
Prevent single quotes surrounding parameter in Jenkinsfile

Time:10-06

In my Jenkinsfile (Jenkins running pipelines), I want to execute a command :

def lastTagV1 = sh(script: 'git describe --abbrev=0 --tags', returnStdout: true).trim()

// same result
def lastTagV2 = sh(script: '''git describe --abbrev=0 --tags''', returnStdout: true).trim()

// same result
def lastTagV3 = sh(script: "git describe --abbrev=0 --tags", returnStdout: true).trim()

But when executing this step, Jenkins (or bash) is surrounding the abbrev with single quotes, and the command is failing :

  git describe '--abbrev=0' --tags
fatal: No names found, cannot describe anything.

Maybe there is another solution to get the last tag from git command, but I want to understand why and how to solve this single quote problem.

SOLUTION

The command works on Jenkins even with single quotes, but needed to fetch tags before, else it fails as described above.

sh 'git fetch --tags'
def lastTag = sh(script: 'git describe --abbrev=0 --tags', returnStdout: true).trim()

CodePudding user response:

I don't think Jenkins has any reason to add quotes like that. Also, the command seems to work even with quotes git describe '--abbrev=0' --tags. I assume you are seeing that error because you don't have any tags in your repo. The following work fine for me.

pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                script {
                    cleanWs()
                    git(url: 'https://github.com/jenkinsci/analysis-pom-plugin.git', branch: 'master')
                    def lastTag = sh(script: 'git describe --abbrev=0 --tags', returnStdout: true).trim()
                    echo "$lastTag"
                }
            }
        }
    }
}
  • Related