jenkins学习5-jenkins拉取git仓库代码,执行python自动化脚本

前言

python自动化的脚本开发完成后需提交到git代码仓库,接下来就是用Jenkins拉取代码去构建自动化代码了

git源码管理

代码上传git仓库这里就不介绍了,可以看之前写过的github相关这篇https://www.cnblogs.com/yoyoketang/p/7302515.html
自己公司内部的一般用gitlab,可以参考这篇https://www.cnblogs.com/yoyoketang/p/10282529.html

打开Jenkins新建一个自由风格的项目

源码管理

  • Repository URL 代码仓库地址
  • Credentials git仓库登陆的账号和密码凭证
  • 指定分支(为空时代表any)分支默认*/master

Repository URL 代码仓库地址

备注:如果是ssh方式链接的,就点右上角的SSH地址

Credentials 点开Jenkins按钮,输入git仓库登陆的账号和密码

构建 执行shell

执行shell,先pip3安装requirements.txt,再用pytest执行脚本

查看控制台输入出,console查看日志

+ ls
requirements.txt
test_demo.py
+ pip3 install -r requirements.txt
Collecting requests==2.18.4 (from -r requirements.txt (line 1))
  Downloading 

Installing collected packages: idna, urllib3, requests, atomicwrites, six, more-itertools, wcwidth, attrs, py, zipp, importlib-metadata, pluggy, pytest, pytest-metadata, pytest-html
  Found existing installation: idna 2.8
    Uninstalling idna-2.8:
Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/var/jenkins_home/python3/lib/python3.6/site-packages/idna-2.8.dist-info/INSTALLER'
Consider using the `--user` option or check the permissions.

发现没有权限安装,可以进入容器内部安装

docker exec -it -u root 容器id /bin/bash

打开workspace目录安装 /home/jenkins/workspace/pytest_demo

[root@cb8e397d5308]# cd /home/jenkins/workspace/pytest_demo
[root@cb8e397d5308]# ls
requirements.txt  test_demo.py
[root@cb8e397d5308]# pip3 install -r requirements.txt

安装完成后输入pytest检查pytest:-bash: pytest: command not found

[root@VM_0_2_centos pytest_demo]# pytest
-bash: pytest: command not found

查找pytest安装地址添加软链接,输入pytest --version查看环境

[root@cb8e397d5308]# find / -name pytest
/var/jenkins_home/python3/bin/pytest
[root@cb8e397d5308]# ln -s /var/jenkins_home/python3/bin/pytest /usr/bin/pytest
[root@cb8e397d5308]# pytest --version
This is pytest version 4.5.0, imported from /root/python36/lib/python3.6/site-packages/pytest.py
setuptools registered plugins:
  pytest-html-1.19.0 at /root/python36/lib/python3.6/site-packages/pytest_html/plugin.py
  pytest-metadata-1.8.0 at /root/python36/lib/python3.6/site-packages/pytest_metadata/plugin.py

构建job

上面需要的环境都安装完成后,执行shell的时候,直接输入pytest命令就可以执行自动化的脚本了

构建成功

posted @   上海-悠悠  阅读(5975)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示