https://stackoverflow.com/questions/15125003/convert-string-variable-to-a-list-groovy/29675035
def ids = "[10, 1, 9]"
def l = Eval.me(ids)
参考:https://www.thinbug.com/q/44022775
很长时间以来,我一直在寻找答案,但我发现了一个破解方法!我把try / catch块放在了整个阶段:
try {
stage('some-stage') {
//do something
}
} catch (Exception e) {
echo "Stage failed, but we continue"
}
try {
stage("some-other-stage") { // do something }
} catch (Exception e) {
echo "Stage failed, but we still continue"
}
这仍然不是理想的,但是它给出了必要的结果。
https://stackoverflow.com/questions/63975674/groovy-lang-missingpropertyexception-no-such-property-props-for-class-groovy
问题:MissingPropertyException: No such property: props for class: groovy.lang.Binding
code: jenkins-pipeline
def jobes = [:]
def lists=["xls","doc","ole","exe86"]
/*def res_dic = readJSON text: '''
{
"xls":"xls/as",
"doc":"doc/as",
"ole":"ole/as",
"exe86":"exe86/as"
}
'''
*/
node("192.168.120.100"){
def props = readJSON file: '/home/sandbox/lj_test/code_backup/input.json'
}
def total=lists.size()
for (int i = 20; i >15; i--) {
//for (i in lists){
def sandbox_id = i
def sample=lists[i-20]
def res_d = res_dic["${sample}"]
def props_d = props["${sample}"]
解决:
Your definition of the variable props is inside the try-catch:
try {
def props = readJSON text: env.hb_job_params
...
}
But later, you try to use it in props.get(application_server)
and that variable does not exist anymore at that point
修改后:
def jobes = [:]
def lists=["xls","doc","ole","exe86"]
/*def res_dic = readJSON text: '''
{
"xls":"xls/as",
"doc":"doc/as",
"ole":"ole/as",
"exe86":"exe86/as"
}
'''
*/
def props = readJSON text: """ {} """
node("192.168.120.100"){
props = readJSON file: '/home/sandbox/lj_test/code_backup/input.json'
}
def total=lists.size()
for (int i = 20; i >15; i--) {
//for (i in lists){
def sandbox_id = i
def sample=lists[i-20]
def res_d = res_dic["${sample}"]
def props_d = props["${sample}"]
...
https://stackoverflow.com/questions/42380712/jenkins-pipeline-conditional-stage-succeeds-but-jenkins-shows-build-as-failed
问题:
MissingContextVariableException: Required context class hudson.FilePath is missing
code: jenkins-pipeline
def jobes = [:]
def lists=["xls","doc","ole","exe86"]
def props = readJSON file: '/home/sandbox/lj_test/code_backup/input.json'
def total=lists.size()
for (int i = 20; i >15; i--) {
def sandbox_id = i
def sample=lists[i-20]
def props_d = props["${sample}"]
...
}
解决:
So after looking more closely at the log file it helped me to track down the problem.
It's worth noting that clicking on the build stage to view the logs is what threw me - this is what I had been doing. When I actually went to the full console log output i saw the error about:
Required context class hudson.FilePath is missing
Perhaps you forgot to surround the code with a step that provides this, such as: node
Underneath the node {} section that I had I had a statement for deploys:
def branch = readFile('branch').trim()
if (branch == master) {
...
}
The problem was that the readFile statement was defined outside of a node.
The answer was to put the readFile statement within a node {} section.
参考:https://www.jenkins.io/doc/pipeline/examples/#jobs-in-parallel
def branches=[:] #定义并行的结构,相当于运行的容器吧,这后面的parallel branches想呼应
#下面定义每一个的branche
branches["branch1"] = {
node("192.168.100.121"){ //使用node指定这个job的运行位置
stage("run1"){ //stage是最后在运行结果页显示步骤列的位置用,这个测试要在执行脚本的上一级,但不能再node上
build job: 'param1', parameters:[ //要运行的其他job 及其参数
string(name:"hi",value:"1")
]}
}
}
branches["branch2"] = {
node("192.168.100.121I"){
stage("run2"){
build job: 'param2', parameters:[
string(name:"hi",value:"2")
]}
}
}
parallel branches
node(){ def hostid=2 sh "pwd" writeFile file: "_tmp", text: """ #!/bin/bash date +"%F_%T">_1 sleep 20 ps -ef|grep slim_engine_test|grep 'cfg=check_0ue.cfg -host=${hostid}'|awk '{print \$2;system(\"kill -9 \"\$2)}' date +"%F_%T">>_1 """ sh "nohup bash -x _tmp &" }
def check_packet_num = sh(returnStdout: true, script: "echo ${ue_path}/log") //script 使用‘’不支持外部变量,使用""支持外部变量
def labels = ['precise', 'trusty'] // labels for Jenkins node types we will build on def builders = [:] for (x in labels) { def label = x // Need to bind the label variable before the closure - can't do 'for (label in labels)' [popexizhi:这里x是地址引用,如果做列表复制,一定要做局部变量] // Create a map to pass in to the 'parallel' step so we can fire all the builds at once builders[label] = { node(label) { // build steps that should happen on all nodes go here } } } parallel builders
git branch: 'master', credentialsId: '12345-1234-4696-af25-123455', url: 'ssh://git@bitbucket.org:company/repo.git'
toInteger()
method to convert a String
to an Integer
, e.g.int value = "99".toInteger()
int value = "66" as Integer
String
can be converted before performing the conversion, useString number = "66"
if (number.isInteger()) {
int value = number as Integer
}