场景概述:我想要一个具有这种结构的字典/地图:
它的关键是:一个目录名
它的值是:该dir的文件更改总数
我有一个有目录名的列表。ms_dir就是这个列表,我正在编写一个groovy script.inside it,我试图运行一个script.inside并将它赋值给一个值。我想使用shell执行以下操作:
中的几个目录进行的。
[ms_dir] = [aws_app1_service, aws_app2_service, aws_app3_service, aws_app4_service]
def changed_no = [:]
for (int i = 0; i< ms_dir.size();i++){
println "${ms_dir[i]}"
changed_no["${ms_dir[i]}"] = []
change_value = sh '''git diff --name-status HEAD^ HEAD ${ms_dir[i]} | cut -d'/' -f1 | sort | uniq -c | awk '{print $1}' '''
changed_no["ms_dir[i]"].add("$value")
println "${changed_no}"
}
println "total number of changes in each dir: ${changed_no}"
这里我面临的问题是,由于参数${ms_diri}和{print $1}的不同形式,groovy内部的shll无法工作。如果使用三重引号或双引号,则不会传递${ms_diri}值。下面是输出。如您所见,它给出的是每个目录的值,而不是循环目录的值。
2020-07-22 21:24:08.092 | . aws_app1_service
[Pipeline] sh
2020-07-22 21:24:08.375 | . + git diff --name-status 'HEAD^' HEAD
2020-07-22 21:24:08.375 | . + cut -d/ -f1
2020-07-22 21:24:08.375 | . + sort
2020-07-22 21:24:08.375 | . + uniq -c
2020-07-22 21:24:08.375 | . + awk '{print $1}'
2020-07-22 21:24:08.375 | . 2
2020-07-22 21:24:08.375 | . 4
2020-07-22 21:24:08.375 | . 1
2020-07-22 21:24:08.375 | . 2
当im使用单引号时,它会抛出DSL错误:
java.lang.NoSuchMethodError: No such DSL method ' -f1 | sort | uniq -c | awk ' found among steps [ArtifactoryGradleBuild, MavenDescriptorStep, addInteractivePromotion,
ansiColor, archive, artifactoryDistributeBuild, artifactoryDownload, artifactoryEditProps, artifactoryMavenBuild, artifactoryNpmInstall, artifactoryNpmPublish, artifactoryPromoteBuild,
artifactoryUpload, bat, build, catchError, checkout, collectEnv, conanAddRemote, conanAddUser,
deleteDir, deployArtifacts, dir, dockerFingerprintFrom, dockerFingerprintRun, dockerPullStep, dockerPushStep, echo, emailext, emailextrecipients, envVarsForTool, error, fileExists,
findBuildScans, findFiles, getArtifactoryServer, getContext, git, httpRequest,
initConanClient, input,
请让我知道如何整理这个外壳部分。有没有办法既包括${ms_diri}作为groovy变量,又包括{print $1}作为shell参数
发布于 2020-07-23 00:35:23
命令使用
发送到shell的大多数命令都可以在Groovy中使用。例如排序,独特和awk打印。它使您只需执行git命令即可。请参阅下面的示例,以获得完成此操作的一种方法。另一种方法是将git的结果导入Groovy脚本。
您提供的脚本没有为我编译,您的命令解释看起来是错误的。由于我不理解您的代码,我将向您展示如何完成任务。
列出给定子目录中跟踪文件的阶段性更改。
听起来你想要一张每个目录的阶段性跟踪文件的地图。如果这不是您所需要的,那么更改cmd
集合以匹配您要执行的命令。
它的实现方式如下:
def ms_dir = [
'aws_app1_service',
'aws_app2_service',
'aws_app3_service',
'aws_app4_service'
]
def cmd = ['git', 'status', '--untracked=no', '--short']
def changes = cmd.execute().text
def dirChangeCounts = [:]
ms_dir.each {
dirChangeCounts[it] = 0
}
changes.eachLine { change ->
def resultLineParts = change.split( "[ ]+|[/]" )
dirChanged = resultLineParts[1]
if ( ms_dir.contains( dirChanged ) ) {
dirChangeCounts[ dirChanged ]++
}
}
dirChangeCounts.each { dirName, count ->
suffix = count == 1 ? ' ' : 's'
printf '%3d staged change%s in %s\n', count, suffix, dirName
}
结果
在我的测试存储库上运行上面的脚本将提供以下输出:
1 staged change in aws_app1_service
4 staged changes in aws_app2_service
3 staged changes in aws_app3_service
9 staged changes in aws_app4_service
https://stackoverflow.com/questions/63039118
复制相似问题