html tool

显示标签为“pipeline”的博文。显示所有博文
显示标签为“pipeline”的博文。显示所有博文

2021年7月14日星期三

转:Convert String variable to a List [Groovy]

https://stackoverflow.com/questions/15125003/convert-string-variable-to-a-list-groovy/29675035

 def ids = "[10, 1, 9]"

def l = Eval.me(ids)


转:jenkins pipeline string to integer

 https://stackoverflow.com/questions/1713481/converting-a-string-to-int-in-groovy

Use the toInteger() method to convert a String to an Integer, e.g.

int value = "99".toInteger()

2021年4月14日星期三

pipeline的stage显示失败但继续运行

 参考: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"
 }

结果是您将获得以下内容: enter image description here

这仍然不是理想的,但是它给出了必要的结果。

2021年4月7日星期三

解决:MissingPropertyException: No such property: props for class: groovy.lang.Binding

 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}"]

...

解决:MissingContextVariableException: Required context class hudson.FilePath is missing

 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.



2020年9月28日星期一

jenkins pipeline 并行eg

参考: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

2019年2月18日星期一

linux 同时安装python2和python3 pip冲突问题

参考:https://www.zhihu.com/question/21653286

pope安装的是python36

#yum install python36-pip

# python36 -m pip -V
pip 8.1.2 from /usr/lib/python3.6/site-packages (python 3.6)

之后 比如安装依赖
# python36 -m pip install requests

查看3的安装列表
# python36 -m pip list
certifi (2018.11.29)
chardet (3.0.4)
idna (2.8)
pip (8.1.2)
requests (2.21.0)
setuptools (39.2.0)
urllib3 (1.24.1)

查看2的列表,依然用默认的就ok
# pip list
Package                          Version  
-------------------------------- ---------
altgraph                         0.16.1   
asn1crypto                       0.24.0   
certifi                          2018.4.16
cffi                             1.11.5   
chardet                          3.0.4    
configobj                        4.7.2    
cryptography                     2.3.1    
decorator                        3.4.0    
dis3                             0.1.2    
dnspython                        1.15.0   
elasticsearch                    1.9.0    
enum34                           1.1.6    
future                           0.17.1   
idna                             2.7      
iniparse                         0.4      
ipaddress                        1.0.22   
IPy                              0.75     
javapackages                     1.0.0    
lxml                             3.2.1    
macholib                         1.11     
MySQL-python                     1.2.5    
pefile                           2018.8.8 


2017年7月4日星期二

pipeline use bash


问题:使用pipeline的sh无法使用bash 运行脚本很郁闷

go了半天也没解决,现在发现迂回一下使用writeFile把要执行的写入文件,使用bash 调用就ok了,

如下:

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 &"
    
}

pipeline bulid other job 忽略或记录测试结果

忽略测试结果:
 
pipeline bulid other job  忽略err , propagate: false

check_res[i]=build job: 'check_0package_ue', parameters: [string(name: 'message', value: reslist[i]) ], propagate: false


2017年7月3日星期一

pipeline How to do I get the output of a shell command executed using into a variable from Jenkinsfile (groovy)?


参考:
https://stackoverflow.com/questions/36547680/how-to-do-i-get-the-output-of-a-shell-command-executed-using-into-a-variable-fro
https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#code-sh-code-shell-script


--------------------
def check_packet_num = sh(returnStdout: true, script: "echo ${ue_path}/log")  //script 使用‘’不支持外部变量,使用""支持外部变量



2017年6月15日星期四

pipeline 并行处理多个节点 job

https://jenkins.io/doc/pipeline/examples/#jobs-in-parallel
问题:
并行处理多个节点 job,node在数组中添加

Parallel Multiple Nodes 

Synopsis
This is a simple example showing how to succinctly parallel the same build across multiple Jenkins nodes. This is useful for e.g. building the same project on multiple OS platforms.
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

2017年6月14日星期三

pipeline script中指定git 的branch clone


参考:https://stackoverflow.com/questions/38461705/checkout-jenkins-pipeline-git-scm-with-credentials

You can use the following in a pipeline:
git branch: 'master', credentialsId: '12345-1234-4696-af25-123455', url: 'ssh://git@bitbucket.org:company/repo.git'

2017年5月2日星期二

pipeline : string2int

add:http://stackoverflow.com/questions/1713481/groovy-string-to-int

Use the toInteger() method to convert a String to an Integer, e.g.
int value = "99".toInteger()
An alternative, which avoids using a deprecated method (see below) is
int value = "66" as Integer
If you need to check whether the String can be converted before performing the conversion, use
String number = "66"

if (number.isInteger()) {
  int value = number as Integer
}