在詹金斯管道上提取字符串的一部分 [英] Extracting part of a string on jenkins pipeline

查看:1623
本文介绍了在詹金斯管道上提取字符串的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在管道脚本中的语法有麻烦.

I am having some trouble with the syntax in my pipeline script.

我正在尝试捕获最后一个斜杠"/"之后和最后一个句号"之前的所有内容.在此字符串git@github.com:project/access-server-pd.git(access-server-pd)

I am trying to capture everything after the last forward slash "/" and before the last period "." in this string git@github.com:project/access-server-pd.git (access-server-pd)

此处(如下)是我的工作方式想要设置

Here (below) is how I would like to set it up

MYVAR="git@github.com:project/access-server-pd.git" 

NAME=${MYVAR%.*}  # retain the part before the colon
NAME=${NAME##*/}  # retain the part after the last slash
echo $NAME

我目前已在管道脚本上设置了三引号:

I have it current set up with triple quotes on the pipeline script:

  stage('Git Clone') {
  MYVAR="$GIT_REPO"
  echo "$MYVAR"
  NAME="""${MYVAR%.*}"""
  echo "$NAME"

但是我收到一个关于"的意外令牌.错误.我该怎么写才能使它正常工作?

But I am receiving an unexpected token on "." error. How might I write this so that I can get this to work?

更新:此命令可以解决问题:

UPDATE: This command does the trick:

echo "git@github.com:project/access-server-pd.git" | sed 's#.*/\([^.]*\).*#\1#'

现在,我只需要找到合适的语法来创建一个变量来存储该值.

Now I just need to find the proper syntax to create a variable to store that value.

推荐答案

在这种情况下,看起来像在String上使用一些Groovy/Java方法可以提取零件.

In this case, it looks like using a few Groovy/Java methods on the String can extract the parts.

final beforeColon = url.substring(0, url.indexOf(':'))  // git@github.com
final afterLastSlash = url.substring(url.lastIndexOf('/') + 1, url.length()) // project/access-server-pd.git

这使用了几种不同的方法:

This uses a few different methods:

  • public int String.indexOf(String str, int fromIndex)
  • public String String.substring(int beginIndex, int endIndex)
  • public int String.length()
  • public int String.lastIndexOf(String str)

您确实需要注意管道中使用的代码.如果是沙盒,它将在受保护的域中运行,在该域中,所有调用都经过安全检查.例如,白名单"rel =" nofollow noreferrer> 脚本安全插件 将上面使用的所有调用列入白名单(例如,

You do need to be careful about the code you use in your pipeline. If it is sandboxed it will run in a protected domain where every invocation is security checked. For example, the whitelist in the Script Security Plugin whitelists all of the calls used above (for example, method java.lang.String lastIndexOf java.lang.String).

在管道代码中执行String操纵是完全合理的,因为您可以做出决定并根据它更改编排.

Performing String manipulation in your pipeline code is perfectly reasonable as you might make decisions and change your orchestration based on it.

这篇关于在詹金斯管道上提取字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆