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
'''