Jenkins Pipeline(7)

Pipeline 流水线

Jenkins 流水线是一套插件,用来实现CI / CD pipeline

Pipeline Plugin

Pipeline Plugin是Jenkins初次启动时,推荐安装的插件之一。如果之前使用了默认安装,此时应该已经加载有Pipeline Plugin

  • Manage Jenkins > Manage Plugins > Installed Tab
  • 在filter框内输入Pipeline,查看Pipeline plugin

Pipeline Stage View Plugin

用来显示pipeline stage 的视图插件

  • Manage Jenkins > Manage Plugins > Installed Tab
  • 在filter框内输入Pipeline,安装 Pipeline Stage View
  • 重启Jenkins服务器

Declarative Pipeline 声明式流水线

pipeline {
    agent any

    stages {
        stage('Build') { 
            steps {
                echo 'Building ...'
            }
        }
        stage('Test') { 
            steps {
                echo 'Testing ...'
            }
        }
        stage('Deploy') { 
            steps {
                echo 'Deploying ...'
            }
        }
    }
}
  1. Declarative Pipeline jenkinsfile 总是从pipeline开始。 pipeline 流水线是最基础的概念模型。定义了整个流水线的构建过程, 通常包括构建, 测试和交付等步骤(指令块block)。
  2. agent 声明Jenkins为整个流水线分配一个执行器 (在节点上)和工作区。any表示在任何可用的代理上执行。agent一般定义在pipeline 块的顶层, 但是每个stage可以有自己的agent。
  3. stages可以封装多个stage指令。
  4. stage定义了在整个流水线的不同阶段(比如BuildTestDeploy),可以包含多个步骤。
  5. steps 包含了一串执行列表

Pipeline project

New Item > Pipeline (name: pipeline-test)

  • pipeline section,填入上面的jenkinsfile
  • 保存
  • Build
  • 查看每阶段log
  • 查看输出

multi-steps pipeline

执行多个步骤的案例 sh ''' xxxx '''

pipeline {
    agent any

    stages {
        stage('Build') { 
            steps {
                sh 'echo "multi-steps pipeline"'
                sh '''
                    echo "Hello World"
                    whoami
                    ls -l
                '''
            }
        }
    }
}

 

可以看到输出的信息中,jenkins的liunx用户使用的就是jenkins

失败重试

 使用retry(3)包裹起来的代码出现错误,会让他重试执行3次

pipeline {
    agent any

    stages {
        stage('Timeout') { 
            steps {
                retry(3) {
                    sh 'timeout failure...'
                }
            }
        }
    }
}

失败超时

这里timeout:time:3(代表只允许代码块执行时间在3秒) unit:'SECONS'(代表秒单位)

sh 'sleep 5' ,让他休眠5秒,这里就会因为时间超时而导致执行失败。

pipeline {
    agent any

    stages {
        stage('Deploy') { 
            steps {
                retry(3) {
                    sh 'echo hello_world'
                }

                timeout(time: 3, unit: 'SECONDS') {
                    sh 'sleep 5'
                }
            }
        }
    }
}

环境变量

可以通过environment设置环境变量

pipeline {
    agent any

    environment {
        NAME1 = 'Tom'
        NAME2 = 'Jerry'
    }

    stages {
        stage('Build') { 
            steps {
                sh 'echo "Hello, $NAME1 and $NAME2"'
            }
        }
    }
}

post actions

post代表构建之后执行的内容

always:代表不管上面stages执行失败或者成功,都会要执行的操作

success:代表只有上面stages执行成功才会执行

failure:代表只有上面stages执行失败才会执行

这里使用exit 1;代表让执行变为失败。所以这里之后执行always和failure

pipeline {
    agent any

    stages {
        stage('Test') { 
            steps {
                sh 'echo "test fail!"; exit 1'
            }
        }
    }

    post {
        always {
            echo 'always executed'
        }
        success {
            echo 'executed if this test success'
        }
        failure {
            echo 'executed if this test fails'
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值