jenkins pipeline使用方式

pipeline 使用

使用groovy的一种DSL语言,流程控制 pipeline脚本同其他脚本语言一样,从上到下顺序执行,它的流程控制取决于Groovy表达式,为jenkins用户提供了更巨大的灵活性和可扩展性,本章内容围绕 devops [持续交付实践] pipeline

 

1.使用声明式写法

agent : 该agent部分指定整个Pipeline或特定阶段将在jenkins环境中执行的位置,具体取决于该agent部分的放置位置。该部分必须在pipeline块内的顶层定义。但stage级使用是可选的

  1):any、none、指定slave

    any 在任何可用的agent 上执行Pipeline或stage

              none 当在pipeline块的顶层使用none时,将不会为整个Pipeline运行分配全局agent ,每个stage部分将需要包含其自己的agent

              label 使用提供的label标签,在Jenkins环境中可用的代理上执行Pipeline或stage。例如:agent { label 'my-defined-label' } 

parameters:配置部署之前参数。

  1):string、password、choice

复制代码
def name="test1"
def test2(name1){
  println name1
if(name1 == "test3"){
    println "the is a test name1 is ${name}"
}else{
    println "the not is name1 the is ${name}"
    }

}
pipeline { agent any
  

    parameters {
       string(name: 'username', defaultValue: 'guozh10', description: 'username ')
        password(name: 'password', defaultValue: 'password', description: 'password')

         choice(name: 'ENV_TYPE', choices: editionName, description: 'test means test env,….')
    }

    stages {
        stage('Non-parallel Stage') {
            steps {
                echo 'This stage will be executed first.'
            }
        }
        stage('parakked Stage') {
            when {
                branch 'master'
            }
            parallel {
                stage('Branch A') {
                    agent {
                        label "for-branch-a"
                    }
                    steps {
                        echo "On Branch A"
                    }
                
                }
                stage('Branch B') {
                    agent {
                        label "for-branch-b"
                    }
                    steps {
                        echo "On Branch B"
                    }
                }
            }
        }
    }
}
复制代码

 

脚本式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
node ("master"){
    stage('Example') {
        try {
            sh 'exit 1'
        }
        catch (exc) {
            echo 'Something failed, I should sound the klaxons!'
            throw
        }
    }
}
node("slave01") {
    stage('Example') {
        try {
            sh 'exit 1'
        }
        catch (exc) {
            echo 'Something failed, I should sound the klaxons!'
            throw
        }
    }
}

  

 

posted @   扛把子修BUG  阅读(2865)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示