栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Jenkins

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Jenkins

Jenkins的学习内容:

1. Linux环境下Jenkins的搭建

2. Jenkins拉取代码的环境配置--git插件

3. Jenkins与pytest-- Jenkins运行pytest测试用例,报告生成,覆盖率等

4. Jenkins与github-webhook 和github api

5. Jenkins build with parameter-- Jenkins变量的使用

6.Jenkins的其他使用方式---查看其他项目的配置进行学习

7. Jenkins pipeline job配置

一.Linux环境下Jenkins的搭建 具体环境安装可参考jenkins官网:LinuxJenkins – an open source automation server which enables developers around the world to reliably build, test, and deploy their softwarehttps://www.jenkins.io/doc/book/installing/linux/#debianubuntu 1.安装java

目前jenkins要求的版本是java8或者java11,其他版本会出问题

Java requirements

ubuntu可安装open jdk

使用ubuntu的apt命令来安装open jdk

sudo apt update
sudo apt search openjdk
sudo apt install openjdk-8-jdk

安装完成后,java的安装路径为:/usr/lib/jvm

安装完成后,即可使用java -version命令,不需要配置环境变量

2. 安装jenkins

安装完成java后,开始安装jenkins,参考jenkins官网的ubuntu的安装方法,注意:ubuntu中使用apt-get命令,安装一直失败,可以改用apt命令

jenkins官网是apt-get命令,需要换成apt命令才能安装成功

curl -fsSL https://pkg.jenkins.io/debian/jenkins.io.key | sudo tee 
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] 
  https://pkg.jenkins.io/debian binary/ | sudo tee 
  /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt update
sudo apt install jenkins

以上步骤,jenkins安装完成

jenkins安装完成后,默认的linux用户名是jenkins,就是jenkins使用“jenkins”这个用户名在linux机器上执行shell命令

可以改为使用root用户

中间由于使用apt-get的问题,导致jenkins安装和启动失败,改为apt安装解决问题

3。Jenkins的一些基本配置

(1)配置Jenkins的linux用户名

在/etc/default/jenkins文件中,可以配置user和group以及port

然后让root用户有jenkins文件的权限

sudo chown -R root:root /var/lib/jenkins

(2)

配置防火墙

Jenkins的默认端口是8080,需要在Linux上配置下防火墙,允许访问8080端口,才能其他电脑上使用http访问jenkins

4.启动jenkins

使用命令启动和重启jenkins

systemctl start jenkins

可在jenkin官网上查看命令

二.拉取git代码 1.安装git

需要在Linux服务器上安装git

sudo apt install git

2.配置jenkins全局工具配置

Jenkins--》Global Tool Configuration

配置git的执行目录

在Linux中使用whereis git即可

 在jenkins中安装git插件: jenkins-》Manager Jennkins-》Manage Plugins

3.在job中配置git

新建一个freestyple,在source code management中配置

凭证是github或者gitlab的用户名和密码,如果是公司的账号,可以使用accesstoken

三。jenkins与pytest

如果需要使用pytest与jenkins结合,首先需要安装python和pytest

ubuntu已经安装了python3,可直接安装pytest

pip3 install pytest

pip3 install pytest-cov

1.配置jenkins

在Build里配置如下shell

cd /var/lib/jenkins/workspace
pytest --cov --cov-report=html

build job时,出现以下问题:

(1)出现如下问题:

ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --cov --cov-report=html
  inifile: None
  rootdir: /var/lib/jenkins/workspace/demo1/test

Linux服务器上已经安装了pytest和pytest-cov,但是还是不行

Linux使用的用户是ubuntu

1.首先在Linux服务器上使用pytest --cov --cov-report=html命令,发现出现error:sqlite3,can not open database file,出现这个错误,应该是权限问题,先试试看单独的pytest命令是否能运行

2.使用单独的pytest命令,运行成功了,但是出现warnning:can not create pytest-cache on workspace,猜测,pytest --cov的命令应该也是这个文件夹权限引起的,查看workspace的权限,发现没有create权限,使用chmod 777 /var/lib/workspace,然后再次运行pytest,没有warnning

3.再次运行pytest --cov --cov-report=html,运行成功,在workspace中出现htmlcov

4. 在Linux上运行pytest --cov --cov-report=html命令成功后,再次使用jenkins运行,还是失败,仍然是这个错误:

ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...] pytest: error: unrecognized arguments: --cov --cov-report=html inifile: None rootdir: /var/lib/jenkins/workspace/demo1/test

尝试使用单独的pytest命令来运行,出现这个warning

=============================== warnings summary ===============================
../../../../usr/local/lib/python3.8/dist-packages/_pytest/cacheprovider.py:428
  /usr/local/lib/python3.8/dist-packages/_pytest/cacheprovider.py:428: PytestCacheWarning: cache could not write path /var/lib/jenkins/workspace/.pytest_cache/v/cache/nodeids
    config.cache.set("cache/nodeids", sorted(self.cached_nodeids))

../../../../usr/local/lib/python3.8/dist-packages/_pytest/stepwise.py:49
  /usr/local/lib/python3.8/dist-packages/_pytest/stepwise.py:49: PytestCacheWarning: cache could not write path /var/lib/jenkins/workspace/.pytest_cache/v/cache/stepwise
    session.config.cache.set(STEPWISE_CACHE_DIR, [])

-- Docs: How to capture warnings — pytest documentation

这个workspace的权限已经更改,使用jenkins运行,为什么还出现权限问题?应该是Linux上运行的用户是ubuntu,但是jenkins运行的用户名不是ubuntu,查看/etc/default/jenkins文件,发现jenkins_user是jenkins,应该是以jenkins来运行的,然后修改配置为root,并且重启jenkins,再次build,可以成功,如果将jenkins_user改为ubuntu,不知道为啥重启jenkins失败

5.再将pytest命令改为pytest --cov --cov-report=html,重新build,还是出现error

ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --cov --cov-report=html
  inifile: None
  rootdir: /var/lib/jenkins/workspace

随猜测,pytest-cov这个插件,root用户可能访问不了,将shell脚本改为:

pip3 install pytest-cov
cd /var/lib/jenkins/workspace
pytest --cov --cov-report=html

重新安装pytest-cov,build成功

还不明白,如何配置Linux,可以不用pip3 install pytest-cov这个命令

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/729225.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号