jenkins

Jenkins: Unable to create directory using the date


I want to create a directory using jenkins with the days date. I'm using the new jenkins declarative syntax. When i run the build as described in the job below it fails. The mkdir command though, works perfectly well on the console.

pipeline {
agent any
stages {
    stage('Prepare') {
        steps {

            echo "Checking for the existence of a debian packages directory for this package"
            sh "mkdir -p {env.JENKINS_HOME}/workspace/debian_packages/api-config/$(date +"%d-%m-%Y")"
            }
        }
    }
}

This is the error I get (I've tried escaping the $ characters but it still fails)

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup 
failed:
WorkflowScript: 19: illegal string body character after dollar sign;
solution: either escape a literal dollar sign "\$5" or bracket the 
value expression "${5}" @ line 19, column 17.
               sh "mkdir -p 
 {env.JENKINS_HOME}/workspace/debian_packages/api-
 config/$(date +'%d-%m-%Y')"

What could be the issue? Isn't the jenkins "sh" meant to take commands as they would have been issued directly on the console?


Solution

  • This is workaround that I could come up:

    stage('Prepare') {
            steps {
                script{
                sh '(date +"%d-%m-%Y") > outFile'
                def curDate = readFile 'outFile'
                echo "The current date is ${curDate}"
                sh "mkdir -p {env.JENKINS_HOME}/workspace/${curDate}"
                }
            }
        }