Home > OS >  Jenkins remove special characters from environment variable
Jenkins remove special characters from environment variable

Time:10-06

I have the following environment variable fixed in the Declarative Pipeline of Jenkins.

environment {
    branch = "${GIT_BRANCH}"
}

Unfortunately my branch consists partly of special characters as example

feature/asldasd-asdasd

How can I remove these special characters like / in the declarative pipeline and then use the variable normally ?

        steps {
            echo '%branch%'
     }

CodePudding user response:

You can simply replace the character before using it. Check the example below.

pipeline {
  agent any
  environment {
    branch = "feature/asldasd-asdasd"
    }
  stages {
    stage('Example') {
      steps {
        script {
            branchName = env.branch.replaceAll('/', '')
            echo "$branchName"
            
        }
      }
    }
  }
}
  • Related