Jenkins的精髓是Pipeline(流水线技术),那为何要用Pipeline呢?实现自动化构建,其中Pipeline可以将之前project中的配置信息以steps的方式放在一个脚本里,将本来独立运行于单个或者多个节点的任务链接起来,实现单个任务难以完成的复杂流程,造成流水式发布,构建步骤视图化。node
简单来讲,Pipeline适用的场景更普遍,能胜任更复杂的发布流程。举个例子,job构建工做在master节点,自动化测试脚本在slave节点,而不一样节点的执行处理经过Pipeline能够。python
【Stage: 阶段】,一个Pipeline能够划分为若干个Stage,每一个Stage表明一组操做。注意,Stage是一个逻辑分组的概念,能够跨多个Node。git
【Node: 节点】,一个Node就是一个Jenkins节点,或者是Master,或者是slave,是执行Step的具体运行期环境。github
【Step: 步骤】,Step是最基本的操做单元,小到建立一个目录,大到构建一个Docker镜像,由各种Jenkins Plugin提供。golang
二者都是pipeline代码的持久实现,都可以使用pipeline内置的插件或者插件提供的steps,二者均可以利用共享库扩展。docker
二者不一样之处在于语法和灵活性。Declarative pipeline对用户来讲,语法更严格,有固定的组织结构,更容易生成代码段,使其成为用户更理想的选择。windows
可是Scripted pipeline更加灵活,由于Groovy自己只能对结构和语法进行限制,对于更复杂的pipeline来讲,用户能够根据本身的业务进行灵活的实现和扩展。app
pipeline { agent any //在可用的节点运行 stages{ stage ('Prepare'){ steps{ //清空发布目录 bat '''if exist D:\\publish\\LoginServiceCore (rd/s/q D:\\publish\\LoginServiceCore) if exist C:\\Users\\Administrator\\.nuget (rd/s/q C:\\Users\\Administrator\\.nuget) exit''' } } //拉取git代码仓库 stage ('Checkout'){ steps{ checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'c6d98bbd-5cfb-4e26-aa56-f70b054b350d', url: 'http://xxx/xxx/xxx']]]) } } //构建 stage ('Build'){ steps{ bat '''cd "D:\\Program Files (x86)\\Jenkins\\workspace\\LoginServiceCore\\LoginApi.Hosting.Web" dotnet restore dotnet build dotnet publish --configuration Release --output D:\\publish\\LoginServiceCore''' } } //部署 stage ('Deploy'){ steps{ bat '''cd D:\\PipelineScript\\LoginServiceCore python LoginServiceCore.py''' } } //自动化测试(python代码实现) stage ('Test'){ steps{ bat'''cd D:\\PipelineScript\\LoginServiceCore python LoginServiceCoreApitest.py''' } } } }
node('master') { //master节点运行,如下stage也可指定节点 stage 'Prepare' //清空发布目录 bat '''if exist D:\\publish\\LoginServiceCore (rd/s/q D:\\publish\\LoginServiceCore) if exist C:\\Users\\Administrator\\.nuget (rd/s/q C:\\Users\\Administrator\\.nuget) exit''' //拉取git代码仓库 stage 'Checkout' checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'c6d98bbd-5cfb-4e26-aa56-f70b054b350d', url: 'http://xxx/xxx/xxx']]]) //构建 stage 'Build' bat '''cd "D:\\Program Files (x86)\\Jenkins\\workspace\\LoginServiceCore\\LoginApi.Hosting.Web" dotnet restore dotnet build dotnet publish --configuration Release --output D:\\publish\\LoginServiceCore''' //部署 stage 'Deploy' bat ''' cd D:\\PipelineScript\\LoginServiceCore python LoginServiceCore.py ''' //自动化测试(python代码实现) stage 'Test' bat''' cd D:\\PipelineScript\\LoginServiceCore python LoginServiceCoreApitest.py ''' }
node{ // 代码检出 stage('get Code') { git credentialsId: 'git-credentials-id', url: 'http://192.168.19.250/libo/test.git' } // 镜像中进行单元测试 stage('unit testing'){ // 启动golnag:1.7并在golang内编译代码 docker.image('golang:1.7').inside { sh './script/unittest.sh' } } // 镜像中代码构建 stage('Build'){ def confFilePath = 'conf/app.conf' def config = readFile confFilePath writeFile file: confFilePath, text: config // 启动golnag:1.7并在golang内编译代码 docker.image('golang:1.7').inside { sh './script/build.sh' } } // 编译镜像并push到仓库 def imagesName = '192.168.18.250:5002/ufleet/uflow:v0.9.1.${BUILD_NUMBER}' stage('Image Build And Push'){ docker.withRegistry('http://192.168.18.250:5002', 'registry-credentials-id') { docker.build(imagesName).push() } } // 启动刚运行的容器 stage('deploy iamegs'){ // 须要删除旧版本的容器,不然会致使端口占用而没法启动。 try{ sh 'docker rm -f cicdDemo' }catch(e){ // err message } docker.image(imagesName).run('-p 9091:80 --name cicdDemo') } }
withCredentials([usernamePassword(credentialsId: '<credentials-id>', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) { sh ''' printf "machine github.com\nlogin $GIT_USERNAME\n password $GIT_PASSWORD" >> ~/.netrc // continue script as necessary working with git repo... ''' }
checkout scm: ([ $class: 'GitSCM', userRemoteConfigs: [[credentialsId: '******',url: ${project_url}]], branches: [[name: 'refs/tags/${project_tag}']] ])