bash 可以在 bitbucket 管道中使用“if”语句吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46599478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can "if" statements be used in bitbucket pipelines?
提问by Gaston Pisacco
Im trying to run the following step, but it does not execute the "if" steps (lines 5 and 6) (I'm pretty sure they should as the directory tested for does not exist, i tried in multiple formats of bash if, but all of them fails. Is there a way to test for a condition than the one I'm using?
我试图运行以下步骤,但它不执行“if”步骤(第 5 行和第 6 行)(我很确定它们应该执行,因为测试的目录不存在,我尝试了多种格式的 bash,如果,但所有这些都失败了。有没有办法测试比我正在使用的条件的条件?
- step:
name: Google Cloud SDK Installation
caches:
- pip
- cloudsdk
script:
- export ENV=dev
- source scripts/setenv.sh
- export CLOUDSDK_CORE_DISABLE_PROMPTS=1
- SDK_FILENAME=google-cloud-sdk-$SDK_VERSION-linux-x86_64.tar.gz
- if [ ! -e ${HOME}/google-cloud-sdk ] ; then `curl -O -J https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/${SDK_FILENAME}`; fi
- if [ ! -e ${HOME}/google-cloud-sdk ] ; then `tar -zxvf ${SDK_FILENAME} --directory ${HOME}`; fi
- export PATH=${PATH}:${HOME}/google-cloud-sdk/bin
- GAE_PYTHONPATH=${HOME}/google_appengine
- export PYTHONPATH=${PYTHONPATH}:${GAE_PYTHONPATH}
- python scripts/fetch_gae_sdk.py $(dirname "${GAE_PYTHONPATH}")
回答by BlueM
Basically, Bb Pipelines support conditions, be it file checks using -e
or comparisons. For instance, these lines all do work:
基本上,Bb Pipelines 支持条件,无论是文件检查使用-e
还是比较。例如,这些行都有效:
script:
- '[ ! -e "$BITBUCKET_CLONE_DIR/missing.txt" ] && echo "File does not exist"'
- 'if [ ! -e "$BITBUCKET_CLONE_DIR/missing.txt" ]; then echo "File does not exist"; fi'
- if [ ! -e "$BITBUCKET_CLONE_DIR/missing.txt" ]; then echo "File does not exist"; fi
As shown in the example, for some commands it can be needed to wrap the line in single quotes (here, only for demonstration purposes), so if Bb reports a syntax error, you should experiment with that.
如示例所示,对于某些命令,可能需要将行括在单引号中(此处仅用于演示目的),因此如果 Bb 报告语法错误,您应该尝试这样做。
But: are you sure you really want $HOME
? By default, you are in $BITBUCKET_CLONE_DIR
– not in $HOME
–, and therefore the curl
call downloads the SDK to $BITBUCKET_CLONE_DIR
.
但是:你确定你真的想要$HOME
吗?默认情况下,您在$BITBUCKET_CLONE_DIR
– 不在$HOME
–,因此curl
调用会将 SDK 下载到$BITBUCKET_CLONE_DIR
.