如何在CloudBuild中进行字符串比较工作?

问题描述

我在GCP CloudBuild 步骤中进行了简单的字符串测试,但是它永远无法正常工作。步骤看起来像这样

steps:

- id: 'branch name'
  name: 'alpine'
  entrypoint: 'sh'  
  args: 
  - '-c'
  - | 
      export ENV=$BRANCH_NAME
      if [ $ENV = "master" ]; then
         export ENV="test-dev"
      fi
      echo "***********************"
      echo "$BRANCH_NAME"
      echo "$ENV"
      echo "***********************"     

CloudBuild 始终将其报告为sh: master: unkNown operand。显然,这是字面意义。 我将相同的代码放入一个小的sh脚本中,只要我为BRANCH_NAME设置一个值,它就可以正常运行。 CloudBuild 肯定为BRANCH_NAME提供了一个值,并且在echo "$BRANCH_NAME"始终为空的情况下显示echo "$ENV"中。

有没有办法使此字符串比较有效?

解决方法

使用Linux env var and not substitution variables (or predefined variables),you have to escape the $ with another one

steps:

- id: 'branch name'
  name: 'alpine'
  entrypoint: 'sh'  
  args: 
  - '-c'
  - | 
      export ENV=$BRANCH_NAME
      if [ $$ENV = "master" ]; then
         export ENV="test-dev"
      fi
      echo "***********************"
      echo "$BRANCH_NAME"
      echo "$$ENV"
      echo "***********************"