本文介绍了如何在Jenkins多分支管道作业中仅构建一个目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个叫做multibranch-test的github存储库,带有两个子目录Project1,Project2.

I have a github repo called multibranch-test with two sub-directories Project1, Project2.

PS C:\Repos\multibranch-test> tree .Folder PATH listing for volume WindowsVolume serial number is 2085-6D3DC:\REPOS\MULTIBRANCH-TEST├───Project1└───Project2

PS C:\Repos\multibranch-test> tree .Folder PATH listing for volume WindowsVolume serial number is 2085-6D3DC:\REPOS\MULTIBRANCH-TEST├───Project1└───Project2

每个子目录都有一个Jenkins文件和该项目的代码.

Each sub-directory has a Jenkinsfile and the code for that project.

在詹金斯,我有两个多分支管道工作-一个用于Project1,一个用于Project2.在Project1的配置中,如果在Project2的子目录中推送了提交,则我不希望通过推送通知或轮询来构建Project1.

In Jenkins I have two multibranch pipeline jobs - one for Project1 and one for Project2. In the configuration for Project1 I don't want a push notification or polling to build Project1 if a commit was pushed in sub-directory for Project2.

因此在Project1中,我配置了其他行为:

So in Project1 I have configured Additional Behaviours:

  • 高级克隆行为:选中了浅克隆
  • 稀疏签出路径设置为Project1#
  • 轮询会忽略某些路径中的提交
    • 包含的区域: Project1/*
    • 排除的地区: *
    • Advanced clone behaviours: Shallow clone is checked
    • Sparse checkout path is set to Project1#
    • Polling ignores commits in certain paths
      • Included Regions: Project1/*
      • Excluded Regions: *

      发生的事情是,如果我将提交推送到子目录Project2中的主目录,则会同时构建 Project1和Project2作业.我只希望Project2构建.有人可以指出我做错了什么吗?

      What is happening is if I push a commit to master in sub-directory Project2, both Project1 and Project2 jobs get built. I only want Project2 to build. Can someone point out what I'm doing wrong?

      两个项目的Jenkinsfiles相似,如下所示:

      Jenkinsfiles for both Projects are similar and look like:

      #!groovy
      node  {
          stage ('checkout') {
              checkout scm
          }
      
          stage ('build') {
              dir ('Project1') {
                  bat 'powershell -Command gci'
                  bat 'powershell -Command gci env:'
                  bat 'powershell -File .\\Project1.ps1'
              }
          }
      

      推荐答案

      这对我们来说是一个很大的麻烦,但是我们能够通过一些解决方法来解决它.

      This has been a big hassle for us, but we were able to solve it with some workarounds.

      我们有一个由GitHub提交钩子触发的Jenkins主工作.它找出自上次提交以来发生了什么变化,然后触发其他 service-specific Jenkins作业.

      We have a master Jenkins job that's triggered by a GitHub commit hook. It figures out what changed since the last commit, and then triggers other service-specific Jenkins jobs.

      我们还使用了一些其他约定(例如,服务,目录和Jenkins作业的命名约定),但希望可以对某些人有所帮助.

      We have some other conventions we're using (like naming conventions for services, directories, and Jenkins jobs) that aren't specified here, but hopefully this will help someone out.

      以下是解决方案中每个组件的细分:

      Here's a breakdown of each component in the solution:

      1. monorepo中每个服务的Jenkins作业和相应的Jenkinsfiles.

      C:\REPOS\MULTIBRANCH-TEST\Project1\Jenkinsfile(此处为构建逻辑)C:\REPOS\MULTIBRANCH-TEST\Project2\Jenkinsfile(您的构建逻辑在这里)

      C:\REPOS\MULTIBRANCH-TEST\Project1\Jenkinsfile (your build logic here)C:\REPOS\MULTIBRANCH-TEST\Project2\Jenkinsfile (your build logic here)

      1. shell脚本,获取自上次提交以来更改的列表(改编自此博客文章).

      C:\REPOS\MULTIBRANCH-TEST\change-sets.sh

          #!/usr/bin/env bash
      
          changeSets=(`git diff-tree --name-status HEAD`)
          for(( i=0; i<${#changeSets[@]}; i++))
          do
            if [ ${changeSets[$i]} == "M" ]
            then
              echo ${changeSets[$i+1]}
            fi
          done
      
      1. 基于GitHub提交钩子的Jenkins作业

      C:\REPOS\MULTIBRANCH-TEST\Jenkinsfile

          #!/usr/bin/env groovy
      
          pipeline {
              agent any
      
              stages {
      
                  stage('Define Services to Build') {
                      steps {
                          script {
      
                              def SERVICES_TO_BUILD = sh script:"./change-sets.sh", returnStdout: true
                              SERVICES_TO_BUILD.split("\n").each {
                                  echo "Triggering build for ${it}"
                                  try {
                                      build job: "${it}", propagate: false, wait: false
                                  } catch (ex) {
                                      echo "Failed to trigger build for ${it}: ${ex.message}"
                                  }
      
                              }
                          }
                      }
                  }
              }
          }
      

      这篇关于如何在Jenkins多分支管道作业中仅构建一个目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 23:00