1

I'm trying to build a Jenkins Pipeline for which a parameter is optional:

parameters {
    string(
        name:'foo',
        defaultValue:'',
        description:'foo is foo'
    )
}

My purpose is calling a shell script and providing foo as argument:

stages {
    stage('something') {
        sh "some-script.sh '${params.foo}'"
    }
}

The shell script will do the Right Thing™ if the provided value is the empty string.

Unfortunately I can't just get an empty string. If the user does not provide a value for foo, Jenkins will set it to null, and I will get null (as string) inside my command.

I found this related question but the only answer is not really helpful.

Any suggestion?

Dacav
  • 13,590
  • 11
  • 60
  • 87
  • Quick workaround using shell: `sh "some-script.sh $([[ '${params.foo}' == 'null' ]] && echo '' || echo ${params.foo})"` – Razvan Oct 18 '17 at 09:18
  • Thanks @Razvan. For the record, I wrote things as a shell script just to find out that I still need to do hacks and unreadable stuff for it. I think that scriptable pipelines are a good idea, overall, but Jenkins is really problematic even when it tries to be decent… Facepalm – Dacav Oct 18 '17 at 09:20

1 Answers1

0

OP here realized a wrapper script can be helpful… I ironically called it junkins-cmd and I call it like this:

stages {
    stage('something') {
        sh "junkins-cmd some-script.sh '${params.foo}'"
    }
}

Code:

#!/bin/bash

helpme() {
cat <<EOF
Usage: $0 <command> [parameters to command]

This command is a wrapper for jenkins pipeline. It tries to overcome jenkins
idiotic behaviour when calling programs without polluting the remaining part
of the toolkit.

The given command is executed with the fixed version of the given
parameters. Current fixes:

 - 'null' is replaced with ''
EOF
} >&2

trap helpme EXIT
command="${1:?Missing command}"; shift
trap - EXIT

typeset -a params
for p in "$@"; do

    # Jenkins pipeline uses 'null' when the parameter is undefined.
    [[ "$p" = 'null' ]] && p=''

    params+=("$p")
done

exec $command "${params[@]}"

Beware: prams+=("$p") seems not to be portable among shells: hence this ugly script is running #!/bin/bash.

Dacav
  • 13,590
  • 11
  • 60
  • 87