使用 Jenkins 参数化管道执行 python 脚本
Executing python script using Jenkins parameterized pipeline
我已经设置了一个 Jenkins 参数化作业来使用 Jenkin 中的执行 shell 功能执行 python 脚本。该作业具有以下参数:
user-name: string, order_area_name: comma-separated strings, country_name: string, country_code: string, and so on...
我的用例是拆分 order_area_name 并按顺序为每个 order_area_name 执行 python 脚本。所以,我写了一个看起来像这样的脚本:
#!/bin/bash
export PYTHONHASHSEED=0
empty_string=""
parameters_list=""
IFS=","
#Checking every parameter if it is present or not
if [ "$user_name" != "$empty_string" ]
then
parameters_list=$parameters_list" --user "$user_name
fi
if [ "$country_code" != "$empty_string" ]
then
parameters_list=$parameters_list" --country_code "$country_code
fi
if [ "$country_category" != "$empty_string" ]
then
parameters_list=$parameters_list" --country_category "$country_category
fi
parameters_list=$parameters_list" --aws_access_key_id "$aws_access_key_id
parameters_list=$parameters_list" --aws_secret_access_key "$aws_secret_access_key
##Checking if the parameter is present then splitting the string and storing it into array
##Then for each order_area_name executing the python script in sequential manner
if [ "$order_area_names" != "$empty_string" ]
then
read -r -a order_area_name_array <<< "$order_area_names"
for order_area in "${order_area_name_array[@]}";
do
final_list=$parameters_list" --order_area_name "$order_area
echo $final_list
python3 ./main.py ${final_list}
done
fi
exit
我无法将 final_list 的值传递给 python 脚本,因此 Jenkin 作业失败。如果我回显 final_list,我会看到值已正确初始化:
--user jay@abc.com --mqs_level Q2 --num_parallel_pipelines 13 --sns_topic topicname --ramp_up_time 45 --max_duration_for_task 30 --batch_size 35 --lead_store_db_schema schema --airflow_k8s_web_server_pod_name airflow-web-xyz --aws_access_key_id 12345678 --aws_secret_access_key 12345678 --order_area_name London
错误看起来像这样:
main.py: error: the following arguments are required: --user, --sns_topic, --aws_access_key_id, --aws_secret_access_key
Build step 'Execute shell' marked build as failure
Finished: FAILURE
我在很多地方搜索过这个但是没有得到任何具体的答案。谁能帮我解决这个问题?
而不是像这样写我的命令:
python3 ./main.py ${final_list}
我用过这个,效果很好:
echo $final_list | bash
"final_list" 变量有需要执行的命令。
我已经设置了一个 Jenkins 参数化作业来使用 Jenkin 中的执行 shell 功能执行 python 脚本。该作业具有以下参数:
user-name: string, order_area_name: comma-separated strings, country_name: string, country_code: string, and so on...
我的用例是拆分 order_area_name 并按顺序为每个 order_area_name 执行 python 脚本。所以,我写了一个看起来像这样的脚本:
#!/bin/bash
export PYTHONHASHSEED=0
empty_string=""
parameters_list=""
IFS=","
#Checking every parameter if it is present or not
if [ "$user_name" != "$empty_string" ]
then
parameters_list=$parameters_list" --user "$user_name
fi
if [ "$country_code" != "$empty_string" ]
then
parameters_list=$parameters_list" --country_code "$country_code
fi
if [ "$country_category" != "$empty_string" ]
then
parameters_list=$parameters_list" --country_category "$country_category
fi
parameters_list=$parameters_list" --aws_access_key_id "$aws_access_key_id
parameters_list=$parameters_list" --aws_secret_access_key "$aws_secret_access_key
##Checking if the parameter is present then splitting the string and storing it into array
##Then for each order_area_name executing the python script in sequential manner
if [ "$order_area_names" != "$empty_string" ]
then
read -r -a order_area_name_array <<< "$order_area_names"
for order_area in "${order_area_name_array[@]}";
do
final_list=$parameters_list" --order_area_name "$order_area
echo $final_list
python3 ./main.py ${final_list}
done
fi
exit
我无法将 final_list 的值传递给 python 脚本,因此 Jenkin 作业失败。如果我回显 final_list,我会看到值已正确初始化:
--user jay@abc.com --mqs_level Q2 --num_parallel_pipelines 13 --sns_topic topicname --ramp_up_time 45 --max_duration_for_task 30 --batch_size 35 --lead_store_db_schema schema --airflow_k8s_web_server_pod_name airflow-web-xyz --aws_access_key_id 12345678 --aws_secret_access_key 12345678 --order_area_name London
错误看起来像这样:
main.py: error: the following arguments are required: --user, --sns_topic, --aws_access_key_id, --aws_secret_access_key Build step 'Execute shell' marked build as failure Finished: FAILURE
我在很多地方搜索过这个但是没有得到任何具体的答案。谁能帮我解决这个问题?
而不是像这样写我的命令:
python3 ./main.py ${final_list}
我用过这个,效果很好:
echo $final_list | bash
"final_list" 变量有需要执行的命令。