简体   繁体   中英

jenkinsfile if statement not using wildcard statement

Im trying to work out the correct syntax based on the below if statement in a jenkinsfile, however its not passing the wildcard as expected and trying to match the syntax including the wildcard symbol:

stage('stage c') {
  steps {
    container('tf-jenkins') {
      script {
      sh """
      if [[ \$(gcloud container clusters list --format='value(name)' --project ${project_id} --filter=d-kcl-${short_region} ) = *d-kcl* ]]; then
        echo "cluster already exists, progressing to application deployment"
      else
        echo "no cluster found, a new cluster needs to be deployed"
      fi
      """
      }
    }

With the above the stage block, the following is returned:

+ gcloud container clusters list '--format=value(name)' --project prpject-id '--filter=d-kcl'
+ '[[' paas-gcp-d-kcl-euwe2 '=' '*paas-gcp-d-kcl*' ]]
+ echo 'no cluster found, a new cluster needs to be deployed'
no cluster found, a new cluster needs to be deployed

when running in a normal shell this works as expected but not in the jenkinsfile.

any help would be much appreciated

I would recommend using groovy code instead of a shell script.

stage('stage c') {
 steps {
   container('tf-jenkins') {
     script {
         def commandOutput = sh (
                                  script: "gcloud container clusters list --format='value(name)' --project ${project_id} --filter=d-kcl-${short_region}", 
                                  returnStdout: true,
                                  label: "Check if container exists"
                               ).trim()

        if(commandOutput.contains("d-kcl")){
            echo "cluster already exists, progressing to application deployment"
        } else{
           echo "no cluster found, a new cluster needs to be deployed"
        }
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM