当前位置: 首页 > news >正文

python(77) python脚本与jenkins pipeline交互的5种方式

1. 在 Pipeline 中执行 Python 脚本并获取输出

在 Jenkins Pipeline 中,利用sh(适用于 Linux、macOS)或bat(适用于 Windows)步骤来运行 Python 脚本,并通过returnStdout参数捕获脚本的标准输出。

groovy

pipeline {agent anystages {stage('执行Python脚本') {steps {script {def pythonOutputif (isUnix()) {// 在Linux或macOS上执行pythonOutput = sh(script: 'python3 your_script.py || python your_script.py', returnStdout: true).trim()} else {// 在Windows上执行pythonOutput = bat(script: 'python your_script.py', returnStdout: true).trim()}echo "Python脚本输出: ${pythonOutput}"}}}}
}

说明your_script.py是需要执行的 Python 脚本,returnStdout: true表示获取脚本的标准输出,.trim()用于去除输出结果中的首尾空格和换行符。

python脚本打印print信息默认会显示在pipeline console output中,实时输出需要加-u参数:

python -u demo.py

2. 向 Python 脚本传递参数

可以在执行 Python 脚本时,通过命令行参数的形式将 Pipeline 中的变量传递给 Python 脚本,Python 脚本通过sys.argv获取这些参数。

  • Jenkins Pipeline 脚本

groovy

pipeline {agent anystages {stage('传递参数给Python脚本') {steps {script {def paramValue = "Hello from Pipeline"if (isUnix()) {sh "python3 your_script.py ${paramValue}"} else {bat "python your_script.py ${paramValue}"}}}}}
}
  • Python 脚本(your_script.py

python

运行

import sys
if len(sys.argv) > 1:received_param = sys.argv[1]print(f"接收到的参数: {received_param}")

说明:在 Pipeline 中定义变量paramValue,并将其作为参数传递给 Python 脚本,Python 脚本通过sys.argv获取并处理该参数。

3. 使用 Python 库与 Jenkins 进行远程交互

可以使用python-jenkins库在 Python 脚本中远程调用 Jenkins 的 API,实现诸如触发构建、获取构建状态等操作。

  • 安装python-jenkinspip install python-jenkins
  • Python 脚本示例

python

运行

import jenkinsserver = jenkins.Jenkins('http://your_jenkins_url', username='your_username', password='your_password')
job_name = 'your_job_name'
# 触发构建
server.build_job(job_name)
# 获取构建信息
last_build_number = server.get_job_info(job_name)['lastBuild']['number']
build_info = server.get_build_info(job_name, last_build_number)
print(f"构建状态: {build_info['result']}")

说明:此方式适用于需要在 Python 脚本中主动操作 Jenkins 任务的场景,通过python-jenkins库提供的接口与 Jenkins 服务器进行通信。

4. 通过文件共享数据

在 Pipeline 中,可以使用writeFile步骤将数据写入文件,Python 脚本读取该文件获取数据;反之,Python 脚本也可以将处理结果写入文件,Pipeline 再通过readFile步骤读取。

  • Jenkins Pipeline 脚本

groovy

pipeline {agent anystages {stage('写入数据到文件') {steps {script {def dataToWrite = "Some important data"writeFile file: 'data.txt', text: dataToWrite}}}stage('执行Python脚本处理文件数据') {steps {script {if (isUnix()) {sh 'python3 process_data.py'} else {bat 'python process_data.py'}}}}stage('读取Python脚本处理后的结果') {steps {script {def result = readFile file: 'output.txt'echo "处理结果: ${result}"}}}}
}
  • Python 脚本(process_data.py

python

运行

with open('data.txt', 'r') as file:input_data = file.read()
# 处理数据
processed_data = input_data.upper()
with open('output.txt', 'w') as output_file:output_file.write(processed_data)

说明:Pipeline 先将数据写入文件,Python 脚本读取并处理后再写回文件,最后 Pipeline 读取处理后的结果文件。

这些方式各有适用场景,可根据具体需求选择合适的交互方式,实现 Jenkins Pipeline 与 Python 脚本之间高效的数据交互和功能协作。

5.通过数据库共享数据mysql, redis

http://www.dtcms.com/a/423964.html

相关文章:

  • 网站多久备案一次百度灰色关键词排名
  • 内江建设局网站注册网站刀具与钢材范围
  • 如何将安卓应用迁移到鸿蒙?
  • wordpress商业网站wordpress虚拟
  • dede 网站名称 空的团员建设网站
  • C++(Qt)软件调试---Linux动态库链接异常排查(38)
  • 记录 Qt 跨线程 信号无法触发槽函数问题
  • wireshark 01——安装
  • 网上最好购物网站邯郸网上销售公司
  • 使用top域名做网站seo职位是什么意思
  • CUDIS 健康协议在 Sui 上打造更健康的未来
  • 装修网站排行榜前十名有哪些南昌网站建设哪家最好
  • Golang学习笔记:context的使用场景
  • 带有客户案例的网站广州专业网站建设报价
  • 昆明微信网站建设软件开发模型有几种并简述其特点
  • 高效实现实体删除的宏解决方案:使用Rust宏优化删除操作
  • Rust泛型详解
  • 官方手表网站网站专题分类
  • 新乡网站建设方案搜狗网址大全下载安装
  • 关于可视化卷积核和特征图的深度理解
  • 【mysql】Mybatisplus BINARY {0} LIKE CONCAT(‘%‘, {1}, ‘%‘)写这句话是什么意思
  • 开发避坑指南(59):Vue3中高效删除数组元素的方法
  • wordpress建站要用模板吗wordpress搜索筛选
  • 安卓 WPS Office v18.21.0 国际版
  • 衡阳网站推广优化公司行业网站开发运营方案
  • 临海房产中介网站如何制作网站平台管理
  • 做网站多少人建e室内设计网官网平面图
  • git mere 错误后的回滚处理
  • Java开发入门(一)--- JDK与环境变量配置
  • 最好的营销型网站建设公司报电子商务(网站建设与运营)