使用Jenkins部署Python项目
使用Jenkins部署Python项目
廖大使用Fabric部署的。我使用Jenkins
试试部署过程。
参考Jenkins+Python部署完整版,不过安装方式不同。
-
安装tomcat
yum install tomcat
-
安装jenkins
上面rpm源失效了,找一个
http://pkg.jenkins-ci.org/redhat/
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
yum install jenkins
- ++启动jenkins++
/etc/init.d/jenkins start
启动后访问,IP:8080无反应,用netstat -tnlp | grep 8080
可看到java服务是成功启动的。设置安全组端口后,可以访问。
- 初始化Jenkins
在制定位置复制密码填入页面后继续,选择了git
和github
插件。
安装完成,在新建任务之前,需要把项目放到git上去。
- 发布git
提交log空文件夹,忽略下面所有文件:Git 忽略文件夹下的文件,但是保留空文件夹
# ignore all except .gitignore file
*
!.gitignore
继续上路…
-
配置Jenkins
参考文章中的下载插件和更新跳过。直接配置,填写私钥等。
保存后构建:
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
... ...
ERROR: Error cloning remote repo 'origin'
是因为配置了GIT,在最前面还勾选并填了github项目地址,去掉构建Ok.
- 构建
等等,好像哪里不对。构建的位置去哪了,我的服务器目录就没填啊。构建地址呢?
Started by user xxxx
Building in workspace /var/lib/jenkins/workspace/python博客
> git rev-parse --is-inside-work-tree # timeout=10
使用的默认目录。
搜索了一下Jenkins 构建 目标地址
之类的竟然没搜到想要的结果。!!
Bulid下添加执行Shell,移动文件
\cp -rf $WORKSPACE/$JOB_NAME/* /xxx/web/
\(WORKSPACE/\)JOB_NAME 是 工作目录和项目名
+ cp -rf '/var/lib/jenkins/workspace/pythonBlog/pythonBlog/*' /xxx/web/
cp: cannot stat ‘/var/lib/jenkins/workspace/pythonBlog/pythonBlog/*’: No such file or directory
Build step 'Execute shell' marked build as failure
$WORKSPACE
就是目录没错了。
之后一直报权限错误 Permission denied, 执行脚本无权限。
cp: cannot remove ‘/xxx/web/log/.gitignore’: Permission denied
cp: cannot create regular file ‘/xxx/web/README.md’: Permission denied
cp: cannot remove ‘/xxx/web/www/pymonitor.py’: Permission denied
cp: cannot remove ‘/xxx/web/www/db/table.sql’: Permission denied
cp: cannot remove ‘/xxx/web/www/webFrame.py’: Permission denied
......
网上有一些解决办法jenkins 权限问题、以root用户运行jenkins中shell命令 都是修改jenkins为root用户组或者直接用root去执行了。因为我只一个项目,只修改了文件夹权限为jenkinschown -R jenkins:root /xxx/web
即可。重启服务service jenkins restart
后再次构建,成功!
最后在配置加计划任务SCM H/5 * * * *
五分钟一次,下次修改了测试。