12

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

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.

Lgalan90
  • 595
  • 2
  • 12
  • 31
  • 1
    It looks like you are mixing shell code with Groovy code. – mkobit Apr 26 '18 at 21:38
  • @mkobit Perhaps, in your experience, how would you extract test from a url in Jenkins pipeline? – Lgalan90 Apr 30 '18 at 18:23
  • 1
    For me, I generally try to do some parts of this in the `Jenkinsfile` (which means using the Jenkins subset of supported Groovy features). So in this example, `final beforeColon = url.substring(0, url.indexOf(':'))` stores the result into a `final` variable `beforeColon`. Groovy doesn't need to declare types, but the result is a `String` with the value of `git@github.com`. `final afterLastSlash = url.substring(url.lastIndexOf('/') + 1, url.length())` would get everything after the last `/`. – mkobit Apr 30 '18 at 18:53
  • This is excellent, would you be able to confirm that this is the correct way to do it in my case using variable gitRepo? url="$GIT_REPO" final beforeColon = url.substring(0, url.indexOf('.git')) url="$beforeColon" final gitRepo = url.substring(url.lastIndexOf('/') + 1, url.length()) – Lgalan90 Apr 30 '18 at 20:34
  • That looks ok to me, but you should definitely test it. One point of preference to me is to (generally) always use variables like `final gitRepo`, `def gitRepo`, `String gitRepo`, or even `final String gitRepo` instead of just `gitRepo`. If you just use `gitRepo`, that will make it a globally scoped variable to the entire script which can be difficult to debug sometimes. There are also some libraries/tools to test your `Jenkinsfile` if you want to go that direction. In most cases with Jenkins pipelines, it is just a "try until it works" strategy. – mkobit Apr 30 '18 at 20:59
  • :) You've been extremely helpful! would you mind posting your response, so that I may accept it as an answer? – Lgalan90 Apr 30 '18 at 21:20

1 Answers1

19

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:

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).

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

mkobit
  • 43,979
  • 12
  • 156
  • 150