python - 更新文件中的值并忽略python中的变量是否没有输入
问题描述
我从詹金斯到 python 的变量很少。此 python 获取文件作为输入并检查特定内容并将值替换为通过 jenkins 提供的用户输入。
我的要求是,当没有通过 jenkins 提供输入时,文件中的值应该与以前相同。目前对我来说,它被替换为None
. 请就此提出建议。
提前致谢!
我的 Python 代码如下:
import fileinput
import os
username = os.getenv("username")
Password = os.getenv("Password")
hostname = os.getenv("hostname")
for line in fileinput.input('C:\\Jenkinsfile_test', inplace=True):
if line.startswith('def username'):
print "def username=",username
elif line.startswith('def Password'):
print "def Password=",Password
elif line.startswith('def hostname'):
print "def hostname=",hostname
else:
print line.strip()
我的 Jenkinsfile_test 如下,
def skipRemainingStages = false
def username=un
def Password=pwd
def hostname=host
pipeline {
agent any
stages {
stage('test') {
steps {
.......
}
}
}
}
解决方案
我刚刚添加了一个检查,确认环境已为每个条件提供了一个值。
for line in fileinput.input('C:\\Jenkinsfile_test', inplace=True):
if line.startswith('def username') and username is not None:
print "def username=",username
elif line.startswith('def Password') and Password is not None:
print "def Password=",Password
elif line.startswith('def hostname') and hostname is not None:
print "def hostname=",hostname
else:
print line.strip()
推荐阅读
- python - Elasticsearch 并行批量更新或索引优先
- sql - 我无法添加外键
- mysql - Laravel 将阿拉伯语保存到 MYSQL 数据库中
- git - Git 不暂存子目录中的 .mp4 文件
- 2sxc - 如何获取与模块或项目的上次更改相关的用户信息
- python - 使用快速 api 发布请求“422 无法处理的实体”
- git - 如何全面检查未保存的工作?
- node.js - 如何按存储在 mongoDB 中的降序格式化和排序日期?
- powershell - 为下一次 Read-Host 调用设置硬编码输入
- woocommerce - AlpineJS 使用 x-model 触发原生 JS 事件(woocommerce 购物车)