SuRenPi

  • 博客
  • 翻译
  • 开源
  • Slide

此页面上的内容

  • 触发器
  • 参数化执行
  • 异常
  • 循环
  • HTTP

脚本式流水线

本文介绍 Jenkins 的脚本式流水线

触发器

codes/jenkinsfile/script-trigger.groovy
node('local') {
	echo 'hello'
}

properties([
	buildDiscarder(
		logRotator(
			artifactDaysToKeepStr: '',
			artifactNumToKeepStr: '',
			daysToKeepStr: '5',
			numToKeepStr: '10'
		)
	),
	pipelineTriggers([
		cron('H 3,12,17 * * *'),
		scm('H 3,12,17 * * *')
	])
])
原文件

参数化执行

properties([[$class: 'JobRestrictionProperty'],
	parameters([run(description: '',
		filter: 'ALL', 
		name: 'Name', 
		projectName: 'Project')]),
	pipelineTriggers([])]
)

异常

你可以利用处理异常的方式来实现类似于申明式流水线中的 post

codes/jenkinsfile/try-catch.groovy
node {
    stage("one"){
        try {
            echo "hello"
        }catch(error){
            echo error.getMessage()
        }finally{
            echo "finally"
        }
    }
}
原文件

循环

codes/jenkinsfile/loop.groovy
node('suren') {
    def dev_path = '/opt/suren/bin'
    def services = [
        [
            'name': 'admin',
            'project': 'admin',
            'port': '7002',
            'jarName': 'admin'
        ]
    ];
    
    stage('Copy Artifact') {
        for(service in services){
            step([$class: 'CopyArtifact', fingerprintArtifacts: true, flatten: true,
                projectName: service.project,
                selector: [$class: 'StatusBuildSelector', stable: false],
                target: dev_path + '/' + service.name
            ])
        }
    }
    
    stage('Stop Service') {
        for(service in services){
           sh 'fuser -n tcp -k ' + service.port + ' > redirection &'
        }
    }
    
    stage('Start Service') {
        for(service in services){
            sh 'cd ' + pass_bin + '/' + service.name + ' && nohup nice java -server -Xms128m -Xmx384m \
                -jar ' + service.jarName + '.jar \
                --server.port=' + service.port + ' $> initServer.log 2>&1 &'
        }
    }
}
原文件

HTTP

我们有时候需要在流水线中发送 HTTP 请求,下面给出一个例子:

codes/jenkinsfile/http.groovy
// dependecy plugin is https://plugins.jenkins.io/phoenix-autotest
pipeline {
    agent {
        label "master"
    }
    
    stages {
      stage('test') {
          steps{
              script {
                  http url: "http://baidu.com"
                  http url: "http://baidu.com", responseBody: "body.txt"
                  
                  archiveArtifacts "body.txt"
                  
                  def quy = 'https://www.baidu.com/s?wd=' + URLEncoder.encode('a b', 'UTF-8')
                  echo quy
                  http url: quy
              }
          }
      }
    }
}
原文件

参考

  • Jenkins 插件调试
  • Jenkins
  • Action
  • Annotation
  • CLI
  • DevOps
    • Jenkins
    • 容器技术
    • API
    • General topic About DevOps
    • Jenkins X
    • Kubernetes
    • Team
    • 构建
  • 编程语言
    • Golang
    • Java
    • Python
    • TypeScript
  • 操作系统
    • Linux
    • Mac
  • 其他内容
    • BananaPi
    • SCM
    • 代码质量
    • 包管理器
    • 应用服务
    • 推荐信息
    • 数据库
    • 文档技术
    • 测试
    • 浏览器
    • 终端
    • 编辑器
    • 网络
    • 配置文件
“脚本式流水线” was last updated: January 1, 0001
改善此页
  • Tags:
  • Jenkins
  • Pipeline
  • Jenkins
  • Related entries:
  • Jenkins Permission
  • 申明式流水线
由 SuRen 维护
  • File an Issue
 

Copyright 2018–2022 the original authors.

  • 博客
  • 翻译
  • 开源
  • Slide