0

In my jenkins pipeline I check a parameter EnvironmentName. If this is not valid and another condition is true, I want to set this to a default value like "myname"

In all it is not a good way to change Parameter so I set a global var BUILDPATTERN and want to fill this with namespacepattern out of a shell script. But it looks like the shell script to not return any value to Stdout. In the tutorials the right command is echo I understand.

parameters{
      string(name: 'EnvironmentName', defaultValue: '', description: '')          
}
environment{
  BUILDPATTERN = "${EnvironmentName}"
}
stages{
    stage('Prepare'){
      environment {
        namespacepattern = sh(returnStdout: true, script: '''
          export LC_COLLATE=C #To make regex case sensitiv
          DN1123REGEX='^[a-z0-9]([-a-z0-9]*[a-z0-9])$\'
          if [[ ! "${EnvironmentName}" =~ ${DN1123REGEX} ]]
          then
            if [[ "${Cleanup_Instance}" == "true" ]]     
            then
              echo "myname"
            else
              println "EnvironmentName is not in an valid format"
              exit 1
            fi
          else
            echo "${EnvironmentName}"
          fi          
        ''').trim()
        BUILDPATTERN=namespacepattern
        }
   }
  stage('Build'){
    steps {
        script {
            echo "${BUILDPATTERN}"
        }
  }
}

In all BUILDPATTERN is empty later. Anyone can explain what my problem is here?

IFThenElse
  • 131
  • 1
  • 10
  • What's `println`? You [need an explicit shebang](https://stackoverflow.com/a/38339814/874188) if you use Bash features in `sh` invocations. See also [Difference between sh and bash](https://stackoverflow.com/questions/5725296/difference-between-sh-and-bash) (notably `[[` and `=~` are both Bash extensions). – tripleee Jul 07 '21 at 07:20
  • Groovy string interpolation (`${}`} will not work inside regular strings (single quoted) so the parameters will not be evaluated, use `"""` instead of `'''` to solve it. – Noam Helmer Jul 07 '21 at 07:29

0 Answers0