如何配置Jenkins多分支管道以构建为子模块?

时间:2017-08-11 16:26:14

标签: jenkins git-submodules

我有一个用子模块组织的项目kuma

  • kuma
    • Jenkinsfile(配置为测试kuma)
    • locales
    • kumascript
      • Jenkinsfile(配置为测试kumascript)
    • 一堆其他文件

我想在Jenkins中配置一个多分支管道来监视kumascript repo上的分支,以便:

  1. 查看kuma的主分支
  2. 将语言环境更新为主分支中的提交(常规git submodule update --init
  3. 将kumascript子模块更新为分支以进行测试
  4. 在kumascript分支中运行Jenkins文件
  5. 这可能吗?有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

这对我有用。

首先,在结账前从提交中读取Jekinsfile,因此很容易使用kumascript子模块中的那个,并且更难(不可能?)从中读取它一个不同的回购。

在使用Git插件3.4.1的Jenkins 2.68中,我设置了一个多分支管道。一个来源是Git,指向kumascript存储库:

Jenkins Git setup

"发现分支机构"在存储库中查找分支并为它们启动构建。

"擦除存储库并强制克隆"解决了jgit在检出子模块之前没有获取子模块存储库的问题,因此目标提交不可用。它会导致Jenkins日志中出现如下错误:

> git fetch --no-tags --progress https://github.com/mdn/kumascript +refs/heads/*:refs/remotes/origin/*
Checking out Revision 998d9e539127805742634ef1c850221cf04ca2c7 (build-with-locales-1340342)
org.eclipse.jgit.errors.MissingObjectException: Missing unknown 998d9e539127805742634ef1c850221cf04ca2c7
at org.eclipse.jgit.internal.storage.file.WindowCursor.open(WindowCursor.java:158)
at org.eclipse.jgit.lib.ObjectReader.open(ObjectReader.java:227)
at org.eclipse.jgit.revwalk.RevWalk.parseAny(RevWalk.java:859)
at org.eclipse.jgit.revwalk.RevWalk.parseCommit(RevWalk.java:772)

此问题似乎在https://issues.jenkins-ci.org/browse/JENKINS-45729中报告,并在Git客户端插件2.5.0中得到修复。

清除repo似乎强制完全获取,在父项目中安装时可能是必要的。

Jenkins现在配置为为存储库中的每个分支创建一个构建。要将其作为子模块进行检查,需要在Jenkinsfile中手动检出父项目。我使用了Jenkin" Pipeline Syntax"帮助构建命令的工具

Jenkins pipeline syntax tool

经过一些格式化后,我的Jenkinsfile

    stage("Prepare") {
        // Checkout Kuma project's master branch
        checkout([$class: 'GitSCM',
                  userRemoteConfigs: [[url: 'https://github.com/mozilla/kuma']],
                  branches: [[name: 'refs/heads/master']],
                  extensions: [[$class: 'SubmoduleOption',
                                disableSubmodules: false,
                                parentCredentials: false,
                                recursiveSubmodules: true,
                                reference: '',
                                trackingSubmodules: false]],
                                doGenerateSubmoduleConfigurations: false,
                  submoduleCfg: []
                 ])

        // Checkout KumaScript in subfolder
        dir('kumascript') {
          checkout scm
        }
    }

这将检出kuma项目及其子模块,然后使用" vanilla" checkout检出所请求的分支,但是在子模块目录中:

从那时起,如果我想在kuma repo中运行一个命令,我就运行它:

    stage('Build') {
      sh 'make build-kumascript VERSION=latest'
    }

如果我想在kumascript子模块中运行它,我将其包装在dir中:

    stage('Lint') {
      dir('kumascript') {
        sh 'make lint VERSION=latest'
        sh 'make lint-macros VERSION=latest'
      }
    }