jenkins插件(Dynamic Parameter )-----------groovy自动生成数字+1
给jenkins返回一个参数,每次点击Build whith Parameters tag参数+1
groovy脚本如下:
filepath = "/home/jenkins/tag_report/test_tag/report.txt"
def tagFile = new File(filepath);
if (!tagFile.exists()) {
tagFile.createNewFile();
}
//把读到的文件行内容全部存入List列表中
def list = new File(filepath).collect {it}
tag = (list[-1]);
def last_tag = tag.toInteger()
//在原有的数字上+1
last_tag ++;
def new_tag = last_tag.toString()
//限定数字格式,不到四位补齐四位
def num_tag = (new_tag.length());
println num_tag
if (num_tag<5) {
if (num_tag<4) {
if (num_tag<3) {
if (num_tag<2) {
def test_tag = new_tag.replaceAll ("[\\d]+", {it = it.toInteger() < 10000 ? "000"+it: it});
println test_tag
//把新得到的参数写进文件
new File(filepath).withWriter('utf-8') {
writer -> writer.writeLine test_tag
return test_tag
}
}else{
def test_tag = new_tag.replaceAll ("[\\d]+", {it = it.toInteger() < 10000 ? "00"+it: it});
println test_tag
//把新得到的参数写进文件
new File(filepath).withWriter('utf-8') {
writer -> writer.writeLine test_tag
return test_tag
}
}
}else{
def test_tag = new_tag.replaceAll ("[\\d]+", {it = it.toInteger() < 10000 ? "0"+it: it});
println test_tag
//把新得到的参数写进文件
new File(filepath).withWriter('utf-8') {
writer -> writer.writeLine test_tag
return test_tag
}
}
}else{
def test_tag = new_tag.replaceAll ("[\\d]+", {it = it.toInteger() < 10000 ? ""+it: it});
println test_tag
//把新得到的参数写进文件
new File(filepath).withWriter('utf-8') {
writer -> writer.writeLine test_tag
return test_tag
}
}
}else{
def test_tag = new_tag.replaceAll ("[\\d]+", {it = it.toInteger() < 10000 ? ""+it: it});
println test_tag
//把新得到的参数写进文件
new File(filepath).withWriter('utf-8') {
writer -> writer.writeLine test_tag
return test_tag
}
}
补齐那里怪怪的,但是也能实现。
同仁们有更好的方法吗?