• Jenkins项目构建细节(2)-Git hook自动触发构建(重要)


    刚才我们看到在Jenkins的内置构建触发器中,轮询SCM可以实现Gitlab代码更新,项目自动构建,但是

    该方案的性能不佳。那有没有更好的方案呢? 有的。就是利用Gitlab的webhook实现代码push到仓

    库,立即触发项目自动构建。

    image

    1.jenkins操作----------------------------------------------

    安装Gitlab Hook插件

    需要安装两个插件:

    Gitlab Hook和GitLab

    image

    设置自动构建

    image

    image

    等会需要把生成的webhook URL配置到Gitlab中



    2.Gitlab操作-----------------------------------------------------------------

    Gitlab配置webhook

    1)开启webhook功能

    使用root账户登录到后台,点击Admin Area -> Settings -> Network > outbound requests

    勾选"Allow requests to the local network from web hooks and services"

    image

    image

    2)在项目添加webhook

    点击项目->Settings->Integrations

    注意:一定要在项目下配置


    image


    这里填入的连接是jenkins安装了webhook后生成的连接,把它复制到gitlab

    image

    image


    jenkins配置--------------------------------------------------

    image

    注意:以下设置必须完成,否则会报错!

    Manage Jenkins->Confifigure System

    image

    image



    回到gitlab操作---------------------------------------------------------------------

    image

    image




    3.通过以上设置jenkins gitlab设置好了,现在jenkins配置构建脚本,先测试流水线测试

    参考链接: https://www.bilibili.com/video/BV1kJ411p7mV?p=27

    1.拉取代码

    image

    2.构建(不用生成的方式也行 )

    image

    image

    3.命令执行不用脚本生成器方式

    image

    4.远程项目部署

    image

    全部代码

    pipeline {
         agent any

        stages {
             stage('拉取代码') {
                 steps {
    checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'fbc65d5d-5c95-4313-9d2b-306e8dbc200c', url:'git@192.168.66.102:itheima_group/web_demo.git']]])
                 }
             }
                     stage('编译构建') {
                 steps {
                     sh  'mvn clean package'
                 }
             }
                     stage('项目部署') {
                 steps {
       deploy adapters: [tomcat8(credentialsId: 'ad6cc9f6-c3a0-4c80-bdc1-f3ddb3aa915d', path: '', url: 'http://192.168.66.102:8080/')], contextPath: null, war: 'target/*.war'
                 }
             }
         }
    }

    image

    通过以上步骤实现了流水线构建,现在测试流水线的自动构建

    1.在idea上新建Jenkinsfile脚本,并把以上的脚本内容复制进去

    因为如果脚本放在jenkins上容易丢失,大概步骤

    1.生成git代码

    2.编译打包

    3.构建

    下图配置好的结果图:

    image

    2.把脚本加入版本管理,先提交到本地仓库

    image

    push推上去

    image

    image


    回到jenkins配置,选择指定脚本的方式。这样脚本就不用写在jenkins上了


    image

    image


    最后测试.idea上更改代码后提交会发现一提交就会触发构建

    image

    更改代码后提交-push

    image

    回到jenkins里看看发现自动在构建

    image

  • 相关阅读:
    orleans 的一种模式
    在.net4的环境下使用Microsoft.AspNet.SignalR.Client 2.4.0
    微信卡券领用的附加测试
    SVN忽略本地文件不提交,同时不删除服务器上的文件
    SQL Server 2017安装错误:Polybase要求安装Oracle JRE 7更新51或更高版本的两种解决方法
    SQL Server遍历表(临时表)
    无法确定条件表达式的类型,因为“DateTime”和“<null>”之间没有隐式转换|Nullable类型问题与?:条件运算符
    C# 反射获取对象的内容
    c# 计算执行时间,性能,运行时间Stopwatch
    JS,JQuery循环数组,循环对象生成需要的数据
  • 原文地址:https://www.cnblogs.com/pengrj/p/13679512.html
Copyright © 2020-2023  润新知