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

How to make a string compare work in 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 "***********************"