简体   繁体   English

使用 Jenkins 参数化管道执行 python 脚本

[英]Executing python script using Jenkins parameterized pipeline

I have set up a Jenkins parameterized job to execute python script using execute shell feature in Jenkin.我已经使用 Jenkins 中的执行 shell 功能设置了 Jenkins 参数化作业来执行 python 脚本。 The job has the following parameters:该作业具有以下参数:

user-name: string, order_area_name: comma-separated strings, country_name: string, country_code: string, and so on... user-name:字符串,order_area_name:逗号分隔的字符串,country_name:字符串,country_code:字符串,等等...

My use case is to split the order_area_name and execute the python script for every order_area_name sequentially.我的用例是拆分 order_area_name 并为每个 order_area_name 依次执行 python 脚本。 So, I wrote a script that looks something like this:所以,我写了一个看起来像这样的脚本:

#!/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

I am not able pass the final_list of values to the python script because of which the Jenkin job is failing.我无法将值的final_list传递给 python 脚本,因此 Jenkins 作业失败。 If I echo the final_list I see that the values are properly getting initialized:如果我回显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 --user jay@abc.com --mqs_level Q2 --num_parallel_pipelines 13 --sns_topic 主题名 --ramp_up_time 45 --max_duration_for_task 30 --batch_size 35 --lead_store_db_schema 架构 --airflow_k8s_z344aws67y_name 气流-keyid_web-x12 aws_secret_access_key 12345678 --order_area_name 伦敦

The error looks something like this:错误看起来像这样:

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 main.py:错误:需要以下 arguments:--user、--sns_topic、--aws_access_key_id、--aws_secret_access_key 构建步骤“执行 shell”将构建标记为失败已完成:失败

I searched for this at a lot of places but didn't get any concrete answer for this.我在很多地方搜索过这个,但没有得到任何具体的答案。 Could anyone please help me with this?谁能帮我解决这个问题?

Instead of writing my command like:而不是像这样写我的命令:

python3./main.py ${final_list}

I used this and it worked very well:我用了这个,效果很好:

echo $final_list | bash

"final_list" variable has the command which needs to be executed. “final_list”变量具有需要执行的命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM