代码之家  ›  专栏  ›  技术社区  ›  Amrit Gill

一个存储库中有多个jenkinsfile

  •  45
  • Amrit Gill  · 技术社区  · 8 年前

    我们在Github存储库中有一个项目,其中包含多个Jenkins文件:

    my-project
      app
        Jenkinsfile
      lib1
        Jenkinsfile
      lib2
        Jenkinsfile
    

    我们创建了3个Jenkins管道,每个管道引用一个Jenkins文件。 enter image description here

    问题:“lib2”中有新提交时,如何避免触发“app”和“lib1”管道?我们不希望每次提交时都运行N个作业。

    我看到这个问题在 https://issues.jenkins-ci.org/browse/JENKINS-43749 ,但我还没有找到解决方案。

    4 回复  |  直到 8 年前
        1
  •  19
  •   papanito    5 年前

    最近更新:

    我后来使用以下代码段修复了此问题: 如果看到命令dir('servicelayer'),则使用该命令移动到目录中,执行git命令以查找提交和提升标志之间的差异。这样,我在一个存储库中管理了3个Jenkins文件。

    stage('Validation') {
    steps {
            //Moving in to the directory to execute the commands
            dir('servicelayer') {
                script {
                    //Using the git command to check the difference between previous successful commit. ${GIT_PREVIOUS_SUCCESSFUL_COMMIT} is an environment variable comes with GIT Jenkins plugin
                    //There is a drawback though, if it is the first time you are running this job, this variable is not available and fails the build
                    //For the first time i had to use ${env.GIT_COMMIT} itself at both places to pass the build. A hack but worth it for future builds.
    
                    def strCount = sh(returnStdout: true, script: "git diff --name-only ${env.GIT_COMMIT} ${GIT_PREVIOUS_SUCCESSFUL_COMMIT} | grep servicelayer | wc -l").trim()
                    if(strCount=="0") {
                        echo "Skipping build no files updated"
                        CONTINUE_BUILD = false
                    } else {
                        echo "Changes found in the servicelayer module"
                    }
                }
            }
        }
    }
    

    旧答案:

    您可以通过两种方式完成:

    (a) 通过添加“其他行为”来配置构建作业->“轮询忽略特定区域中的提交” 此行为将允许您添加您不想轮询以触发构建作业的“白名单区域”或“黑名单区域”。

    (b) 编写自定义shell脚本以验证每次提交更改的文件并验证位置。然后可以将这个shell脚本添加到Jenkinsfile中,无论是声明性的还是脚本化的,都可以相应地调整行为。

    我将推荐选项a),因为它更易于配置和维护。希望这有帮助。

    Additional Behaviour

        2
  •  13
  •   iheanyi    7 年前

    我建议使用多分支管道插件,然后指定相应jenkinsfile的路径。因此,您将有三个管道作业,一个用于app、lib1和lib2。

    enter image description here

        3
  •  3
  •   ceztko Jeff Davis    6 年前

    我实现了在一个存储库中拥有多个管道(Jenkinsfile)。这不是黑客行为,但也不是微不足道的,需要更新Jenkins的最新版本 Lockable Resources plugin 并且可能需要在构建过程中执行其他步骤。

    • 创建仅“源代码管理”项目。在詹金斯 术语它可以是一个只关心 使用SCM部分和触发的 已配置(示例名称“MyProjectSCM”)。就我而言,我有一个git 具有轮询计划的存储库。确保“必要时执行并发生成”标志为 不 已检查;

    • 在项目存储库中提交一个或多个Jenkins管道,例如“JenkinsfileBuild”、“JenkinsfileRelease”。确保管道:

      1. 有 skipDefaultCheckout 在 options 部分

      2. 配置了SCM项目上的触发器(“MyProjectSCM”);

      3. 在生成期间锁定SCM项目。

         pipeline {
             agent any
        
             options {
                 disableConcurrentBuilds()   
                 skipDefaultCheckout()
             }
        
             triggers {
                 upstream(upstreamProjects: 'MyProjectSCM')
             }
        
             stages {
                 stage('Build') {
                     lock('MyProjectSCM') {
                         steps {
                             echo 'Build within MyProjectSCM lock...'
                         }
                     }
                 }
             }
         }
        
    • 创建管道项目,每个您在存储库中提交的Jenkinsfile创建一个管道项目。管道应该配置“来自SCM的管道脚本”(在本例中,管道可以命名为“MyProject\u Build”,配置为“JenkinsfileBuild”),并选中“轻量级签出”标志(实际上是默认设置):

    enter image description here

    这个 skipDefaultCheckout “MyProject\u Build”管道中的指令将阻止签出该工作区中的存储库,“轻量级签出”标志本身奇怪的是,它不足以阻止签出。由于“MyProject\u Build”工作区实际上是空的,因此您必须通过环境变量或相对路径在构建脚本中引用仅SCM项目目录。这个 lock 指令很有用,因此在实际构建项目时不会发生新的提交。

        4
  •  2
  •   Sunil    4 年前

    我认为最简单的使用方法 multibranch pipeline 具有 Multibranch build strategy extension 插件 https://plugins.jenkins.io/multibranch-build-strategy-extension/

    它提供了一种仅在特定文件夹/区域发生更改时才触发生成的方法