如何打开文件并在Groovy脚本中编辑其内容
我的詹金斯(Jenkins)作业空间中有一个makefile,我想在运行使用此makefile的下一个脚本之前编辑某些部分,然后保存它。
我要切掉的部分是 -
PUMP_MARKER:=,cpp
ifneq (,$(findstring $(PUMP_MARKER), $(DISTCC_HOSTS)))
PUMP:=pump
else
PUMP:=
endif
我正在寻找可以在我的groovy脚本中放置的shell命令,这些命令将打开该文件并删除上述部分,保存它然后继续进行。
我对壳不好,所以我无法自己弄清楚这一点。
这是我的Groovy脚本的外观 -
stage('Build'){
dir ("$WORKSPACE/$SVN_TAG") {
Here- I want to insert some shell command to edit that file out
}
}
可以提出一些实施此功能的想法。
注1- 我知道我只能vi vi并手动执行此操作,但我想为多个工作做到这一点,我真的在寻找可以粘贴到每个Groovy脚本和的东西自动这样做。
注2- 它不一定是外壳,只要我能在我的groovy脚本中运行它,并且可以完成工作,就可以是任何语言。
I have a Makefile in my Jenkins job's workspace, that I want to edit out certain parts from and then save it, before running next part of the script that uses this Makefile.
the part that I want to cut out is this-
PUMP_MARKER:=,cpp
ifneq (,$(findstring $(PUMP_MARKER), $(DISTCC_HOSTS)))
PUMP:=pump
else
PUMP:=
endif
I am looking for some shell command that I could put in my groovy script, that will open up this file and remove this above mentioned part, save it and then move on.
I'm not good with shell so I cant possible figure this out on my own.
Here is how my groovy script looks like-
stage('Build'){
dir ("$WORKSPACE/$SVN_TAG") {
Here- I want to insert some shell command to edit that file out
}
}
Could any please suggest some ideas to implement this.
Note 1- I know I could just vi that file and manually do this but I want to do this for multiple jobs and I'm genuinely looking for something that I can just paste in my every groovy script and automatically do this.
Note 2- It doesn't have to be shell, it can be any language as long as I can run it inside my groovy script and it gets the job done.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够通过使用诸如此之类的修补工具来解决此问题 -
脚本调用要修改的文件上的补丁程序(在这种情况下为foo.txt)。然后,补丁工具还可以在标准输入上使用它应应用的差异。这是在以下代码中使用Shell的“此处文档”机制实现的,请参见<<其次是引用的标识符端。必须引用结尾,以确保随后的差异文本不受扩展的影响。
I was able to solve this by using the patch tool like this-
The script calls patch on the file to be modified (in this case foo.txt). The patch tool then also gets on the standard input the diff it shall apply. This is in the code below achieved using the "here document" mechanism from the shell, see the << followed by the quoted identifier END. Quoting END is necessary to ensure the subsequent diff text is not subject to expansions.