0

I am able to replace substring in bash with below command. However, same command doesn't work in Jenkins pipeline shell script inside the step. Basically, in Jenkins ${GIT_BRANCH} returns 'origin/docker'. I want to use branch name 'docker' in URL while uploading deployment artifact to JFrog artifactory. However, I do not want to include 'origin/' string in the path.

Working Bash Command:

user@localhost MINGW64 ~
$ echo $GIT_BRANCH
origin/docker

user@localhost MINGW64 ~
$ GIT_BRANCH_NAME="${GIT_BRANCH/origin\/}"

user@localhost MINGW64 ~
$ echo $GIT_BRANCH_NAME
docker

Jenkins File Script:

GIT_BRANCH_NAME="${GIT_BRANCH/origin\/}"

The error I am seeing in Jenkins pipeline execution logs:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:    
WorkflowScript: 38: unexpected char: '\' @ line 38, column 53.
       ANCH_NAME="${GIT_BRANCH/origin\/}

Adding working script block. Now the env variable 'GIT_BRANCH_NAME' returns just 'docker' instead of 'origin/docker'. Also, added few more statements to handle various types of branches like 'bugfix/feature/hotfix/release' etc.

script {
env.GIT_BRANCH_NAME=""
String gitBranchName = env.GIT_BRANCH.replaceFirst('^origin/', '')
env.GIT_BRANCH_NAME = gitBranchName.replaceFirst('^bugfix/', 'bugfix-')
env.GIT_BRANCH_NAME = gitBranchName.replaceFirst('^feature/', 'feature-')
env.GIT_BRANCH_NAME = gitBranchName.replaceFirst('^hotfix/', 'hotfix-')
env.GIT_BRANCH_NAME = gitBranchName.replaceFirst('^release/', 'release-')
}
sh '''
    mv ${GIT_COMMIT}.zip app_${GIT_BRANCH_NAME}_${GIT_COMMIT}.zip
    curl -u JFROG_USR:JFROG_PSW -T app_${GIT_BRANCH_NAME}_${GIT_COMMIT}.zip "some-jfrog-artifctory-url/${BUILD_NUMBER}/app_${GIT_BRANCH_NAME}_${GIT_COMMIT}.zip;git.branch.name=${GIT_BRANCH};git.commit.id=${GIT_COMMIT};jenkins.build.url=${BUILD_URL}"
    rm -rf app_${GIT_BRANCH_NAME}_${GIT_COMMIT}.zip
'''
Ranjeet
  • 151
  • 6
  • 18
  • Does this answer your question? https://stackoverflow.com/a/77004560/2627018 – ycr Aug 30 '23 at 11:42
  • Please show a wider context of the code as presented in the pipeline. Is this a `sh` block? is it wrapped in \`\`\` characters, or one of the other methods mentioned [here](https://stackoverflow.com/questions/36547680/how-do-i-get-the-output-of-a-shell-command-executed-using-into-a-variable-from-j)? Is it because `ANCH_NAME="${GIT_BRANCH/origin\/}` doesn't close the quotes? Are you sure it's actually using a `bash`-like shell that understands that syntax? (Not meaning to rapid fire questions to sound like I'm interrogating you - just hoping one will trigger a useful "aha" moment.) – Paul Hodges Aug 30 '23 at 14:52

1 Answers1

1

In Jenkins file in script block, You've groovy language, so you are able to use groovy semantic.

So if you are in the script block context, you can remove prefix and set up new environment variable:

String gitBranchName = env.GIT_BRANCH.replaceFirst('^origin/', '')
withEnv(["GIT_BRANCH_NAME=${gitBranchName}"]) {
   ...                
}
Olek
  • 26
  • 1
  • Updated the script block as mentioned in the updated part of the question and it started working. – Ranjeet Aug 31 '23 at 06:07