在IF语句中使用Bitbucket的$ BITBUCKET_TAG变量

问题描述

我整天都在努力寻找解决办法。有人可以指出为什么吗

- if [ $BITBUCKET_BRANCH == 'master' ]; echo "Do master branch stuff"; fi

如果我推送到的分支是master

在尝试运行一些代码时工作得很好。

但是当我试图通过带有标签标签来消除它时

- if [ $BITBUCKET_TAG == 'test-*' ]; then echo "Do test tag stuff"; fi

它完全被忽略,好像从未到达if语句中的代码

我在做什么错?我试图以多种方式更改语句,尝试使用正则表达式等无济于事。任何帮助将不胜感激。

这是可复制的示例管道代码

@H_404_24@image: node:12.16.0 options: docker: true deFinitions: steps: - step: &if-test name: If test script: - if [ $BITBUCKET_BRANCH == 'master' ]; then echo "Do master branch stuff"; fi - if [ $BITBUCKET_TAG == 'test-*' ]; then echo "Do test tag stuff"; fi - if [ $BITBUCKET_TAG == 'staging-*' ]; then echo "Do staging tag stuff"; fi pipelines: branches: master: - step: *if-test tags: 'test-*': - step: *if-test 'staging-*': - step: *if-test

解决方法

问题在于您编写"if"语句的方式:

if [ $BITBUCKET_TAG == 'test-*' ];

这是一个bash / unix if语句,它将检查文字字符串"test-*"作为分支名称,您可能不会使用它。

您应该使用 'string contains' 测试而不是'string equals'测试,如下所示:

if [[ $BITBUCKET_TAG == *"test-"* ]];

还请注意,yml'test-*'的用法...

tags:
  'test-*':

...与bash / shell脚本解释'test-*'的方式不同。