Jenkins之使用Pyinstaller构建Python应用程序
1. 极简概述
Jenkins是一个独立的开源自动化服务器,可用于自动执行与构建,测试,交付或部署软件相关的各种任务,。
Jenkins可以通过本机系统软件包,Docker安装,甚至可以由安装了Java Runtime Environment(JRE)的任何机器独立运行。
2. Jenkins配置
安装Jenkins很简单,首先安装Java8,然后再安装Jenkins,这里使用Jenkins的2.173版本。包的话,直接去官网下载吧。
2.1 安装JDK
(1) 解压JDK
[root@jenkins ~]# tar zxvf jdk-8u171-linux-x64.tar.gz
[root@jenkins ~]# mv jdk1.8.0_171/ /usr/local/java
(2)添加环境变量
[root@jenkins ~]# vim /etc/profile # 写入如下内容
export JAVA_HOME=/usr/local/java
export PATH=$PATH:$MAVEN_HOME/bin:$JAVA_HOME/bin
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
(3) 立即生效
[root@jenkins ~]# source /etc/profile
[root@jenkins ~]# java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
2.2 安装Jenkins
(1) 直接用jre跑
[root@jenkins ~]# java -jar jenkins.war --httpPort=8080
(2) 访问Web
http://localhost:8080
(3) 跟着配置
注意:如果这一步出现:Problem accessing /setupWizard/createAdminUser. Reason:,那么直接使用admin登陆。
(4) 安装插件Open Blue Ocean
Blue Ocean为开发人员提供了更具乐趣的Jenkins使用方式,它是从基础开始构建的,实现了一种全新的、现代风格的用户界面,有助于任何规模的团队实现持续交付。它以Jenkins插件的方式安装,并集成于Jenkins的流水线中。
注意:版本2.165可能出现安装失败。解决方法未知。
3. 安装Docker
(1)安装依赖
[root@jenkins ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@jenkins ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
[root@jenkins ~]# yum install docker-ce docker-ce-cli containerd.io
[root@jenkins ~]# sudo systemctl start docker
[root@jenkins ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:92695bc579f31df7a63da6922075d0666e565ceccad16b59c3374d2cf4e8e50e
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
4. 使用PyInstaller构建Python应用程序
必须条件:
- GitHub账号
4.1 Fork 一个库
该存储库中包含一个简单的Python应用程序,它是一个命令行工具“add2vals”,它输出两个值的相加。如果至少有一个值是字符串,则“add2vals”将这两个值视为字符串,而是将值连接起来。
链接为:https://github.com/jenkins-docs/simple-python-pyinstaller-app
4.2 克隆项目
# 自己创建GitHub这个目录
[root@jenkins GitHub]# pwd
/root/GitHub
[root@jenkins GitHub]# git clone https://github.com/liuhedong135/simple-python-pyinstaller-app.git
Cloning into 'simple-python-pyinstaller-app'...
remote: Enumerating objects: 36, done.
remote: Total 36 (delta 0), reused 0 (delta 0), pack-reused 36
Unpacking objects: 100% (36/36), done.
[root@jenkins GitHub]# tree simple-python-pyinstaller-app/
simple-python-pyinstaller-app/
├── jenkins
│ └── Jenkinsfile
├── README.md
└── sources
├── add2vals.py
├── calc.py
└── test_calc.py
2 directories, 5 files
4.3 在Jenkins创建管道项目
[root@jenkins GitHub]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
Generating public/private rsa key pair.
Created directory '/root/.ssh'.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Fgo8vZIPd5ITBmPVx0nB5m8Ihc0wxZUTOjeTJZv7qrA root@jenkins
The key's randomart image is:
+---[RSA 2048]----+
| +...o@+++o. |
| o + o.@.o* |
| + + .=o B. |
| = =...o + |
| + B S. o. |
| = = . o. |
| . . . . |
| o . |
| E ... |
+----[SHA256]-----+
[root@jenkins GitHub]# cat ~/.ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEA4RrL+SbBJgMWmZJTvnZoMQ9L1pfQ1qPDDG+rcM/nCIng8ai+
WI6adg894jIP20pYvDLojTHx/zKMcOAOhUuileclui5coViYnFUwLpr+V6agWjqX
C7p09WtpVuz3i9+0+1n91BzK/0QiErdbALtm5UcSh2qcGq0aPQc7Bn/pzDmwC74F
ylrl5yykKqlUfDTUDtZZCtj48g7nkRUq0c/Kk5ETSPJDSBf4kMgOLwM4bAazCCWZ
allApAiGWP9Bc61WCz1Dyr29Jwgd9q3n6g7WxxcCmZOolxdJzwZKsyvKpweeiXOY
wc4mO7jrXprRbf3QIsLO0MvEZmHFvydYnOc6xQIDAQABAoIBAAcJsHDkvksy84Tv
p0CgMRe/k65s5rGfQxzjbOfFCEMjFG5ZyUG5i6i/Rz8IM6i2hVm0+cJYWmmRKgTp
bGrMd5PaOzq78ld1DqhsnWoHnIYfBoprpIdQWBHIPu3h0MeFwgHNyXrVP5+FgM82
/lhig5ANjc3ADoti+BwKWtUUMLoa0JxfMb7LDM1TxLVDp3iReMeQQFbM7OhAg27k
18RCLNDKYcenxFKhaU0N/fUNbpAwhowqj792H2AfLtsTX1wpncwJlYgzBVSKMsQc
bzbuRidLTnhN4JU24QggJDzM8eTLlVzAf3PwbkjPavKe7EmNAUYpmwjPUwMytgBq
8Lq42CECgYEA8cm9F951o6SpvAwodXCDbWX8EDo1pzF89aISEMus4rQLa97cnGv+
bqF5Rp8Rh7L0OEDXFFAXiDYlUtUpKqFopG+dxPxEyRq1XcGTFjHWguBCwUUM6YkU
ZZdyWSF53nuSObzZhnxIVk7RqlX8eqKWkNcqXvrr5PEdbonO9xfU6M0CgYEA7lYE
qbTSsYiH5l2tYfQ2Ku8wgSfv8YbrsMoe4Wn4StRAvcYYpy9XpF0XrhG1BJqzbxKh
nXZ6VpJMxwkeH+ADuwWGD8NIqSO+6OYeRt3j+7+qbhtixyoQmqChREk1JYyZ+y8S
PLI78gDuR+aGvK+BYaHxfmbCSNdg7T55GLj5edkCgYA8KXlungDhhhBh5RiQGD3r
k6vDkehVwL+aEwixuyw6gJgy8BIurcRyXKVjiCMBg+q2sJOTz1X7o5Vn5fhHTkBO
YiN8p1SqQYYNXSQvKpl4mTqjAn5zAZPcr3FZcjXzwQWibjYl+3i4WQZcfubC0P/r
7E3zsjXOEpzDK3EfFIP6pQKBgDVRLufcGMo4ddZ2HGRuYnPGYH50Nbw9Z5p8DN52
e2IaC4WovDkiYSPW44ZAVdN63Cs76sW0DHgzcFOSWZfd4FfFS5FZKXcVFQ8684j8
+JSH8xOAst9FLMO6rXj+O5Jz2rqTdCv532krBiM1kezCAlqaBNE3Ib1z2S4ZrLN4
Je85AoGAU5L+ZEjGB/9ayk47j5UOaquFfvRd+0v0sgdRnU0QxPSYy/vuRvTiaADS
Cp4//7+z+fixHgbtZQYSYdGK4c6YSj6bcVhlvOfSG1oe/O/J42PzDVljl6DCxMT9
mJc31CK32iFpQmXXPV8lyWcPuVr6qHmg1gvd4eNqp0N9L3Z9x6Y=
-----END RSA PRIVATE KEY-----
4.4 为初始管道创建Jenkinsfile
[root@jenkins simple-python-pyinstaller-app]# pwd
/root/GitHub/simple-python-pyinstaller-app
[root@jenkins simple-python-pyinstaller-app]# vim Jenkinsfile
# 写入如下内容
pipeline {
agent none
stages {
stage('Build') {
agent {
docker {
image 'python:2-alpine'
}
}
steps {
sh 'python -m py_compile sources/add2vals.py sources/calc.py'
}
}
}
}
[root@jenkins simple-python-pyinstaller-app]# git config --global user.email "liuhedong135@163/com"
[root@jenkins simple-python-pyinstaller-app]# git config --global user.name "liuhedong"
[root@jenkins simple-python-pyinstaller-app]# git add .
[root@jenkins simple-python-pyinstaller-app]# git commit -m "Add initial Jenkinsfile"
[master 6966a7d] Add initial Jenkinsfile
1 file changed, 15 insertions(+)
create mode 100644 Jenkinsfile
4.5 测试阶段
[root@jenkins simple-python-pyinstaller-app]# pwd
/root/GitHub/simple-python-pyinstaller-app
[root@jenkins simple-python-pyinstaller-app]# vim Jenkinsfile
# 添加如下内容
stage('Test') {
agent {
docker {
image 'qnib/pytest'
}
}
steps {
sh 'py.test --verbose --junit-xml test-reports/results.xml sources/test_calc.py'
}
post {
always {
junit 'test-reports/results.xml'
}
}
}
[root@jenkins simple-python-pyinstaller-app]# cat Jenkinsfile
pipeline {
agent none
stages {
stage('Build') {
agent {
docker {
image 'python:2-alpine'
}
}
steps {
sh 'python -m py_compile sources/add2vals.py sources/calc.py'
}
}
stage('Test') {
agent {
docker {
image 'qnib/pytest'
}
}
steps {
sh 'py.test --verbose --junit-xml test-reports/results.xml sources/test_calc.py'
}
post {
always {
junit 'test-reports/results.xml'
}
}
}
}
}
[root@jenkins simple-python-pyinstaller-app]# git stage .
[root@jenkins simple-python-pyinstaller-app]# git commit -m "Add Test stage"
[master 342b643] Add Test stage
1 file changed, 16 insertions(+)
重新进入Blue Ocean
4.6 交付阶段
[root@jenkins simple-python-pyinstaller-app]# pwd
/root/GitHub/simple-python-pyinstaller-app
[root@jenkins simple-python-pyinstaller-app]# vim Jenkinsfile
# 写入如下内容
stage('Deliver') {
agent {
docker {
image 'cdrx/pyinstaller-linux:python2'
}
}
steps {
sh 'pyinstaller --onefile sources/add2vals.py'
}
post {
success {
archiveArtifacts 'dist/add2vals'
}
}
}
# 最终为如下内容
[root@jenkins simple-python-pyinstaller-app]# cat Jenkinsfile
pipeline {
agent none
stages {
stage('Build') {
agent {
docker {
image 'python:2-alpine'
}
}
steps {
sh 'python -m py_compile sources/add2vals.py sources/calc.py'
}
}
stage('Test') {
agent {
docker {
image 'qnib/pytest'
}
}
steps {
sh 'py.test --verbose --junit-xml test-reports/results.xml sources/test_calc.py'
}
post {
always {
junit 'test-reports/results.xml'
}
}
}
stage('Deliver') {
agent {
docker {
image 'cdrx/pyinstaller-linux:python2'
}
}
steps {
sh 'pyinstaller --onefile sources/add2vals.py'
}
post {
success {
archiveArtifacts 'dist/add2vals'
}
}
}
}
}
[root@jenkins simple-python-pyinstaller-app]# git stage .
[root@jenkins simple-python-pyinstaller-app]# git commit -m "Add Deliver stage"
[master 19c0c48] Add Deliver stage
1 file changed, 15 insertions(+)
重新进入Blue Ocean
最后点击下载,并传入到linux,
[root@jenkins ~]# ls -lh
total 3.4M
-rw-r--r-- 1 root root 3.4M Apr 21 10:54 add2vals
drwxr-xr-x 3 root root 43 Apr 22 09:32 GitHub
[root@jenkins ~]# chmod +x add2vals
[root@jenkins ~]# ./add2vals
You entered 0 value/s.
Usage: 'add2vals X Y' where X and Y are individual values.
If add2vals is not in your path, usage is './add2vals X Y'.
If unbundled, usage is 'python add2vals.py X Y'.
[root@jenkins ~]# ./add2vals 1 1
The result is 2
最后我们就成功的将应用打包,并在linux上运行,这个应用是一个简单的计算。
Jenkins之使用Pyinstaller构建Python应用程序的更多相关文章
- 使用pyinstaller打包python小程序(没有使用第三方模块)
准备: 1,xxx.py程序文件 2,自定义的图标文件:xxx.ico 图标文件应该包含常见的多分辨率格式,以便适应在不同场合显示,不能是单一图片. 你可以用专用的软件处理生成图标,不过少量的图标生产 ...
- 【Jenkins】jenkins构建python项目提示:'python' 不是内部或外部命令,也不是可运行的程序或批处理文件
一.问题:jenkins构建python项目提示:'python' 不是内部或外部命令,也不是可运行的程序或批处理文件 二.原因:要在jenkins配置本地环境变量 三.解决方案:添加python.e ...
- PyInstaller 打包 python程序成exe
pychaim下PyInstaller 打包 python程序 主题是使用PyInstaller 打包python时遇到一些问题以及解决方案,其中将要打包的程序是用tensorflow做的LSTM算法 ...
- “failed to excute script xxx” PyInstaller 打包python程序为exe文件过程错误
在使用PyInstaller打包python程序,打包命令为: pyinstaller -F -w -i manage.ico yourpyfile.py 顺便说一下几个参数的作用 -F:是直接生成单 ...
- Jenkins自动化构建python nose测试
[本文出自天外归云的博客园] 简介 通过Jenkins自动化构建python nose测试分两步: 1. 创建节点(节点就是执行自动化测试的机器): 2. 创建任务并绑定节点(用指定的机器来跑我们创建 ...
- (转)使用 PyInstaller 把python程序 .py转为 .exe 可执行程序
最近使用Python为项目开发一款绘图工具(绘出 声场三维模型).因为希望能把Python脚本发布为脱离Python平台运行的可执行程序,比如单个的exe文件.PyInstaller恰满足这个需求.本 ...
- Python flask构建微信小程序订餐系统
第1章 <Python Flask构建微信小程序订餐系统>课程简介 本章内容会带领大家通览整体架构,功能模块,及学习建议.让大家在一个清晰的开发思路下,进行后续的学习.同时领着大家登陆ht ...
- Python flask构建微信小程序订餐系统☝☝☝
Python flask构建微信小程序订餐系统☝☝☝ 一.Flask MVC框架结构 1.1实际项目结构 1.2application.py 项目配置文件 Flask之flask-script模块使 ...
- 【Python开发】PyInstaller打包Python程序
PyInstaller是一个能将Python程序转换成单个可执行文件的程序, 操作系统支持Windows, Linux, Mac OS X, Solaris和AIX.并且很多包都支持开箱即用,不依赖环 ...
随机推荐
- springboot + @KafkaListener 手动提交及消费能力优化
转载 https://blog.csdn.net/asd5629626/article/details/82776450 https://blog.csdn.net/asd5629626/artic ...
- Django中操作Redis
一 创建redis连接池 redis_pool.py pool = redis.ConnectionPool(host='10.211.55.4', port=6379) 二 引入连接池 import ...
- javaweb开发.页面中文乱码问题
1.设置eclips , window->Preferences->web->JSP Files中的Encoding选项为UTF-8 2.修改jsp文件头部为UTF-8 <%@ ...
- Pandas处理丢失数据
1.创建含NaN的矩阵 >>> dates = pd.date_range(', periods=6) >>> df = pd.DataFrame(np.arang ...
- iOS高德地图SDK定位和搜索附近信息的具体使用
1.显示地图.定位.显示当前位置. 导入你需要的功能的头文件,申明全局变量,代理方法等等. 初始化地图,在控制器即将显示额时候打开定位和跟踪用户,这里对参数不懂的话康忙进去都有注释. 对了.i ...
- dpkg: error: dpkg status database is locked by another process
First run: lsof /var/lib/dpkg/lock Then make sure that process isn't running: ps cax | grep PID If i ...
- Spring Boot学习笔记:整合H2数据库
H2数据库:java语言编写的嵌入式sql数据库.可以和应用一起打包发布. H2有三种连接模式(Connection Modes): Embedded mode (local connections ...
- JQUERY-定义-查找
正课: 1. 什么是jQuery 2. 如何使用jQuery 3. 查找 1. 什么是jQuery 第三方开发的 执行DOM操作的 极简化的 函数库 第三方: 下载 执行DOM操作: 学习jQuery ...
- SAS DATA步读取数据
上面一节讲了SAS的基本概念,以及语法结构,这次主要讲解SAS DATA步读取数据. 1 ·列表输入 2 ·按列输入 3 ·格式化输入 使用DATA步读取数据的基本形式如下: DA ...
- FJOI2018 部分题解
领导集团问题 考虑对每一个点暴力dpdpdp:fi,jf_{i,j}fi,j表示iii为根的子树选出来的点集最小值不小于jjj的点集元素个数最大值. 那么显然fi,j=∑max{fv,k≥j}+1 ...