我创建了一个jenkins作业,它从用户那里读取Cron schedule(字符串(参数,并将其转发给某个bash脚本,该脚本将把值插入某个yaml文件:
stringParam(name: "CRON_SCHEDULE", description: "What is the Cron job schedule? (e.g. * * * * *) ")
这些时间表通常包含星号,这会导致bash脚本列出当前目录中的文件,而不是仅作为字符串(如groovy文件所定义的(。
当我从groovy文件调用bash脚本并发送(命名的(参数时:
sh "./scripts/create_yaml.sh CRON_SCHEDULE="'$params.CRON_SCHEDULE'" "
时间表似乎很好(在作业日志中(:
+create_yaml.sh CRON_SCHEDULE='0 0***’
但是,当bash脚本读取这个参数并将其插入yaml文件时,它会列出这些文件,参数如下所示:
"0 0文件1文件2文件3文件1文件3文件3文件2文件1文件2中文件3">
我已经尝试了我所知道的每一种引号操作,在bash脚本之外,也在里面,我不知道下一步该怎么做。
如何将此参数作为普通字符串传递和处理?
下面是bash脚本,它接收调度参数并创建yaml:
#!/bin/bash
for ARGUMENT in "$@"
do
KEY=$(echo $ARGUMENT | cut -f1 -d=)
VALUE=$(echo $ARGUMENT | cut -f2 -d=)
case "$KEY" in
APPLICATION_NAME)
APPLICATION_NAME=${VALUE} ;;
IMAGE_NAME)
IMAGE_NAME=${VALUE} ;;
APPLICATION_TYPE)
APPLICATION_TYPE=${VALUE} ;;
CRON_SCHEDULE)
CRON_SCHEDULE="${VALUE}" ;;
CHART_FOLDER)
CHART_FOLDER=${VALUE} ;;
esac
done
echo "cron schedule is ${CRON_SCHEDULE}";
touch ./$CHART_FOLDER/values.yaml
if [ $APPLICATION_TYPE == "cronjob" ]; then
cat > "./$CHART_FOLDER/values.yaml" << EOF
image:
image: $IMAGE_NAME
tag: base
cron:
production:
schedule: "$CRON_SCHEDULE"
EOF
fi
任何帮助都将不胜感激。谢谢
以下脚本:
#!/bin/bash
args=([0]="APPLICATION_NAME=Testing1" [1]="APPLICATION_DESCRIPTION=testRun" [2]="IMAGE_NAME=imageName" [3]="SENTRY_DSN=DSN" [4]="COMMAND=command" [5]="COMMAND_ARGS=args" [6]="APPLICATION_TYPE=cronjob" [7]="EXPOSE_JIRA=false" [8]="EXPOSE_WORKATO=false" [9]="CRON_SCHEDULE='0 0 0 * *'" [10]="CHART_FOLDER=chart_output")
set -- "${args[@]}"
for i in "$@"; do
IFS== read -r key value <<<"$i"
# remove single quotes
if [[ "$value" =~ ^'(.*)'$ ]]; then
value=${BASH_REMATCH[1]}
fi
# use declare to declare the variable with that value
declare "$key=$value"
done
echo "cron schedule is ${CRON_SCHEDULE}";
cat << EOF
image:
image: $IMAGE_NAME
tag: base
cron:
production:
schedule: "$CRON_SCHEDULE"
EOF
输出:
cron schedule is 0 0 0 * *
image:
image: imageName
tag: base
cron:
production:
schedule: "0 0 0 * *"