jenkins上添加text parameter, 用脚本读取并存到配置文件中

来源:互联网 发布:软件可否申请专利 编辑:程序博客网 时间:2024/06/06 09:35

1  功能需求

             在jenkins上设置这些参数  即类似于设置系统变量   只不过设置name:custom_config / value : text parameter 而不是string parameter

            自定义设置配置文件中一些session/value,可以不用再创建一个配置文件,以及在运行时载入这个配置文件( eg....  run.bat  -C custom_config.cfg)

            现直接在jenkins上设置text parameter ,  再通过脚本读取text paramenter 并与已有的配置文件自动合并 运行

2  达到效果




              最终生成的配置文件:

#!/usr/bin/env python# -*- coding: utf-8 -*-from os import environ, linesepfrom ConfigParser import ConfigParserfrom StringIO import StringIOfrom pdb import set_traceimport StringIOdefault_version = '0.0.0'def add_val2section(cfg, section, name, value):if value and value != default_version:cfg.add_section(section)cfg.set(section, name, value)else:print('[warning] {0}\'s version is not set in environment!'.format(section))def build_sal_regression_cfg(env):productname = env.get('SAL_product_name', 'sc')sal_pattern_version = env.get('SAL_PATTERN_VERSION', default_version)sal_build_version = env.get('SAL_BUILD_VERSION', default_version)bep_build_version = env.get('BEP_BUILD_VERSION', default_version)bep_pattern_version = env.get('BEP_PATTERN_VERSION', default_version)custom_config = env.get('CUSTOM_CONFIG', '')cfg_name = r'{0}.cfg'.format(productname)cfg = ConfigParser()# read the configuration of the productcfg.read(cfg_name)add_val2section(cfg, 'test_info', 'productname', productname)add_val2section(cfg, 'pattern_sal', 'version', sal_pattern_version)add_val2section(cfg, 'pattern_bep', 'version', bep_pattern_version)add_val2section(cfg, 'build_win32_sal', 'version', sal_build_version)add_val2section(cfg, 'build_win64_sal', 'version', sal_build_version)add_val2section(cfg, 'build_win32_bep', 'version', bep_build_version)add_val2section(cfg, 'build_win64_bep', 'version', bep_build_version)# read custom_configif custom_config:buf = StringIO.StringIO(custom_config)cfg.readfp(buf)return cfgdef build_cfg(env):test_name = env.get('SAL_test_name', 'sal_regression')return build_sal_regression_cfg(env)# raise Exception('SAL_test_name({0}) is not supported'.format(test_name))def main(cfg_file_path):#print environwith open(cfg_file_path, 'w') as f:build_cfg(environ).write(f)passif __name__ == '__main__':main('temp.cfg')print 'creat-cfg-py'


1 0
原创粉丝点击