Jenkins管道未定义变量

时间:2017-10-18 09:11:20

标签: jenkins-pipeline

我正在尝试构建一个参数为的Jenkins管道 可选的:

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

我的目的是调用shell脚本并提供foo作为参数:

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

如果提供的值为空,则shell脚本将执行Right Thing™ 字符串。

不幸的是我不能得到一个空字符串。如果用户没有提供 foo的值,Jenkins会将其设置为null,我将获得null (作为字符串)在我的命令中。

我找到this related question,但唯一的答案并不是真的有用。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

OP在这里意识到包装脚本可能会有所帮助......我讽刺地称之为junkins-cmd,我称之为:

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

代码:

#!/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[@]}"

注意:prams+=("$p")似乎不能在shell中移植:因此这个丑陋的脚本正在运行#!/bin/bash

相关问题