美文网首页
jenkins pipeline script basic

jenkins pipeline script basic

作者: AppleLin_cdea | 来源:发表于2021-10-14 10:56 被阅读0次

    2021年10月14日10:29:56 星期四
    pipeline script groovy 基础,注意点

    错误代码示例 1. 传参没有注意“字符串”要加引号

    Definition Pipeline script

    node(demo_slave)
    {
        stage('show_ip')
        {
            sh """!/bin/bash -xe
                ip -o -4 a
            """
        }
    }
    

    运行日志

    Console Output
    10:25:02 Started by user apple
    10:25:02 Running in Durability level: MAX_SURVIVABILITY
    10:25:02 [Pipeline] Start of Pipeline
    10:25:02 [Pipeline] End of Pipeline
    10:25:03 hudson.remoting.ProxyException: groovy.lang.MissingPropertyException: No such property: demo_slave for class: WorkflowScript
    10:25:03 at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
    

    错误显示 No such property: demo_slave
    实际上配置了 jenkins slave node 名字是 'demo_slave',
    如果要在 script 中 使用 node(node_name)来用 demo_slave,
    要注意加引号 node('demo_slave'),
    如果不加引号,就表示引用的是变量,如果没有定义变量,就会报上面的错误。

    修改代码

    node_name='demo_slave'
    node(node_name)
    {
        stage('show_ip')
        {
            sh """!/bin/bash -xe
                ip -o -4 a
            """
        }
    }
    

    运行 Console Output

    10:40:15 Started by user apple
    10:40:15 Running in Durability level: MAX_SURVIVABILITY
    10:40:15 [Pipeline] Start of Pipeline
    10:40:15 [Pipeline] node
    10:40:15 Running on demo_slave in /home/jenkins/workspace/demo_slave
    10:40:15 [Pipeline] {
    10:40:16 [Pipeline] stage
    10:40:16 [Pipeline] { (show_ip)
    10:40:16 [Pipeline] sh
    10:40:16 + !/bin/bash -xe
    10:40:16 /home/jenkins/workspace/demo_slave@tmp/durable-04a6f628/script.sh: 1: /home/jenkins/workspace/demo_slave@tmp/durable-04a6f628/script.sh: !/bin/bash: not found
    10:40:16 [Pipeline] }
    10:40:16 [Pipeline] // stage
    10:40:17 [Pipeline] }
    10:40:17 [Pipeline] // node
    10:40:17 [Pipeline] End of Pipeline
    10:40:18 ERROR: script returned exit code 127
    10:40:18 Finished: FAILURE
    

    这个报错是 !/bin/bash 前面少了个 # 注释符

    正确代码

    node_name='demo_slave'
    node(node_name)
    {
        stage('show_ip')
        {
            sh """#!/bin/bash -xe
                ip -o -4 a
            """
        }
    }
    

    Console Output

    10:46:54 Started by user apple
    10:46:54 Running in Durability level: MAX_SURVIVABILITY
    10:46:55 [Pipeline] Start of Pipeline
    10:46:55 [Pipeline] node
    10:46:55 Running on target_ubuntu_h620_182 in /home/jenkins/workspace/demo
    10:46:55 [Pipeline] {
    10:46:55 [Pipeline] stage
    10:46:55 [Pipeline] { (show_ip)
    10:46:55 [Pipeline] sh
    10:46:56 + ip -o -4 a
    10:46:56 1: lo inet 127.0.0.1/8 scope host lo\ valid_lft forever preferred_lft forever
    10:46:56 2: eno1 inet 192.168.1.8/24 brd 192.168.1.255 scope global dynamic eno1\ valid_lft 1249488sec preferred_lft 1249488sec
    10:46:56 [Pipeline] }
    10:46:56 [Pipeline] // stage
    10:46:56 [Pipeline] }
    10:46:56 [Pipeline] // node
    10:46:56 [Pipeline] End of Pipeline
    10:46:57 Finished: SUCCESS
    

    总结

    1. 注意语法规范(使用变量,使用字符串,不能搞混了)
    2. 要细心,程序是严格遵循语法的,你写错了一个标点、多写了、少写了,程序是会报错,它是不讲人情,六亲不认的。所以写代码编程要严谨。
    3. 重要的事说三遍:基础很重要,多听、多读、多写,就是练习,练习,再练习。刻意练习难题,把心思精力放在上面,思考。知其然,知其所以然。
    4. 遵循定理原理,发挥想象探索。

    错误代码 2:要注意不能使用无中生有(没有定义的)变量

    node_name='demo_slave'
    node(node_name)
    {
        stage('log')
        {
            script.archiveArtifacts artifacts: "logs/*", fingerprint: true
        }
    }
    

    Console Output

    11:39:00 Started by user apple
    11:39:00 Running in Durability level: MAX_SURVIVABILITY
    11:39:00 [Pipeline] Start of Pipeline
    11:39:00 [Pipeline] node
    11:39:01 Running on target_ubuntu_518_dm1_100 in /home/jenkins/workspace/demo
    11:39:01 [Pipeline] {
    11:39:01 [Pipeline] stage
    11:39:01 [Pipeline] { (log)
    11:39:01 [Pipeline] }
    11:39:01 [Pipeline] // stage
    11:39:02 [Pipeline] }
    11:39:02 [Pipeline] // node
    11:39:02 [Pipeline] End of Pipeline
    11:39:02 hudson.remoting.ProxyException: groovy.lang.MissingPropertyException: No such property: script for class: WorkflowScript
    

    报错显示 No such property: script for class: WorkflowScript
    说明 script 这个是无中生有(没有定义的变量),在 WorkflowScript 空间直接使用 archiveArtifacts 就可以

    正确代码

    node_name='demo_slave'
    node(node_name)
    {
        stage('log')
        {
            archiveArtifacts artifacts: "logs/*", fingerprint: true
        }
    }
    

    Console Output

    11:48:22 Started by user apple
    11:48:22 Running in Durability level: MAX_SURVIVABILITY
    11:48:22 [Pipeline] Start of Pipeline
    11:48:22 [Pipeline] node
    11:48:22 Running on demo_slave in /home/jenkins/workspace/demo
    11:48:22 [Pipeline] {
    11:48:23 [Pipeline] stage
    11:48:23 [Pipeline] { (log)
    11:48:23 [Pipeline] archiveArtifacts
    11:48:23 Archiving artifacts
    11:48:23 Recording fingerprints
    11:48:25 [Pipeline] }
    11:48:25 [Pipeline] // stage
    11:48:25 [Pipeline] }
    11:48:25 [Pipeline] // node
    11:48:25 [Pipeline] End of Pipeline
    11:48:27 Finished: SUCCESS
    

    错误3: map 要先定义,后设置

    错误代码

    import groovy.transform.Field
    
    @Field def g_detailed_results = [:]
    class CLOG implements Serializable
    {
        def script
        
        def CLOG(WorkflowScript)
        {
            this.script = WorkflowScript
        }
    
        def test_map()
        {
            def mode_result='COV'
            script.g_detailed_results["${mode_result}"]['KEY'] = 'key'
        }
    }
    
    node_name='demo_slave'
    node(node_name)
    {
        stage('log')
        {
            clog=new CLOG(this)
            clog.test_map()
        }
    }
    

    Console Output

    15:18:20 Started by user apple
    15:18:20 Running in Durability level: MAX_SURVIVABILITY
    15:18:20 [Pipeline] Start of Pipeline
    15:18:20 [Pipeline] node
    15:18:20 Running on demo_slave in /home/jenkins/workspace/demo
    15:18:21 [Pipeline] {
    15:18:21 [Pipeline] stage
    15:18:21 [Pipeline] { (log)
    15:18:21 [Pipeline] }
    15:18:21 [Pipeline] // stage
    15:18:21 [Pipeline] }
    15:18:21 [Pipeline] // node
    15:18:22 [Pipeline] End of Pipeline
    15:18:23 java.lang.NullPointerException: Cannot set property 'KEY' on null object
    

    报错 Cannot set property 'KEY' on null object,
    因为 g_detailed_results["${mode_result}"] 没有定义,所以它是 null object,我们要把它当 map 使用,所以就要在使用前定义它

    script.g_detailed_results["${mode_result}"] = [:]
    

    正确代码

    import groovy.transform.Field
    
    @Field def g_detailed_results = [:]
    class CLOG implements Serializable
    {
        def script
        
        def CLOG(WorkflowScript)
        {
            this.script = WorkflowScript
        }
    
        def test_map()
        {
            def mode_result='COV'
            script.g_detailed_results["${mode_result}"] = [:]
            script.g_detailed_results["${mode_result}"]['KEY'] = 'key'
        }
    }
    
    node_name='demo_slave'
    node(node_name)
    {
        stage('log')
        {
            clog=new CLOG(this)
            clog.test_map()
        }
    }
    

    Console Output

    15:35:35 Started by user apple
    15:35:35 Running in Durability level: MAX_SURVIVABILITY
    15:35:35 [Pipeline] Start of Pipeline
    15:35:36 [Pipeline] node
    15:35:36 Running on demo_slave in /home/jenkins/workspace/demo
    15:35:36 [Pipeline] {
    15:35:36 [Pipeline] stage
    15:35:36 [Pipeline] { (log)
    15:35:37 [Pipeline] }
    15:35:37 [Pipeline] // stage
    15:35:37 [Pipeline] }
    15:35:37 [Pipeline] // node
    15:35:37 [Pipeline] End of Pipeline
    15:35:37 Finished: SUCCESS
    

    总结

    不能想那当然的使用无中生有的代码(没有定义的代码),要遵守编程规范,不然,就会出低级和莫名其妙的问题。
    注意基础,注意仔细。

    相关文章

      网友评论

          本文标题:jenkins pipeline script basic

          本文链接:https://www.haomeiwen.com/subject/bkiyoltx.html