持续集成 [centos][jenkins][pipeline] jenkins 环境搭建及常用配置

bauul · 2017年12月08日 · 1124 次阅读

maven setup

wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo
yum -y install apache-maven

Download

rpm download

install

rpm -ih '/root/下载/jenkins-2.89.1-1.1.noarch.rpm'

jenkins 工作目录

# 配置文件目录
/etc/sysconfig/jenkins
# 在该配置文件中,可以看到jenkins的主工作目录
JENKINS_HOME="/var/lib/jenkins"

start/stop service

service jenkins start
service jenkins stop

change the default port

vi /etc/sysconfig/jenkins 
#change the line:
JENKINS_PORT="8180"

plugins

  • Build With Parameters
  • Allure Jenkins Plugin
  • Git plugin
  • Pipeline
  • Extended E-mail Notification

全局属性

  • 环境变量
    key: CLASSPATH
    value: .:/usr/java/jdk1.8.0_151/lib/dt.jar:/usr/java/jdk1.8.0_151/lib/tools.jar
    key: JAVA_HOME
    value: /usr/java/jdk1.8.0_151

  • jenkins location
    Jenkins URL: http://192.168.1.100:8180/

  • Extended E-mail Notification
    SMTP server: smtp.exmail.qq.com
    Default user E-mail suffix: @163.com
    Use SMTP Authentication: User Name, Password, SMTP port, Charset
    Default Content Type: HTML
    Default Subject: ${BUILD_STATUS} - ${PROJECT_NAME} - Build # ${BUILD_NUMBER} !
    Default Content:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次构建日志</title>
</head>

<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"
    offset="0">
    <table width="95%" cellpadding="0" cellspacing="0"
        style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
        <tr>
            <td>(本邮件是程序自动下发的,请勿回复!)</td>
        </tr>
        <tr>
            <td><h2>
                    <font color="#0000FF">构建结果 - ${BUILD_STATUS}</font>
                </h2></td>
        </tr>
        <tr>
            <td><br />
            <b><font color="#0B610B">构建信息</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td>
                <ul>
                    <li>项目名称&nbsp;&nbsp;${PROJECT_NAME}</li>
                    <li>构建编号&nbsp;&nbsp;第${BUILD_NUMBER}次构建</li>
                    <li>触发原因:&nbsp;${CAUSE}</li>
                    <li>构建日志:&nbsp;<a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
                    <li>构建&nbsp;&nbsp;Url&nbsp;&nbsp;<a href="${BUILD_URL}">${BUILD_URL}</a></li>
                    <li>工作目录&nbsp;&nbsp;<a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>
                    <li>项目&nbsp;&nbsp;Url&nbsp;&nbsp;<a href="${PROJECT_URL}">${PROJECT_URL}</a></li>
                </ul>
            </td>
        </tr>
        <tr>
            <td><b><font color="#0B610B">Changes Since Last
                        Successful Build:</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td>
                <ul>
                    <li>历史变更记录 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li>
                </ul> ${CHANGES_SINCE_LAST_SUCCESS,reverse=true, format="Changes for Build #%n:<br />%c<br />",showPaths=true,changesFormat="<pre>[%a]<br />%m</pre>",pathFormat="&nbsp;&nbsp;&nbsp;&nbsp;%p"}
            </td>
        </tr>
        <tr>
            <td><b>Failed Test Results</b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td><pre
                    style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">$FAILED_TESTS</pre>
                <br /></td>
        </tr>
        <tr>
            <td><textarea cols="80" rows="30" readonly="readonly"
                    style="font-family: Courier New">${BUILD_LOG, maxLines=100}</textarea>
            </td>
        </tr>
    </table>
</body>
</html>

schedule build

0 5 * * *

即每天的早上 5 点构建一次

issue

gradlew tasks failed
id -gn

vi /etc/sysconfig/jenkins
JENKINS_USER="root"

sudo chown -R root /var/log/jenkins 
sudo chgrp -R root /var/log/jenkins

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

sudo chown -R root /var/cache/jenkins 
sudo chgrp -R root /var/cache/jenkins

global properties

JAVA_HOME=/usr/java/jdk1.8.0_151
CLASSPATH=.:/usr/java/jdk1.8.0_151/lib/dt.jar:/usr/java/jdk1.8.0_151/lib/tools.jar

maven 使用 exec 插件运行 main 的方法:

mvn compile
//without parameters
mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main"

//with parameters
mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.args="arg0 arg1 arg2"

# 或者使用maven-assembly-plugin打出可执行的jar包
java -cp target/api-executor-2.0.jar com.wz.APITestRunAssist localJsonFilePath=$jsonPath jobName=$JOB_NAME sendMessage=true

# 打印GC日志
java -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCDateStamps  -Xloggc:/var/jenkins_home/gc.log   -cp target/api-executor-2.0.jar com.wz.APITestRunAssist localJsonFilePath=$jsonPath jobName=$JOB_NAME sendMessage=true

hosts

# if you need to add one dns proxy on your hosts
vi /etc/hosts
# append one line
192.168.9.66 versiontestapp.com

pipeline

  1. run sh command failed on pipeline shell cd /var/lib/jenkins chmod -R 777 workspace

reference

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册