Cannot call ruby rake with parameter containing spaces in jenkins pipeline

110 views Asked by At

I have Jenkins Pipeline that will deploy a pod with a container using my ruby's application image, and there is a stage that will run a rake command from a $COMMAND parameter like this: rake db_command:count_data[histories,status IN ("active","pending")] inside the container.

this is the step I wrote on my stage to run the rake command from a $COMMAND parameter:

stage("Rake Task") {
            try {
                container("ruby-rake-task") {   
                  sh '''
                    cd /var/www/mos
                    ${COMMAND}
                  '''
                }
            } catch(e) {
                echo "rake failed"
                sh "exit 1"
            }
        }

but I get this error from console output jenkins:

+ cd /var/www/mos
+ rake 'db_command:count_data[histories,status' IN '("active","pending")]'
rake aborted!
Don't know how to build task 'db_command:count_data[histories,status' (See the list of available tasks with `rake --tasks`)
/usr/local/bundle/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
(See full trace by running task with --trace)

it seems the jenkins or groovy won't execute the rake command as a whole command, and instead it split the command to three parts. Jenkins is somehow always give a quote for all parameters after 'rake' command this really hard when the parameter is having spaces like above.

I already try adjust my stage step like this:

stage("Rake Task") {
            try {
                container("ruby-rake-task") {   
                  sh "cd /var/www/mos"
                  def rakeTask = $/eval ${COMMAND}/$
                  resultRake = sh(script: "${rakeTask}", returnStdout: true).toString()
                  echo "${resultRake}"
                }
            } catch(e) {
                echo "rake failed"
                sh "exit 1"
            }
        }

yet the result is produce another error like this:

[Pipeline] stage
[Pipeline] { (Rake Task)
[Pipeline] container
[Pipeline] {
[Pipeline] sh
+ cd /var/www/mos
[Pipeline] sh
/home/jenkins/agent/workspace/RUBY-APP-RAKE-TASK@tmp/durable-c0e8d0d4/script.sh: line 1: syntax error: unexpected "("
[Pipeline] }
[Pipeline] // container

what do I need to do to be able run this rake task as a whole command included the spaces parameter? can someone tell me what am I missing here?

Thank you!

1

There are 1 answers

0
dreSte On

I'm using a node container with entrypoint "bash" and resolved errors like your. I adapt it for you:

"-c \"cd /var/www/mos && ${command}\""

I hope it helps.