首页 > 解决方案 > 如何通过shell(Jenkinsfile)中变量的给定模式获取单词?

问题描述

目前我面临一个问题,即如何从我的 Jenkinsfile 中的变量(Shell)中提取一些测试类的名称。

场景:每次在 Bitbucket 中创建拉取请求时,都会在 Jenkins 中激活流水线类型的作业,我可以在其中访问 Jenkinsfile 中名为“description”的变量。

例如,描述变量的输入可以包含以下内容:


US-2371 此处的任何描述 测试类:testclass1_tst,testclass_test 此处的任何其他描述。

预期输出:仅将以下内容保存在另一个变量中:testclass1_tst,testclass_test

注意:我需要用“,”分隔的测试类的名称,模式是那些以 tst、test、Test 结尾的测试类。

提前致谢。

参考:

Jenkinsfile - 变量

标签: shelljenkinssedgrepjenkins-pipeline

解决方案


由于测试类:将出现在所有描述中,我们可以查找它并解析出类。

echo $description | sed -r 's/(.*Test Classes: )(\w.+)([[:space:]])(.*)/\2/

这会让你
testclass1_tst,testclass_test


推荐阅读