虚拟化容器,大数据,DBA,中间件,监控。

Jenkins 流水线语法 04 agent和post

21 12月
作者:admin|分类:系统运维

agent 构建节点


参数:

  • any: 运行在任一可用节点。
  • none:当pipeline全局指定agent为none,则根据每个stage中定义的agent运行(stage必须指定)。
  • label:在指定的标签的节点运行。(标签=分组)
  • node:支持自定义流水线的工作目录。
## 一
pipeline {
    agent any
}

## 二
pipeline {
    agent { label "label Name" }
}


## 三 自定义节点,加上node有一些高级的特性,比如自定义工作目录
pipeline {
  agent { 
     node {
        label "labelName",
        customWorkspace "/opt/agent/workspace"
     }
  }
}

Running on build-01 in /data/cicd/jenkinsagent/workspace/devops-maven-service

pipeline {
    agent {label "build"}

    stages {
        stage('Hello') {
            steps {
                script{
                    echo "The branch name is ${branchName}"  #这里变量是参数构建
                    sh "echo the job name is ${JOB_NAME}"
                }
            }
        }
    }

}



Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on build-01 in /data/cicd/jenkinsagent/workspace/pipeline-test
[Pipeline] {
[Pipeline] stage (hide)
[Pipeline] { (Hello)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
The branch name is master
[Pipeline] sh
+ echo the job name is pipeline-test
the job name is pipeline-test
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

post 构建后操作


定义: 根据流水线的最终状态匹配后做一些操作。

状态:

  • always: 不管什么状态总是执行
  • success: 仅流水线成功后执行
  • failure: 仅流水线失败后执行
  • aborted: 仅流水线被取消后执行
  • unstable:不稳定状态,单侧失败等等

Post就是构建后的操作

post就是根据流水线的状态去做一些事情,always是不管怎么样总是要做的一些事情

注意post可以写到stage里面,那么就是针对于某个阶段了,如果写在pipeline里面那么就是全局的,是针对整个流水线的状态,里面script不一定要写,但是运行了脚本方式的语法最好带上。

什么时候在stage里面写post?比如构建maven项目在单元测试之后,会产生一个报告,也就是单元测试成功之后可以将这个报告展示在jenkins上面,失败了就可以不让她展示了

在post always里面可以清理项目的空间,比如删除workspace的空间,在always里面的sript里面加入shell清理worksapce空间

pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                script{
                    echo "The branch name is ${branchName}"
                    sh "echo the job name is ${JOB_NAME}"
                }
            }
            post{
                always{
                    script{
                      echo "stage always.........."
                    }
                }
            }
        }
    }

post {
        always{
            script{
                println("流水线结束后,经常做的事情")
                echo "The job status is ${currentBuild.currentResult}"
            }
        }
        
        success{
            script{
                println("流水线成功后,要做的事情")
            }
        
        }
        failure{
            script{
                println("流水线失败后,要做的事情")
            }
        }
        
        aborted{
            script{
                println("流水线取消后,要做的事情")
            }
        
        }
    }
}




Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/pipeline-test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
The branch name is master
[Pipeline] sh
+ echo the job name is pipeline-test
the job name is pipeline-test
[Pipeline] }
[Pipeline] // script
Post stage
[Pipeline] script
[Pipeline] {
[Pipeline] echo
stage always..........
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
流水线结束后,经常做的事情
[Pipeline] echo
The job status is SUCCESS
[Pipeline] }
[Pipeline] // script
[Pipeline] script
[Pipeline] {
[Pipeline] echo
流水线成功后,要做的事情
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

浏览465 评论0
返回
目录
返回
首页
Jenkins 流水线语法 06 options 运行时选项和 parameters参数化构建 Jenkins 流水线语法 03 全局变量