求解答:jenkins执行 pytest报错,但在服务器上执行成功

问题

jenkins job执行shell时到pytest命令时报错,但是登陆到jenkins所在的服务器上可以执行成功。

服务器centos8 ,未使用docker。
有使用虚拟环境。

报错信息

Started by user Liu
Running as SYSTEM
Building on the built-in node in workspace /var/lib/jenkins/workspace/test001
The recommended git tool is: NONE
using credential 6bad3449-6d7d-4f3f-8e0c-4c12d15aad0e

git rev-parse --resolve-git-dir /var/lib/jenkins/workspace/test001/.git # timeout=10
Fetching changes from the remote Git repository
git config remote.origin.url https://gitee.com/Running1011/pytest-39.git # timeout=10
Cleaning workspace
git rev-parse --verify HEAD # timeout=10
Resetting working tree
git reset --hard # timeout=10
git clean -ffdx # timeout=10
Fetching upstream changes from https://gitee.com/Running1011/pytest-39.git
git --version # timeout=10
git --version # ‘git version 2.27.0’
using GIT_ASKPASS to set credentials
git fetch --tags --force --progress – https://gitee.com/Running1011/pytest-39.git +refs/heads/:refs/remotes/origin/ # timeout=10
git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision 19bca58b73313b70a5ff2dca9a75df362f65c054 (refs/remotes/origin/master)
git config core.sparsecheckout # timeout=10
git checkout -f 19bca58b73313b70a5ff2dca9a75df362f65c054 # timeout=10
Commit message: “测试jenkins”
git rev-list --no-walk 19bca58b73313b70a5ff2dca9a75df362f65c054 # timeout=10
[test001] $ /bin/bash /tmp/jenkins18043907083916733487.sh
Requirement already satisfied: pytest in ./myenv/lib/python3.9/site-packages (from -r requirements.txt (line 1)) (8.3.2)
Requirement already satisfied: pytest-ordering in ./myenv/lib/python3.9/site-packages (from -r requirements.txt (line 2)) (0.6)
Requirement already satisfied: pytest-xdist in ./myenv/lib/python3.9/site-packages (from -r requirements.txt (line 3)) (3.6.1)
Requirement already satisfied: pytest-rerunfailures in ./myenv/lib/python3.9/site-packages (from -r requirements.txt (line 4)) (14.0)
Requirement already satisfied: pyyaml in ./myenv/lib/python3.9/site-packages (from -r requirements.txt (line 5)) (6.0.1)
Requirement already satisfied: allure-pytest in ./myenv/lib/python3.9/site-packages (from -r requirements.txt (line 6)) (2.13.5)
Requirement already satisfied: iniconfig in ./myenv/lib/python3.9/site-packages (from pytest->-r requirements.txt (line 1)) (2.0.0)
Requirement already satisfied: packaging in ./myenv/lib/python3.9/site-packages (from pytest->-r requirements.txt (line 1)) (24.1)
Requirement already satisfied: pluggy<2,>=1.5 in ./myenv/lib/python3.9/site-packages (from pytest->-r requirements.txt (line 1)) (1.5.0)
Requirement already satisfied: exceptiongroup>=1.0.0rc8 in ./myenv/lib/python3.9/site-packages (from pytest->-r requirements.txt (line 1)) (1.2.2)
Requirement already satisfied: tomli>=1 in ./myenv/lib/python3.9/site-packages (from pytest->-r requirements.txt (line 1)) (2.0.1)
Requirement already satisfied: execnet>=2.1 in ./myenv/lib/python3.9/site-packages (from pytest-xdist->-r requirements.txt (line 3)) (2.1.1)
Requirement already satisfied: allure-python-commons==2.13.5 in ./myenv/lib/python3.9/site-packages (from allure-pytest->-r requirements.txt (line 6)) (2.13.5)
Requirement already satisfied: attrs>=16.0.0 in ./myenv/lib/python3.9/site-packages (from allure-python-commons==2.13.5->allure-pytest->-r requirements.txt (line 6)) (23.2.0)
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR> File “/var/lib/jenkins/jobs/test001/pytest-39/myenv/lib/python3.9/site-packages/_pytest/main.py”, line 279, in wrap_session
INTERNALERROR> config._do_configure()
INTERNALERROR> File “/var/lib/jenkins/jobs/test001/pytest-39/myenv/lib/python3.9/site-packages/_pytest/config/init.py”, line 1118, in _do_configure
INTERNALERROR> self.hook.pytest_configure.call_historic(kwargs=dict(config=self))
INTERNALERROR> File “/var/lib/jenkins/jobs/test001/pytest-39/myenv/lib/python3.9/site-packages/pluggy/_hooks.py”, line 535, in call_historic
INTERNALERROR> res = self._hookexec(self.name, self._hookimpls.copy(), kwargs, False)
INTERNALERROR> File “/var/lib/jenkins/jobs/test001/pytest-39/myenv/lib/python3.9/site-packages/pluggy/_manager.py”, line 120, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR> File “/var/lib/jenkins/jobs/test001/pytest-39/myenv/lib/python3.9/site-packages/pluggy/_callers.py”, line 139, in _multicall
INTERNALERROR> raise exception.with_traceback(exception.traceback)
INTERNALERROR> File “/var/lib/jenkins/jobs/test001/pytest-39/myenv/lib/python3.9/site-packages/pluggy/_callers.py”, line 103, in _multicall
INTERNALERROR> res = hook_impl.function(*args)
INTERNALERROR> File “/var/lib/jenkins/jobs/test001/pytest-39/myenv/lib/python3.9/site-packages/allure_pytest/plugin.py”, line 167, in pytest_configure
INTERNALERROR> file_logger = AllureFileLogger(report_dir, clean)
INTERNALERROR> File “/var/lib/jenkins/jobs/test001/pytest-39/myenv/lib/python3.9/site-packages/allure_commons/logger.py”, line 18, in init
INTERNALERROR> shutil.rmtree(self._report_dir)
INTERNALERROR> File “/usr/local/lib/python3.9/shutil.py”, line 715, in rmtree
INTERNALERROR> _rmtree_safe_fd(fd, path, onerror)
INTERNALERROR> File “/usr/local/lib/python3.9/shutil.py”, line 672, in _rmtree_safe_fd
INTERNALERROR> onerror(os.unlink, fullname, sys.exc_info())
INTERNALERROR> File “/usr/local/lib/python3.9/shutil.py”, line 670, in _rmtree_safe_fd
INTERNALERROR> os.unlink(entry.name, dir_fd=topfd)
INTERNALERROR> PermissionError: [Errno 13] Permission denied: ‘429271d4-cbf0-465d-9206-43b4ba543dfd-attachment.txt’
Build step ‘Execute shell’ marked build as failure
[test001] $ /usr/local/test/allure-2.30.0/bin/allure generate -c -o /var/lib/jenkins/workspace/test001/pytest-39/report
allure-results does not exist
Report successfully generated to /var/lib/jenkins/workspace/test001/pytest-39/report
Allure report was successfully generated.
Creating artifact for the build.
Artifact was added to the build.
Finished: FAILURE

环境

cetnos8,python39

求解答

确保 Jenkins 用户对工作空间目录及其所有文件具有正确的读写权限。可以使用以下命令修改权限:

sudo chown -R jenkins:jenkins /var/lib/jenkins/workspace/test001
sudo chmod -R 755 /var/lib/jenkins/workspace/test001
2 个赞

感谢,解决了。