bash 有条件地运行脚本在 travis.yml 中不起作用,为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38789253/
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
Running script conditionally does not work in travis.yml, why?
提问by user1283776
The following causes travis to not build at all. When I try to validate the travis.yml
file, it complains that the line just above the if statement is missing a -
character at column 3, but the error has to do with the if statement below.
以下原因导致 travis 根本无法构建。当我尝试验证travis.yml
文件时,它抱怨 if 语句正上方的行在第-
3 列缺少一个字符,但错误与下面的 if 语句有关。
Do I have to move the if statement out to a script?
我是否必须将 if 语句移到脚本中?
# Deploy
after_success:
- ./tools/docker-push-container.sh
- if [ $TRAVIS_BRANCH == "master" && $TRAVIS_PULL_REQUEST == "false" ]; then
./.travis/success_message.sh
fi
回答by larsks
You're making some assumptions about YAML syntax that are causing you problems. If you "exend" a line of YAML by indenting subsequent lines, like this:
您正在对导致您出现问题的 YAML 语法做出一些假设。如果您通过缩进后续行来“扩展”一行 YAML,如下所示:
- The quick brown fox
jumped over the
lazy dog.
It is exactly like you instead wrote this:
就像你写的那样:
- The quick brown fox jumped over the lazy dog.
This means that your shell fragment, which you've written as:
这意味着你的 shell 片段,你已经写成:
- if [ $TRAVIS_BRANCH == "master" && $TRAVIS_PULL_REQUEST == "false" ]; then
./.travis/success_message.sh
fi
Actually becomes:
实际上变成:
if [ $TRAVIS_BRANCH == "master" && $TRAVIS_PULL_REQUEST == "false" ]; then ./.travis/success_message.sh fi
And if you try run that line in the shell, you get:
如果您尝试在 shell 中运行该行,您会得到:
sh: -c: line 1: syntax error: unexpected end of file
If you want to include a multi-line shell script in your YAML document, your best bet is probably to use the verbatim block operator, |
, like this:
如果要在 YAML 文档中包含多行 shell 脚本,最好的办法可能是使用逐字块运算符|
,如下所示:
- |
if [ $TRAVIS_BRANCH == "master" && $TRAVIS_PULL_REQUEST == "false" ]; then
./.travis/success_message.sh
fi
Which will result, as intended, in:
这将导致,如预期的那样:
if [ $TRAVIS_BRANCH == "master" && $TRAVIS_PULL_REQUEST == "false" ]; then
./.travis/success_message.sh
fi
Alternatively, you could just make proper use of semicolons:
或者,您可以正确使用分号:
- if [ $TRAVIS_BRANCH == "master" && $TRAVIS_PULL_REQUEST == "false" ]; then
./.travis/success_message.sh;
fi
Note the new ;
before the terminal fi
. This results in:
注意;
终端前的新fi
。这导致:
if [ $TRAVIS_BRANCH == "master" && $TRAVIS_PULL_REQUEST == "false" ]; then ./.travis/success_message.sh; fi
...which is perfectly valid shell syntax.
...这是完全有效的 shell 语法。
回答by Raf
I tried the above solution by larsksbut, it did not work for me and could be because in bash
when you use &&
and ||
you need to separate the conditions.
我试图通过上述方案larsks但是,它并没有对我来说有效,可能是因为bash
当你使用&&
和||
需要分离的条件。
I had the following (according to above solution)
我有以下(根据上述解决方案)
- if [ $TRAVIS_PULL_REQUEST == false && $TRAVIS_BRANCH == "development" ]; then
echo "# Bump version and flyway migrate db";
else
echo "Skip version increment!";
fi
And I also checked the .travis.yml
in Travis Lintand it shows as valid but, according to bash the above can be separated as shown below, also mentioned in this SO Questionand I changed to following
我还检查.travis.yml
在特拉维斯棉绒,它显示了作为有效的,但是,根据打坏上述可以分离如下所示,同样在此提到的SO问题和余改变为以下
- if [ $TRAVIS_PULL_REQUEST == false ] && [ $TRAVIS_BRANCH == "development" ]; then
echo "# Bump version and flyway migrate db";
else
echo "Skip version increment!";
fi
And travis buildworked. It kind of make sense because above is valid way to use &&and ||in bash. Just sharing in case someone else come across a similar issue.
和特拉维斯构建工作。这是有道理的,因为上面是使用&&和|| 的有效方法 在 bash 中。只是分享以防其他人遇到类似的问题。
回答by Rebekah Apelt
Raf's solution worked for me with separating the conditionals with []
Raf 的解决方案对我有用,用 [] 分隔条件
[ $TRAVIS_PULL_REQUEST == false ] && [ $TRAVIS_BRANCH == "develop" ];
回答by MrMeemus
None of the suggested workarounds worked for me, I used this instead:
建议的解决方法都不适合我,我改用了这个:
- if [[ ( "$TRAVIS_OS_NAME" == "osx" ) && ( "$TOXENV" == "py36" ) ]]; then brew update; fi