DEVOPS技术实践_08:Jenkins多分支管道

简介

多分支的管道是在jenkins2.x中新增的功能 。

多分支管道允许你针对分布式的控制器的每个分支创建一个管道。 下图是对它的一个描述。使用jenkinsfile去创建多分支的管道,jenkinsfile可以存放在代码仓库中。 Jenkinsfile只是定义CI管道的一个脚本。

另外,多分支管道的设计初衷就是当Git仓库中的代码改变时,去自动触发构建。下图是对它的一个描述。

一. 准备工作

(1)在【全局工具配置】中Maven工具已经配置好。
(2)安装了【Pipeline Maven Integration Plugin】
(3)Java工具也需要安装好,由于我们构建是在jenkins master上执行,所以可以跳过此步(因为我们在安装Jenkins时,已经安装好Java)
(4)安装Gitlab插件,本实验使用gitlab做为代码管理

添加gitlab凭据到jenkins中

添加全局凭据

二. 从jenkins中配置在Gitlab服务器的webhooks

gitlab创建个人访问令牌(personal access token)

2.1 登录到gitlab,点击Settings

2.2 在右侧菜单栏中,点击Access Tokens

2.3 输入令牌名称、到期时间、令牌作用域等选项,点击Create personal access token按钮

2.4 查看生成的个人访问令牌,并保存到安全位置,页面刷新后将无法查看

jenkins添加gitlab

2.5 添加凭据

添加后,test测试报错

点击高级模式,选择API-levle为v3

升级git版本

[[email protected] ~]# wget https://github.com/git/git/archive/v2.2.1.tar.gz

[[email protected] ~]# tar -xf v2.2.1.tar.gz

[[email protected] ~]# cd git-2.2.1/

[[email protected] git-2.2.1]# yum install perl-ExtUtils-MakeMaker

[[email protected] git-2.2.1]# yum install zlib-devel

[[email protected] git-2.2.1]# make configure
GIT_VERSION = 2.2.1
GEN configure

[[email protected] git-2.2.1]# ./configure --prefix=/usr/local/git --with-iconv=/usr/local/libiconv

[[email protected] git-2.2.1]# make

[[email protected] git-2.2.1]# make  install

[[email protected] git-2.2.1]# ln -s /usr/local/git/bin/git  /usr/bin/

[[email protected] git-2.2.1]# git --version

git version 2.2.1

问题依然出现,最后排查的过程中发现,复制的不是真正的token,这是坑爹的地方

2.6 jenkins查看成功

安装成功

三 创建一个多分支

3.1 编辑jenkinsfile

 node (‘master‘) {
    checkout scm
    stage(‘Build‘) {
      withMaven(maven: ‘M3‘) {
        if (isUnix()) {
          sh ‘mvn -Dmaven.test.failure.ignore clean install package‘
        }
        else {
          bat ‘mvn -Dmaven.test.failure.ignore clean install package‘
        }
      }
    }
    stage(‘Results‘) {
      junit ‘**/target/surefire-reports/TEST-*.xml‘
      archive ‘target/*.jar‘
    }
  } installinstall

提交,开始创建多分枝流水线

3.2 创建多分枝项目

3.3 Trouble Shootting

出现错误:是因为 /usr/libexec/git-core/ 路径没在 PATH 环境变量中。

Started by user darren ning
[Sat Oct 26 14:12:57 EDT 2019] Starting branch indexing...
 > git --version # timeout=10
using GIT_ASKPASS to set credentials
 > git ls-remote http://192.168.132.132/root/simple-maven-project-with-tests.git # timeout=10
ERROR: [Sat Oct 26 14:12:57 EDT 2019] Could not update folder level actions from source 9d2e73ea-7ee8-4128-a8b2-0d7f20e75599
hudson.plugins.git.GitException: Command "git ls-remote http://192.168.132.132/root/simple-maven-project-with-tests.git" returned status code 128:
stdout:
stderr: fatal: Unable to find remote helper for ‘http‘

    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2042)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1761)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1666)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1657)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.getRemoteReferences(CliGitAPIImpl.java:2877)
    at jenkins.plugins.git.AbstractGitSCMSource.retrieveActions(AbstractGitSCMSource.java:1139)
    at jenkins.scm.api.SCMSource.fetchActions(SCMSource.java:848)
    at jenkins.branch.MultiBranchProject.computeChildren(MultiBranchProject.java:592)
    at com.cloudbees.hudson.plugins.folder.computed.ComputedFolder.updateChildren(ComputedFolder.java:277)
    at com.cloudbees.hudson.plugins.folder.computed.FolderComputation.run(FolderComputation.java:164)
    at jenkins.branch.MultiBranchProject$BranchIndexing.run(MultiBranchProject.java:1026)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:429)
[Sat Oct 26 14:12:57 EDT 2019] Finished branch indexing. Indexing took 19 ms
FATAL: Failed to recompute children of simple-maven-project-with-tests
hudson.plugins.git.GitException: Command "git ls-remote http://192.168.132.132/root/simple-maven-project-with-tests.git" returned status code 128:
stdout:
stderr: fatal: Unable to find remote helper for ‘http‘

    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2042)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1761)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1666)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1657)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.getRemoteReferences(CliGitAPIImpl.java:2877)
    at jenkins.plugins.git.AbstractGitSCMSource.retrieveActions(AbstractGitSCMSource.java:1139)
    at jenkins.scm.api.SCMSource.fetchActions(SCMSource.java:848)
    at jenkins.branch.MultiBranchProject.computeChildren(MultiBranchProject.java:592)
    at com.cloudbees.hudson.plugins.folder.computed.ComputedFolder.updateChildren(ComputedFolder.java:277)
    at com.cloudbees.hudson.plugins.folder.computed.FolderComputation.run(FolderComputation.java:164)
    at jenkins.branch.MultiBranchProject$BranchIndexing.run(MultiBranchProject.java:1026)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE

添加xport PATH=/usr/libexec/git-core:$PATH到/etc/profile里

在此执行

3.4 控制台输出

Started by user darren ning
[Sat Oct 26 14:30:07 EDT 2019] Starting branch indexing...
 > git --version # timeout=10
using GIT_ASKPASS to set credentials
 > git ls-remote http://192.168.132.132/root/simple-maven-project-with-tests.git # timeout=10
Creating git repository in /root/.jenkins/caches/git-308bd15be61318b6e1166e23433fdd72
 > git init /root/.jenkins/caches/git-308bd15be61318b6e1166e23433fdd72 # timeout=10
Setting origin to http://192.168.132.132/root/simple-maven-project-with-tests.git
 > git config remote.origin.url http://192.168.132.132/root/simple-maven-project-with-tests.git # timeout=10
Fetching & pruning origin...
Listing remote references...
 > git config --get remote.origin.url # timeout=10
 > git --version # timeout=10
using GIT_ASKPASS to set credentials
 > git ls-remote -h http://192.168.132.132/root/simple-maven-project-with-tests.git # timeout=10
Fetching upstream changes from origin
 > git config --get remote.origin.url # timeout=10
using GIT_ASKPASS to set credentials
 > git fetch --tags --progress origin +refs/heads/*:refs/remotes/origin/* --prune
Checking branches...
  Checking branch master
      ‘Jenkinsfile’ found
    Met criteria
Scheduled build for branch: master
  Checking branch feature
      ‘Jenkinsfile’ found
    Met criteria
Scheduled build for branch: feature
Processed 2 branches
[Sat Oct 26 14:30:08 EDT 2019] Finished branch indexing. Indexing took 1 sec
Finished: SUCCESS

已经完成

原文地址:https://www.cnblogs.com/zyxnhr/p/11747648.html

时间: 2024-08-01 11:22:27

DEVOPS技术实践_08:Jenkins多分支管道的相关文章

DEVOPS技术实践_08:声明式管道语法

简介 前面简单的做了管道的实验,看了一下的它的效果 声明式管道是Groovy语法中的一个更简单和结构化的语法.下面主要学习明式管道语法. 一 声明式管道的基本结构 以上节的代码为例 node { def mvnHome stage('Preparation') { // for display purposes // Get some code from a GitHub repository git 'https://github.com/jglick/simple-maven-project

DEVOPS技术实践_11:Jenkins集成Sonar

前言 前面已经有介绍sonar的安装,简单应用,下面在简答的研究一下sonar和jenkins集成的简单使用,对于sonar的安装不做介绍 一 sonar的简单介绍 持续检查避免了低质量的代码,比如SonarQube工具就能够帮助我们完成此项.每次代码提交后,在代码上就会执行代码分析. 分析是基于代码分析工具中定义的一些规则,如果代码通过了错误阀值,那么它会允许到生命周期的下一步,如果超过了设定的阀值,那么直接返回错误. SonarQube是代码质量管理工具,允许团队去管理,追踪和改善他们的源代

DEVOPS技术实践_12:创建持续集成的管道

持续集成不仅包含了Jenkins或者相关其它的CI工具,也包含了包含代码如何控制,采用的什么分支策略等.不同的组织可能采用不同的类型的策略来完成CI,策略类型和项目的类型的有很大的关系. 一 分支策略 1.1 本实验分支 分支能够有效的对代码较好的管理,也是能够把工作的代码和开发环境的代码隔离的有效方式.主要有三种的分支策略类型– master分支– integration 分支– feature 分支 1.master分支 master分支也叫做生产分支,该分支的代码全部是经过测试OK的代码.

DEVOPS技术实践_13:使用Jenkins持续传送设计-CD基础

1. 分支策略 持续集成中使用的分支策略包括以下三个: The master branch The integration branch The feature branch 而CD只在Integration的release上分支上执行即可. 2. Release 分支 一些团队采用发布分支的策略.release分支是在所有代码在生产环境中经过验证之后创建的.也就是从master上面拉取的.创建release分支的目的就是在相应的版本上进行bug修复. 3. CD 管道 在此处,不会再创建新的p

DEVOPS技术实践_10:安装部署Artifactory

需要一种机制去存储所有的二进制代码(build,packages,third-party plugins等)到类似于版本控制系统的系统. 像Git,SVN存储代码,它们存储的往往是源代码,不是二进制文件.Artifactory或者Nexus就是和Jenkins紧密集成的二进制文件存储库系统. 可以带来以下好处:追踪构建(谁触发?谁构建)依赖关系部署历史 jfrog artifactory是一款二进制存储管理工具,用来管理构建工具(如:maven.gradle)等所依赖的二进制仓库,以方便管理第三

DevOps企业实践与架构

原文地址:http://www.sohu.com/a/112351816_355140 什么是DevOps及其误区 DevOps概念从2009年提出已有8个年头.可是在8年前的那个时候,为什么DevOps没有迅速走红呢?即便是在2006年Amazon发布了ECS,微软在2008年和2010年提出和发布了Azure,DevOps的重要性似乎都没有那么强烈.我分析其原因主要有: 第一个很重要的原因是因为那时候云计算还是小众产品,更多的与虚拟化.虚拟机相关,它们还是重量级的IT基础设施. 第二个很重要

2019 DevOps 技术指南

原文链接:https://hackernoon.com/the-2018-devops-roadmap-31588d8670cb 原文作者:javinpaul 翻译君:CODING?戴维奥普斯 写在前面 我们在推进国内研发团队 DevOps 落地的过程中,发现不少研发组织在积极寻求 DevOps 技能方面的提升.今天翻译的这篇深受欢迎的 DevOps 技术雷达来自一位国外的 Java 博主,他也是一位非常热爱学习的开发者,接下来让我们马上进入到正文. DevOps 技术指南 DevOps 目前非

Redis进阶实践之十八 使用管道模式提高Redis查询的速度

原文:Redis进阶实践之十八 使用管道模式提高Redis查询的速度 一.引言 学习redis 也有一段时间了,该接触的也差不多了.后来有一天,以为同事问我,如何向redis中批量的增加数据,肯定是大批量的,为了这主题,我从新找起了解决方案.目前的解决方案大都是从官网上查找和翻译的,每个实例也都调试了,正确无误.把结果告诉我同事的时候,我也跟清楚这个主题如何操作了,里面的细节也更清楚了.大然也有人说可以通过脚本来做这个操作,没错,但是我对脚本语言还没有研究很透,就不来班门弄斧了. 二.管道的由来

腾讯技术分享:GIF动图技术详解及手机QQ动态表情压缩技术实践

本文来自腾讯前端开发工程师" wendygogogo"的技术分享,作者自评:"在Web前端摸爬滚打的码农一枚,对技术充满热情的菜鸟,致力为手Q的建设添砖加瓦." 1.GIF格式的历史 GIF ( Graphics Interchange Format )原义是"图像互换格式",是 CompuServe 公司在1987年开发出的图像文件格式,可以说是互联网界的老古董了. GIF 格式可以存储多幅彩色图像,如果将这些图像((https://www.q