2016-11-18 Frank
jenkins 持续集成工具。本文在开发环境测试,应用应该部署在测试和生产环境。
java -jar jenkins.war --httpPort=8000
Centos安装sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum install jenkins
启动sudo systemctl start jenkins.service
解决:sudo ln -s /usr/local/java/jdk1.7.0_80/bin/java /usr/bin/java
修改端口vim /etc/sysconfig/jenkins
JENKINS_PORT="8085"
日志查看tail -f /var/log/jenkins/jenkins.log
查看初始密码more /var/lib/jenkins/secrets/initialAdminPassword
How to Install Jenkins on CentOS 7保存。
立即构建,等待。。
安装Git Parameter Plugin
为了避免浪费时间,一定要下载安装包进行安装。
插件安装成功后,在项目配置中后看到多了一个“参数化构建过程”
勾选“参数化构建过程”,然后添加Git Parameter,配置如下
name: BRANCH
Default Value: origin/testing
Poll SCM:定时检查源码变更(根据SCM软件的版本号),如果有更新就checkout最新code下来,然后执行构建动作。
Build periodically:周期进行项目构建(它不care源码是否发生变化)。
1 . 在用户的【设置】页面的【API Token】部分可以获取用户的令牌
2 . 在项目配置页面的【构建触发器】部分勾选【触发远程构建】,在身份验证令牌中输入可构建者的令牌
3 . 通过发送触发url到jenkins服务器即可远程触发该项目的构建,如:http://127.0.0.1:8180/jenkins/job/testCI/build?token=967ae5423f43915463d7436bb13d7e54
curl —user admin:1234 -s http://localhost:8080/jenkins/job/liuwan/build?token=c1e9e2489611a6987142ccad72b53be7
#!/use/bin/python
'''
http://pythonhosted.org/jenkinsapi/
https://github.com/pycontribs/jenkinsapi/tree/master/examples
http://python-jenkins.readthedocs.io/en/latest/
curl --user admin:1234 -s http://localhost:8080/jenkins/job/liuwan/buildWithParameters?token=c1e9e2489611a6987142ccad72b53be7
'''
from jenkinsapi.jenkins import *
from jenkinsapi.build import *
from jenkinsapi.job import *
jenkins_url = 'http://localhost:8080/jenkins'
jenkins_user = 'admin'
jenkins_password = '1234'
job_name = 'liuwan'
jenkins = Jenkins(jenkins_url,jenkins_user,jenkins_password)
job_url = jenkins_url+'/job/'+job_name
print job_url
my_job = jenkins.get_job(job_name)
previous_build_number = my_job.get_last_buildnumber()
current_build_number = previous_build_number+1
print 'Previous build number is {0}'.format(previous_build_number)
print 'Current build number is {0}'.format(previous_build_number)
My_Build_Drop_Location = r"origin/testing"
Buid_Parameters = {"Build_Drop_Location":My_Build_Drop_Location}
# my_job.invoke("None",False,False,3,15,Buid_Parameters)
# my_job.invoke("c1e9e2489611a6987142ccad72b53be7",False,Buid_Parameters,None,None,5)
my_job.invoke(None,False,Buid_Parameters,None,None,5)
# my_job.enable()
# my_job.disable()
last_build = my_job.get_last_build()
print last_build
print my_job.get_last_buildnumber()
print 'Build Status is {0}'.format(last_build.get_status())
执行远程构建
python build.py
报错:
No valid crumb was included in the request
解决:
安全配置里面(/jenkins/configureSecurity/)反选 防止跨站点请求伪造
系统管理->系统设置->邮件通知 里设置smtp地址,在高级里设置端口、用户名、密码,然后选择通过发送测试邮件测试配置。如果失败请确保上面的“系统管理员邮件地址”填写用户名一样的地址再重新测试。
su jenkins -s /bin/bash
ssh-keygen -t rsa
sed -i 's#<property name=\"username\" value=\".*\"/>#<property name=\"username\" value=\"user\"/>#g' src/main/resources/applicationContex.xml
sed -i 's#<property name=\"password\" value=\".*\"/>#<property name=\"password\" value=\"pass\"/>#g' src/main/resources/applicationContext.xml
chown -R jenkins:jenkins /workspace/project
cp -rf build/classes/main/ /workspace/project/src/main/webapp/WEB-INF/classes/
$visudo
jenkins ALL=NOPASSWD: /opt/apache-tomcat-8.5.15/bin/restart.sh
$ vim restart.sh
#!/bin/sh
bin=$(cd `dirname $0`; pwd)
pid=$(ps aux | grep tomcat | grep -v grep | grep -v restart | grep ${bin} | awk '{print $2}')
if [ -n "${pid}" ]; then
echo "Shutdown..."
sh ${bin}/shutdown.sh
sleep 3
pid=$(ps aux | grep tomcat | grep -v grep | grep -v restart | grep ${bin} | awk '{print $2}')
if [ -n "${pid}" ]; then
kill -9 ${pid}
sleep 1
fi
fi
echo "Startup..."
sh ${bin}/startup.sh
if [ "$1" = "-v" ]; then
tail -f ${bin}/../logs/catalina.out
fi
hook方式没成功,暂时用poll scm
#!/bin/bash
SOURCE_DIR=/var/lib/jenkins/workspace/${JOB_NAME}
DEST_DIR=/opt/www/xxx
REMOTE_IP=192.168.60.61
/usr/bin/rsync -e "ssh -p2222" -avpgolr --delete-before $SOURCE_DIR $REMOTE_IP:$DEST_DIR
同样修改jenkins的配置文件,修改JAVA_OPTIONS参数
vim /etc/sysconfig/jenkins
找到JENKINS_JAVA_OPTIONS=”-Djava.awt.headless=true”
修改成
JENKINS_JAVA_OPTIONS=”-Djava.awt.headless=true -Xms256m -Xmx512m -XX:MaxNewSize=256m -XX:MaxPermSize=256m”
参考:
jenkins构建时支持git选择分支
jenkins的构建触发中Poll SCM和Build periodically的区别
在jenkins中配置令牌远程触发项目构建
How to configure Git post commit hook
Jenkinsapi 远程启动jenkins参数化构建(parameterized build)
发表评论 登录: