Jenkins-pipeline的实现步骤

jenkins实现持续集成

  • 搭建jenkins环境,安装插件
  • 建立pipeline公用类库,文件夹vars,默认的
  • 添加.groovy文件,可以由以下几个类库组成
    • dockerImageBuild 负责构建项目镜像
    • dockerImageDeploy 负责将镜像推到仓库
    • dockerServiceStart 负责启动docker服务
    • gitCheckout 负责迁出项目源码
    • servicePipeline 程序入口
  • 根据项目添加对应的.jenkinsfile文件
  • jenkins里建立文件夹,选择公用类库的仓库,起个名字
  • 在文件夹里建立新的Job,然后选择对象的jenkinsfile文件
  • 项目构建完成

相关源代码分享

  1. gitCheckout源代码
def call(body) {
    def config = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config
    body()

    echo 'Checking out code from "' + config.repoUrl + '" with credentialsId "' + \
        config.credentialsId + '" ...'
    
    checkout([$class: 'GitSCM', branches: [[name: config.branches]], 
    doGenerateSubmoduleConfigurations: false, 
    extensions: [], 
    submoduleCfg: [], 
    userRemoteConfigs: [[credentialsId: config.credentialsId, url: config.repoUrl]]])   
}
  1. servicePipeline.groovy源码
def call(body) {
    def config = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config
    body()

    pipeline {
        agent { label 'build-server' }
       
        stages {
            stage('Initialization') {
                steps {
                    script {
                       echo "项目初始化"
                    }
                }
            }
            stage('CI') {
                steps{
                    script{
                        gitCheckout{
                            repoUrl = config.repoUrl
                            credentialsId = config.credentialsId
                            branches = config.branches
                            commit = config.commit
                        }
                    }
                }
            }
            stage('Service') {
            echo "启动dockder服务"
          }
        }
    }
}
  1. lind.jenkinsfile项目部署源码
@Library("lind-library") _

servicePipeline ( {
   repoUrl = "git@github.com:bfyxzls/LindDotNetCore.git"
   credentialsId = "012f0d4e-47e2-48ce-8b9e-cd399e9b3d61"
   branches = "dev"
 })

事实上,上面的构建只是一个基础版本,当你的构建有多种语言组成时,你需要为他们分别写一个入口,如dotnet,nodejs,java,php,它们有自己的入口,然后再servicePipeline里去分别处理,前开发人员输入语言类型,然后统一进行处理。

posted @ 2018-07-10 13:49  张占岭  阅读(2836)  评论(0编辑  收藏  举报