简体   繁体   English

Jenkins Multibranch Pipeline 仅检查推送到 GIT 的新文件或更改文件

[英]Jenkins Multibranch Pipeline check only the new or changed files pushed to GIT

we are working with a tool where the elements are all written via .json.我们正在使用一种工具,其中所有元素都通过 .json 编写。

Currently my GIT folder structure looks like this:目前我的 GIT 文件夹结构如下所示:

elementsfolder -> element1.json, element2.json, element3.json元素文件夹-> element1.json、element2.json、element3.json

scriptsfolder -> eg transformation.py脚本文件夹 -> 例如transformation.py

testfolder -> run-element.sh测试文件夹-> run-element.sh

Jenkinsfile詹金斯文件

The problem I have now is that the .json files in folder elementsfolder should be tested with the bashscript from the testfolder and (if necessary) a script like transformations.py should be called.我现在遇到的问题是,应使用 testfolder 中的 bashscript 测试文件夹 elementsfolder 中的 .json 文件,并且(如有必要)应调用像 transformations.py 这样的脚本。 This also works so far, but all .json files are always tested (no matter if unchanged or not).到目前为止,这也有效,但始终测试所有 .json 文件(无论是否未更改)。 But this should not be the case.但事实并非如此。 Only the elements that are either changed or newly created should be tested.仅应测试已更改或新创建的元素。 We have at the end of the day over 6000 elements, accordingly, the test over all elements would be too costly.一天结束时,我们有超过 6000 个元素,因此,对所有元素的测试成本太高。 Can anyone help me with this?谁能帮我这个? In Jenkins the pipeline looks like this (I only post the stage build, because test and deploy are similar):在 Jenkins 中,管道看起来像这样(我只发布阶段构建,因为测试和部署是相似的):

 stages {
        stage('Build') {
            environment {
                CREDS = credentials('creds')
                ENDPOINT = 'automation-api'
            }
            steps {
                sh '''
                username=$USR
                password=$PSW

                # Login curl
                login=$(curl -k -s -H "Content-Type: application/json" -X POST -d \\{\\"username\\":\\"$username\\",\\"password\\":\\"$password\\"\\} "$ENDPOINT/session/login" )
                token=$(echo ${login##*token\\" : \\"} | cut -d '"' -f 1)

                # Build
                #1. Validation .json
                curl -k -s -H "Authorization: Bearer $token" -X POST -F "definitionsFile=@element/*.json" "$ENDPOINT/build"
                #2. Show Error
                curl --show-error --fail -k -s -H "Authorization: Bearer $token" -X POST -F "definitionsFile=@element/*.json" "$ENDPOINT/build"
                #3. Logout
                curl -k -s -H "Authorization: Bearer $token" -X POST "$ENDPOINT/session/logout"
                '''
            }
        }

If you are checking out from an SCM Jenkins will allow you to check the Changeset between the last build and the current build.如果您从 SCM 签出,Jenkins 将允许您检查上次构建和当前构建之间的变更集。 So basically you can first try to find the files that really changed and then use only those files.所以基本上你可以先尝试找到真正改变的文件,然后只使用那些文件。 Refer to the following example.请参考以下示例。 After getting the list of files that changed you can either move them to a new directory or delete the unchanged files from the elementsfolder .获得更改的文件列表后,您可以将它们移动到新目录或从elementsfolder中删除未更改的文件。 The following sample getFilesChanged will return a list of all the changed/added files in the repo.以下示例getFilesChanged将返回 repo 中所有更改/添加的文件的列表。 You may want to tweak this function to match your requirement.您可能需要调整此功能以符合您的要求。

pipeline {
  agent any
  stages { 
      stage('clone') {
              steps {
                    git branch: 'main', url: 'https://github.com/xxx/sample.git'
              }
      }
      stage('build') {
              steps {
                  script {
                        println(getFilesChanged())
                        // Do your cleanup here and then execute the SH block
                  }
              }
      }
  }
}

def getFilesChanged() {
    def filesList = []
    def changeLogSets = currentBuild.changeSets
                    for (int i = 0; i < changeLogSets.size(); i++) {
                        def entries = changeLogSets[i].items
                        for (int j = 0; j < entries.length; j++) {
                            def entry = entries[j]
                            def files = new ArrayList(entry.affectedFiles)
                                for (int k = 0; k < files.size(); k++) {
                                def file = files[k]
                                filesList.add(file.path)
                }
            }
        }
    return filesList
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM