How to use reference with a condition?

I am using below custom block in my steps of job.

!reference [.configure_gcp_cli, script]

It is working.

I want to run this only if a variable is set, so added below code as part of script in the job.


    - |
      if [[ $CLOUD_PLATFORM == "gcp" ]]
      then
        echo "google cloud cli configuration"
        !reference [.configure_gcp_cli, script]
      fi

But it is failing and giving the error as.


/bin/bash: line 213: !reference: command not found

How to achieve this conditional logic?

References are adding in the array of lines in the script, so each would start with "- ". You’d need to add some conditional logic to configure_gcp_cli.

- |
  if [[ $CLOUD_PLATFORM == "gcp" ]]
  then
    skip_gcloud_setup=true
  fi
- !reference [.configure_gcp_cli, script]

and

.configure_gcp_cli:
  script:
    - |
      if [[ -z skip_gcloud_setup ]]
      then
        <all logic here>
      fi

which compiles to

- |
  if [[ $CLOUD_PLATFORM == "gcp" ]]
  then
    skip_gcloud_setup=true
  fi
- |
  if [[ -z skip_gcloud_setup ]]
  then
    <all logic here>
  fi