以前用的jenkins自由风格发布代码。界面丑陋,出现问题位置不够清晰。今天改进一下流程使用jenkins pipeline构建项目。

学习使我快乐

步骤一、安装pipeline插件

点击系统管理->插件管理->可选插件,输入pipeline,选择如下插件安装

步骤二、Jenkins设置邮箱相关参数
Manage Jenkins->Configure System

步骤三、创建流水线项目

新建任务->选择流水线

描述瞎写,自己区分项目内容

丢弃旧的构建,选择构建天数,输入保持构建的最大个数。然后直接拉到最下面,在流水线里选择pipeline script或者pipeline script from SCM

先贴一下自己常用的一段代码

def createVersion() {
// 定义一个版本号作为当次构建的版本,输出结果 20191210175842_69
return new Date().format('yyyyMMddHHmmss') + "_${env.BUILD_ID}"
} pipeline{
agent any
parameters {
gitParameter branch: '', branchFilter: '.*', defaultValue: 'master', description: '选择代码分支', name: 'branch', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'PT_BRANCH'
choice choices: ['张晓琳', '裴明明', '刘新潮'], description: '填写开发人员', name: 'developer'
text defaultValue: '', description: '请填写更新日志', name: 'record'
}
environment {
def ip = "ip地址"
def remotedir = "/home/wwwroot/hxtPayAdmin"
def _version = createVersion()
def tmpdir = "/tmp/hxtPayAdmin_${_version}"
def today = sh(script: "echo `date +%Y_%m_%d`", returnStdout: true).trim()
} stages{
stage('拉取代码'){
steps{
checkout([$class: 'GitSCM', branches: [[name: '$branch']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '9df82ae3-5563-4d32-808b-00e8aa9ae26c', url: 'git@192.168.80.13:php/hxtPayAdmin.git']]])
}
}
stage('备份服务器文件'){
steps{
sh label: '', script: '''ansible ${ip} -m file -a "path=$tmpdir state=directory"
ansible ${ip} -m shell -a "rsync -aqp --exclude=\'storage\' --exclude=\'*.csv\' $remotedir $tmpdir"'''
}
}
stage('发布文件到服务器'){
steps {
sh label: '', script: 'chown www.www . -R'
sh label: '', script: 'ansible ${ip} -m synchronize -a "src=${WORKSPACE}/ dest=$remotedir delete=no archive=yes compress=yes rsync_opts=--exclude=\'.git\',--exclude=\'storage\',--exclude=\'vendor\',--exclude=\'.env\'"'
}
}
}
post('send mail'){
always{
emailext(
subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} -${BUILD_STATUS}!',
body: '${FILE,path="/root/script/email.html"}',
to: 'guoyabin@96199.com.cn') echo "-----构建状态: ${currentBuild.currentResult}-----" sh label: '', script: '''
now=`date "+%Y_%m_%d_%H_%M_%S"`
echo "时间: $now" >> /root/update_log/$today
echo "开发者: $developer" >> /root/update_log/$today
echo "git地址: git@192.168.80.13:php/hxtPayAdmin.git" >> /root/update_log/$today
echo "项目名称: ${JOB_NAME}" >> /root/update_log/$today
echo "构建编号: $BUILD_NUMBER" >> /root/update_log/$today
echo "分支名字: $branch" >> /root/update_log/$today
echo "更新日志: $record" >> /root/update_log/$today
''' sh label: '', script: "echo 构建状态: ${currentBuild.currentResult} >> /root/update_log/$today"
sh label: '', script: "echo -e \'-----------------------------------\\n\' >> /root/update_log/$today"
}
}
}

  

贴一下body里的发email代码

<!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>
编写Jenkinsfile添加构建后发送邮件
  <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><b><font color="#0B610B">构建日志 (最后 100行):</font></b>
      <hr size="2" width="100%" align="center" /></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>

步骤四、点击构建,测试,查看日志流程。

五、其他

pipeline语法可以从流水线语法这里检查学习,

jenkins pipeline构建项目的更多相关文章

  1. Jenkins + Pipeline 构建流水线发布

      Jenkins + Pipeline 构建流水线发布 利用Jenkins的Pipeline配置发布流水线 参考: https://jenkins.io/doc/pipeline/tour/depl ...

  2. jenkins pipeline构建后发送邮件通知

    jenkins pipeline构建后发送邮件通知 mail配置 进入系统配置 找到最下边的邮件通知 创建任务 Pipeline片段 post { always { bat "" ...

  3. jenkins pipeline构建后返回构建结果给gitlab

    jenkins pipeline构建后返回构建结果给gitlab 使用场景 gitlab 合并请求时要求管道任务必须成功,否则无法执行合并操作,又不想使用gitlab ci 工具. 实现方法 1.Ge ...

  4. jenkins(2): jenkins定时构建项目

    参考:http://blog.sina.com.cn/s/blog_b5fe6b270102v7xo.html https://blog.csdn.net/xueyingqi/article/deta ...

  5. jenkins的构建项目配置

    继http://www.cnblogs.com/yajing-zh/p/5109517.html搭建好jenkins系统配置之后,新建jenkins构建项目,用于自动化构建. 点击Jenkins界面左 ...

  6. DEVOPS技术实践_02:jenkins自动构建项目

    一.用户名密码错误 打开jenkins发现用户名密码错误,解决 1.1 找到config.xml文件 [root@jenkins-master ~]# ll -a drwxr-xr-x. root r ...

  7. Jenkins定时构建项目

    我们是不是可以自动的来构建项目那么此时我们需要使用Poll SCM和Build periodically,我们在构建触发中选择这两项即可,其实他们两个就是一个自动任务 触发远程构建:触发远程构建Bui ...

  8. jenkins + pipeline构建自动化部署

    一.引言 Jenkins 2.x的精髓是Pipeline as Code,那为什么要用Pipeline呢?jenkins1.0也能实现自动化构建,但Pipeline能够将以前project中的配置信息 ...

  9. jenkins+git 构建项目

    首先先参照了网上几篇文章 http://blog.csdn.net/kefengwang/article/details/54233584 https://www.liaoxuefeng.com/ar ...

随机推荐

  1. 移动端H5选择本地图片

    移动端H5选择本地图片 html://input<input type="file" accept="image/*" capture="cam ...

  2. Android一句话 | ViewGroup事件分发

    ViewGroup中可重写的关于事件分发的事件有dispatchTouchEvent,onTouchEvent,onInterceptTouchEvent和requestDisallowInterce ...

  3. TextBox,RichTextBox设置行高

    /// <summary> /// 设置行距 /// </summary> /// <param name="ctl">控件</param ...

  4. 【刷题-LeetCode】123 Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...

  5. 实习之bii--配置esxi重启时,虚拟机也跟随重启

    由于初创环境不稳定又是服务器会重启,而内部安装的多部虚拟机并不默认跟随启动,需要设置,方法如下: 1.在本地通过vsphere client 登录到esxi的服务器上,然后点击配置找到虚拟机启动/关机 ...

  6. Rust 使用 dotenv 来设置环境变量

    在项目中,我们通常需要设置一些环境变量,用来保存一些凭证或其它数据,这时我们可以使用 dotenv 这个 crate. 首先在项目中添加 dotenv 这个依赖: 例如在下面这个项目中,需要设置数据库 ...

  7. js整体

    1.引入 <script type="text/javascript"> 2.输出 使用 window.alert() 写入警告框  使用 document.write ...

  8. 【Vue源码学习】响应式原理探秘

    最近准备开启Vue的源码学习,并且每一个Vue的重要知识点都会记录下来.我们知道Vue的核心理念是数据驱动视图,所有操作都只需要在数据层做处理,不必关心视图层的操作.这里先来学习Vue的响应式原理,V ...

  9. python变量系列--1

    所以这里需要注意变量的使用,print(message),直接去引用,和shell略有不同 1.变量名只能包含字母,数字,下划线,但不能以数字开头 2.变量名不能含有空格

  10. 通俗讲解IP地址的端口

    ========================================================================================= 在我看来,学习jav ...