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

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

问题描述

我的管道脚本中的语法有些问题.

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:

您确实需要小心您在管道中使用的代码.如果它是沙盒的,它将在一个受保护的域中运行,每个调用都经过安全检查.例如,脚本安全中的" rel="noreferrer">白名单插件 将上面使用的所有调用列入白名单(例如,方法 java.lang.String lastIndexOf java.lang.String).

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天全站免登陆