0

This is the output I got:

"Successfully created scratch org: oopopoooppooop, username: test+color@example.com"

when I run the following script:

echo "1. Creating Scratch Org"
def orgStatus = bat returnStdout: true, script: "sfdx force:org:create --definitionfile ${PROJECT_SCRATCH_PATH} --durationdays 30 --setalias ${SCRATCH_ORG_NAME} -v DevHub "

if (!orgStatus.contains("Successfully created scratch org")) {
    error "Scratch Org creation failed"
} else {
    echo orgStatus
}                           

Now I need to extract scratch org ID and username from the output separately and store it.

zett42
  • 25,437
  • 3
  • 35
  • 72
  • Does this answer your question? [How to restart Jenkins manually?](https://stackoverflow.com/questions/8072700/how-to-restart-jenkins-manually) – Henry Henrinson Feb 18 '20 at 23:36

1 Answers1

0

You can use a regular expression:

def regEx = 'Successfully created scratch org: (.*?), username: (.*)'
def match = orgStatus =~ regEx

if( match ) {
    println "ID: " + match[0][1]
    println "username: " + match[0][2]
}

Here the operator =~ applies the regular expression to the input and the result is stored in match.

Live Demo

zett42
  • 25,437
  • 3
  • 35
  • 72
  • @VikramReddy I don't understand, username is available in `match[0][2]`. – zett42 Feb 18 '20 at 21:22
  • @VikramReddy The question mark is required to extract only the minimum number of characters up to the comma. Otherwise we would extract too much, when username contains comma. Username is located at end of string, so we don't need question mark there. – zett42 Feb 18 '20 at 21:25
  • @VikramReddy You can pass it for a parameter, it depends on the command. – zett42 Feb 18 '20 at 21:38
  • If I want to use in the following command sfdx force:org:create match[0][2] , This is the way or should I add "$" sign before – Vikram Reddy Feb 18 '20 at 21:40
  • @VikramReddy `${match[0][2]}`. Personally I would store username in a variable to make code more readable: `def username = match[0][2]`, then `sfdx force:org:create $username` – zett42 Feb 18 '20 at 21:54
  • @VikramReddy This is not much different from previous problem. You may have issues with the square brackets, which have to be escaped with a backslash in the RegEx: `\[`. If you can't find a solution, you may ask a new question, presenting what you have tried so far. But this is more a Groovy / RegEx question, it doesn't relate to Jenkins at all. – zett42 Feb 21 '20 at 17:38