当git repo中的子目录被更改时,在Jenkins中构建

时间:2016-01-12 18:52:51

标签: git maven jenkins continuous-integration

我有一个包含n个目录的仓库:

.
├── .git
├── project_1
├── project_2
└── project_3

在jenkins中我想为每个项目建立3个版本(所以我使用了稀疏检查)。 但是,如果我只更改了project_1,我只想构建 project_1。如何配置jenkins来做到这一点?

2 个答案:

答案 0 :(得分:0)

如果它在GUN / Linux机器上运行,你可以在jenkins中运行类似这样的脚本:

PROJECTX=$(git diff-tree --no-commit-id --name-only -r HEAD | cut -d"/" -f1 | sort | uniq) && \
if [ "$PROJECTX" == "project_1" ]; then
  # build project_1
elif [ "$PROJECTX" == "project_2" ]; then
  # build project_2
elif [ "$PROJECTX" == "project_3" ]; then
  # build project_3
fi

答案 1 :(得分:0)

如果您使用Stash作为您的git服务器,您可以使用Groovy来测试您提交的路径以运行特定模块。

在你的工作中选择"为跑步准备一个环境"并将下面的脚本粘贴到"评估的Groovy脚本"。 当脚本完成后,您可以使用环境布尔值来测试每个模块中是否有更改:

$ {project_1_CHANGED} == true - >运行逻辑

$ {project_2_CHANGED} == true - >运行逻辑

$ {project_3_CHANGED} == true - >运行逻辑

如果项目不相关,我还建议使用Multijob插件。

import groovy.json.JsonSlurper
import hudson.model.*

println "="*90
println "="*35 + "   Script Started   " + "="*35
println "="*90
def project_1_PATH = """project_1/"""
def project_2_PATH = """project_2/"""
def project_3_PATH = """project_3/"""

def project_1_CHANGED = false
def project_2_CHANGED = false
def project_3_CHANGED = false


def slurper = new JsonSlurper()


// get current thread / Executor and current build
def thr = Thread.currentThread()
def build = thr?.executable

//Get from Env
def stashServer= build.parent.builds[0].properties.get("envVars").find {key, value -> key == 'Stash_Server_HTTP'}

//Get from Job Params
def branch= "Branch"
def resolver = build.buildVariableResolver
def latest_branch = resolver.resolve(branch)


println "param ${branch} value : ${latest_branch}"
println "Stash Server : ${stashServer.value}" 


// get latest push user's email address
new_command = """curl -u User:Password -X GET -H "Content-Type: application/json" ${stashServer.value}/rest/api/1.0/projects/${repository}/compare/changes?from=${latest_branch}&to=master&limit=5000"""
proc = new_command.execute()
println "Sleeping for 10 seconds..."
sleep(10000)
// proc.waitFor()
println "Finished getting changes"
def result_changes = proc.in.text
def parse_changes_string = result_changes.substring(result_changes.indexOf("{"))
def latest_changes_json = slurper.parseText(parse_changes_string)
def change_list = latest_changes_json.values
def changed_files_list = []

println "\nThese are the files that were changed: \n"
for (changedFile in change_list)
{
        changed_files_list.add(changedFile.path.toString)
        println changedFile.path.toString
}


for(oneFilePath in changed_files_list){
     if(oneFilePath.contains(project_1_PATH)){
                project_1_CHANGED = true }
else if(oneFilePath.contains(project_2_PATH )){
                project_2= true }
else if(oneFilePath.contains(project_3_PATH)){
                project_3_CHANGED = true}
}

def result_map = [:]
result_map.put('project_1_CHANGED',project_1_CHANGED)
result_map.put('project_2_CHANGED',project_2_CHANGED)
result_map.put('project_3_CHANGED',project_3_CHANGED)

println "="*90
println "="*35 + "   Script Ends   " + "="*35
println "="*90

return result_map