Jenkins及子任务全自动部署

Ansible基础配置

https://galaxy.ansible.com/geerlingguy/jenkins

Ansible额外配置 – Groovy Scripts

“{{ jenkins_home }}/init.groovy.d/{{ item }}”

Jenkins Seed Job – Job-DSL Groovy Scripts

  • 动态API参考文档: http://<your jenkins>/plugin/job-dsl/api-viewer/index.html
  • Playground: http://job-dsl.herokuapp.com/
  • Deprecated NoTriggerBranchProperty for multibranchPipelineJob
    • 临时configure解决方案
  configure { node ->
    def src = node / sources / data / 'jenkins.branch.BranchSource'
    src << 'buildStrategies' {
      'jenkins.branch.buildstrategies.basic.NamedBranchBuildStrategyImpl' {
        filters {
          'jenkins.branch.buildstrategies.basic.NamedBranchBuildStrategyImpl_-WildcardsNameFilter' {
            includes("")
            excludes("*")
            caseSensitive(false)
          }
        }
      }
    }
  }

Global Shared Libraries – Groovy Scripts

  • http://<your jenkins>/pipeline-syntax/

Jenkinsfile – Groovy Scripts

  • @Library(‘PipelineSharedLib@master’) _

Perforce双Client Checkout解决方案

确保两个client的名称不同:

  • pipelineJob / definition / cpsScm / scm / perforce / workspace / streamSpec with pipelineJob / definition / cpsScm / lightweight and scriptPath
  • checkout() in Jenkinsfile

并从scm中提取相关信息:

  checkout([
    $class:        'PerforceScm',
    credential:    scm.credential,
    workspace:     new StreamWorkspaceImpl(
                     'none',                   // charset
                     false,                    // pinHost
                     scm.workspace.streamName, // streamName
                     "jenkins-${NODE_NAME}-${JOB_NAME}-${EXECUTOR_NUMBER}" // format
                   ),
    populate:      scm.populate
  ])

快速重建git p4 clone过的仓库

背景

  • Bitbucket/Stash仓库 https://stash.abc.com/scm/test/foo_bar.git
  • Perforce仓库 //Foo/bar
  • Bitbucket/Stash仓库的master分支保持和Perforce的内容完全一致

首次创建并同步

  • $ git p4 clone //Foo/bar
  • $ git remote add origin https://stash.abc.com/scm/test/foo_bar.git
  • $ git push -u origin master
  • $ git checkout -b p4/master
  • $ git push -u origin p4/master

快速重建之前的仓库 (跳过git p4 clone,而直接syncFromOrigin + git p4 rebase)

  • $ git clone https://stash.abc.com/scm/test/foo_bar.git
  • $ git config –add git-p4.syncFromOrigin “true” <– 先读取remotes/origin/p4/master
  • $ git fetch origin
  • $ git checkout master
  • $ git p4 rebase <– 自动重建remotes/p4/master
  • $ git push –atomic origin master

维护remotes/origin/p4/master分支

  • 这个分支中的内容会从Bitbucket/Stash同步,而避免了从Perforce缓慢的提取过程
  • 定期从master分支更新内容到这个分支,可以使得尽可能多的内容从Bitbucket/Stash同步