zoukankan      html  css  js  c++  java
  • Jenkins2.x Pipeline持续集成交互

    原文地址:http://blog.csdn.net/aixiaoyang168/article/details/72818804

    Pipeline的几个基本概念:

    • Stage: 阶段,一个Pipeline可以划分为若干个Stage,每个Stage代表一组操作。注意,Stage是一个逻辑分组的概念,可以跨多个Node。
    • Node: 节点,一个Node就是一个Jenkins节点,或者是Master,或者是Agent,是执行Step的具体运行期环境。
    • Step: 步骤,Step是最基本的操作单元,小到创建一个目录,大到构建一个Docker镜像,由各类Jenkins Plugin提供。

    2、环境、软件准备

    本次演示环境,我是在本机mac上操作,以下是我本地软件及版本:

    1. Jenkins:version 2.46.3
    2. Tomcat:version 7.0.70
    3. Jdk:version “1.8.0_91”
    4. Docker: Version 17.03.0-ce-mac1 (15583)
    5. Gitlab: GitLab Community Edition 8.17.4

    注意:本次我们分别演示两种方式安装jenkins,基于Tomcat和Jdk安装,我们需要提前安装好Jdk、Tomcat服务,基于Docker安装,我们需要提前安装docker环境。这里我就忽略Tomcat、Jdk、docker、gitlab的安装过程,着重说下Jenkins安装以及如何跑Pipeline Job。

    3、安装、启动并配置jenkins服务

    一、Jenkins安装启动方式有两种,一种是基于tomcat、Jdk启动,一种是基于Docker启动。

    1)基于Tomcat、Jdk启动

    1. 首先下载Jenkins最新的安装包,可以去官网下载最新版,点击 这里 下载。
    2. 启动Jenkins可以有两种方式
      • 进入war包所在目录,直接执行java -jar jenkins.war
      • 将war包放在Tomcat webapps目录下,启动tomcat。

    2)基于Docker启动

    1. 拉取jenkins官方镜像

      docker pull jenkins
      • 1
    2. 启动jenkins 容器

      docker run -p 8080:8080 -p 50000:50000 -v /Users/wanyang3/jenkins_home:/var/jenkins_home jenkins
      • 1

      启动完成之后,浏览器访问http://localhost:8080,第一次启动初始化稍慢一些,稍等一会就可开始jenkins初始化配置。

    二、Jenkins初始化配置

    1)、解锁Jenkins —》 Unlock Jenkins

    Unlock Jenkins

    说明:按照弹框提示,找到该initialAdminPassword文件,我这里使用Docker启动Jenkins,并且把jenkins_home目录挂载到我磁盘指定目录,所以这里我只需要复制/Users/wanyang3/jenkins_home/initialAdminPassword即可,如果非挂载方式Docker启动,则需要进入容器内根据提示路径找到该文件。

    2)定制 Jenkins Customize Jenkins

    Jenkins Customize Jenkins

    Getting Started

    说明:这里若选择Install suggested plugins安装,那么jenkins就会给你推荐安装一些很有用的插件,若选择Select plugins to install安装,那么就需要自己根据业务需要选择性安装某些插件。

    3)创建第一个管理员用户,Create first admin user

    Create first admin user

    说明:这里创建第一个管理员用户,也可以不设置,直接点击“Continue as admin”,进入jenkins以后再设置。

    4、新建Pipeline Job Demo

    1)创建一个pipeline job

    pipeline job

    创建完成后,点击该job —》设置 —》 Pipeline,在输入框中输入script语句。

    示例script:

    node{
        stage('get clone'){
            //check CODE
           git credentialsId: 'f3eb1fea-42b0-46b2-8342-a2be6a65fe73', url: 'http://xx.xx.xx/xx/qd_api.git'
        }
    
        //定义mvn环境
        def mvnHome = tool 'M3'
        env.PATH = "${mvnHome}/bin:${env.PATH}"
    
        stage('mvn test'){
            //mvn 测试
            sh "mvn test"
        }
    
        stage('mvn build'){
            //mvn构建
            sh "mvn clean install -Dmaven.test.skip=true"
        }
    
        stage('deploy'){
            //执行部署脚本
            echo "deploy ......" 
        }
    }

    注意:这里job执行pipeline定义,可以有两种方式,一种直接在job填写pipeline script来执行,
    一种是使用pipeline script from SCM。

    • pipeline script:直接在Script输入框里面输入pipeline script语句即可,参考说明可以点击输入框下边的Pipeline Syntax,里面有很多示例操作说明,非常好用。
    • pipeline script from SCM:需要配置SCM代码存储Git地址或SVN地址,指定script文件所在路径,每次构建job会自动去指定的目录执行script文件。

    2)配置全局工具配置Maven

    因为我们的项目是Maven工程,这次执行build需要使用mvn命令,所以需要配置一个全局的Maven。
    进入到 系统管理 -》Global Tool —》Maven -》Maven安装,指定Name、MAVEN_HOME、选择要安装的Mavne版本,自动安装即可。

    Global Tool Maven

    3)执行构建

    点击“立即构建”,即可开始构建,右侧Stage View查看构件流程,点击每个stage,可以查看每个阶段的详细日志输出。

    立即构建

    FAQ

    1. 使用插件 mvn 命令,在script语句里面,我们使用的tool工具来获取全局Maven配置M3,这里我们也可以使用Pipeline Maven Integration Plugin插件来完成。
      点击插件管理 —》可选插件 —》Pipeline Maven Integration Plugin —》立即安装,安装完成之后,就可以使用该插件使用mvn命令了。

      Pipeline Maven Integration Plugin

      示例script:

      node{
          stage('get clone'){
              //check CODE
             git credentialsId: 'f3eb1fea-42b0-46b2-8342-a2be6a65fe73', url: 'http://xx.xx.xx/xx/qd_api.git'
          }
      
          stage('mvn test'){
              withMaven(
                  maven: 'M3') {
                      sh "mvn test"
              }
          }
      
          stage('mvn build'){
              //mvn构建
              withMaven(
                  maven: 'M3',
                  mavenLocalRepo: '.repository') {
                      sh "mvn clean install -Dmaven.test.skip=true"
              }
          }
      
          stage('deploy'){
              //执行部署脚本
              echo "deploy ......" 
          }
      }
    2. 这里check code检出代码操作,jenkins默认集成github,这里我们使用自己的gitlab,clone项目需要用户名密码登录,这里我们可以使用jenkins的credentials创建证书,生成证书以后,在clone代码时,指定git credentialsId,即可完成认证工作。 若不知道生成的证书id是多少,这里有个好办法,去每个项目的pipeline-syntax,默认进入到Snippet Generator(代码段生成器),我们选择git: Git,然后输入Repository URL、Branch、选择Credentials,点击Generate Pipeline Script,在下方输入框里面,就可以生成对应的流程的脚本语句,是不是很方便。
      pipeline-syntax

    持续集成交互(英文指导:https://www.mindtheproduct.com/2016/02/what-the-hell-are-ci-cd-and-devops-a-cheatsheet-for-the-rest-of-us/)

    例子script (From http://www.ciandcd.com/?p=155):

    // Run this on the master node:
    node  {
        // The JDK is configured as a tool with the name 'jdk-8u77' in the Jenkins 'Global Tool Configuration'
        env.JAVA_HOME="${tool 'jdk-8u77'}"
        env.PATH="${env.JAVA_HOME}/bin:${env.PATH}"
     
        // Maven is configured as a tool with the name 'M3' in the Jenkins 'Global Tool Configuration'.
        def mvnHome = tool 'M3'
        
        stage 'Checkout'
        // Configure the credential in Jenkins, and use the credential's ID in the following step:
        git url: 'ssh://git@gitrepo.computas.com/fs/fs-knowledge-editor.git', credentialsId: '8dbfb6d2-2549-4c6e-9a6e-994ae8797efc'
         
        stage 'Build and tag'
        // Define the version of this build.
        // BASE_VERSION is defined as a build parameter in the UI definition of the job.
        // Note how Groovy code is used to format the number of the current build.
        def version = "${BASE_VERSION}-J2TEST-" + currentBuild.number.toString().padLeft(4,'0')
        // Execute the maven command as a shell command step. On Windows, a 'bat'-step would be used instead.
        sh "${mvnHome}/bin/mvn clean verify -f KnowledgeEditor/pom.xml -Dfs.version=${version}"
        // Archive the zip file for access in through the Jenkins UI, or for other uses.
        archive 'KnowledgeEditor/com.computas.fs.ke.products/target/products/*.zip'
         
        // Each build is tagged with an annotated tag.
        // There is no pipeline plugin for this (the Git Publisher plugin is not compatible),
        // so everything has to be implemented using shell commands.
        // First, we have to configure git with the mandatory user information:
        sh "git config user.name "Jenkins Pipeline""
        sh "git config user.email bob@computas.com"
        // Next, tag this commit.
        def msg = ""Automatically created tag ${version}""
        sh "git tag -a -m ${msg} ${version}"
        // Finally, push to the repo.
        // For this to work, the ssh keys must be available in Jenkins' ~/.ssh folder
        sh "git push origin ${version}"
        
        // Send a mail to the person responsible for manual testing and release.
        mail subject: 'A new version of KEIII is available for testing.',
            body: 'A new version of KEIII is available for testing and approval of release.',
            charset: 'utf-8',
            from: 'bob@computas.com',
            mimeType: 'text/plain',
            to: 'fs-tester@computas.com'
     
        stage 'Release'
        // User input showing up in the Jenkins UI.
        // If the timeout is reached, an exception is thrown and the build aborted.
        timeout(time: 120, unit: 'SECONDS') {
            input message: 'Do you want to release this version, ' +  version + ', of KEIII?', ok: 'Release'
        }
        // A catch block could deal with the exception.
         
        // In order to release to Nexus, deploy and access information needs to be made available in
        // a maven settings file. This configuration is copied into the file defined as
        // mavenSettingsFile from the corresponding managed file in Jenkins...
        def mavenSettingsFile = "${pwd()}/.m2/settings.xml"
         
        // ... using the configuration file build wrapper:
        wrap([$class: 'ConfigFileBuildWrapper',
                managedFiles: [
                    [fileId: '85adba0c-908b-4dbf-b3aa-65fe823e8984',
                    targetLocation: "${mavenSettingsFile}"]]]) {
            // deploy to Nexus
            sh "${mvnHome}/bin/mvn deploy -s ${mavenSettingsFile} -f KnowledgeEditor/pom-deploy.xml -Dfs.version=${version}"
        }
    

     }

    参考文档:
    pipeline hello-world
    pipeline-plugin
    using-pipeline-plugin-accelerate-continuous-delivery-part-1
    using-pipeline-plugin-accelerate-continuous-delivery-part-2
    using-pipeline-plugin-accelerate-continuous-delivery-part-3

  • 相关阅读:
    findall查找 ^$*+?{ }{m,n}[].[.] w s d  D W
    find查找、split分隔、replace替换
    round四舍五入
    pow求一个数的n次幂
    iter创建一个可以被迭代的对象
    notepad++ gmt中文乱码问题
    matlab eps 字体用AI打开乱码的解决
    [转载]Matlab中使用xlswrite函数时出现服务器出现异常的解决方法
    How to determine which grid cells a line segment passes through?
    matlab给定点生成多边形,多边形掩膜处理
  • 原文地址:https://www.cnblogs.com/boonya/p/7508477.html
Copyright © 2011-2022 走看看