我有一个要使用 ruamel 更改的 yaml 文件。如果部署只包含一个环境,我可以更改它。问题是我想添加/更改多个环境。这是一个示例代码,它适用于使用 2 个参数的一个环境。
示例.py
import ruamel.yaml
import sys
file_name = 'Jenkinsfile.yaml'
from ruamel.yaml.util import load_yaml_guess_indent
config, ind, bsi = load_yaml_guess_indent(open(file_name))
totalArgs = len(sys.argv) - 1
print(totalArgs)
print(sys.argv[1:])
if totalArgs == 2:
deploy = config['deploy']
deploy[0]['env'] = sys.argv[1]
deploy[0]['org'] = sys.argv[2]
elif totalArgs == 4:
deploy = config['deploy']
deploy[0]['env'] = sys.argv[1]
deploy[0]['org'] = sys.argv[2]
deploy[0]['env'] = sys.argv[3]
deploy[0]['org'] = sys.argv[4]
deploy[0]['host'] = 'https://**.com'
deploy[0]['loc'] = 'saas'
deploy[0]['manifest'] = 'manifest_dev.yml'
ruamel.yaml.round_trip_dump(config, open('Jenkinsfile.yaml', 'w'),
indent=ind, block_seq_indent=bsi)
结果
sample.py 仿真测试
deploy:
- env: mitul-trial (Will change)
org: test (Will change)
host: https://**.com (will remain same)
loc: saas (will remain same)
manifest: manifest_dev.yml (will remain same)
approval: true (will remain same)
想要的结果:
sample.py mitul-trial 测试 mitul1 testsomething
deploy:
- env: mitul-trial (Will change)
org: test (Will change)
host: https://**.com (will remain same)
loc: saas (will remain same)
manifest: manifest_dev.yml (will remain same)
approval: true (will remain same)
- env: mitul1 (Will change)
org: testsomething (Will change)
host: https://**.com (will remain same)
loc: saas (will remain same)
manifest: manifest_dev.yml (will remain same)
approval: true (will remain same)