[英]Expected behaviour of if-then-else condition in bash while checking for any changes in git is opposite when written in a github actions workflow
If you have a commit statement when there are no changes then it will return an exit code of 1 which will lead to the failure of a GitHub workflow.如果在没有更改的情况下有提交语句,那么它将返回退出代码 1,这将导致 GitHub 工作流失败。
As a workaround, I tried to modify the solution from this SO post as well as this answer which is basically to use -作为一种解决方法,我尝试修改此SO 帖子中的解决方案以及基本上要使用的答案-
git diff-index --quiet HEAD
On my local bash, it works as expected.在我本地的 bash 上,它按预期工作。 When I run echo "$?"
当我运行echo "$?"
, the output of 1 or 0 corresponds to what I expect. , 1 或 0 的 output 符合我的预期。
Based off of that and this documentation on setting up environment variable in GitHub workflows, I have the following in my workflow file -基于此以及有关在 GitHub 工作流程中设置环境变量的文档,我的工作流程文件中有以下内容 -
- name: Check for changes # This blocks behaviour is just the opposite
run: |
if git diff-index --quiet HEAD; then
echo "changes_exist=true" >> $GITHUB_ENV
else
echo "changes_exist=false" >> $GITHUB_ENV
fi
- name: Debug changes
run: |
echo "${{ env.changes_exist }}"
- name: Commit Datasets # It is true whenever there are NO changes and false whenever there are changes, so whenever there are changes it is just skipped due to the "if" condition
if: env.changes_exist == 'true'
run: |
git commit -m "updating datasets"
git push
However, whenever there are changes to be committed or tracked, the environment variable changes_exist is false
and vice-versa.但是,只要有要提交或跟踪的更改,环境变量changes_exist就会为false
,反之亦然。
This is just the opposite of what I am expecting.这与我的预期正好相反。
As a workaround till I figure this out I use the following which works perfectly in the worker -作为一种解决方法,直到我弄清楚这一点,我使用以下在工人中完美工作的方法 -
git diff-index --quiet HEAD || git commit -m "updating datasets"
Any idea why the above works whereas using if - then - else
the behavior opposite to what it is supposed to be?知道为什么上述方法有效,而使用if - then - else
的行为与应有的行为相反吗?
You are referencing this answer which shows:您正在引用这个答案,它显示:
git diff-index --quiet HEAD || git commit -m 'bla'
Which means, there are changes if ! git diff
这意味着,如果! git diff
! git diff
: the commit part is executed only if the git diff
part is false, fails, different from 0. ! git diff
:仅当git diff
部分为 false、失败、不同于 0 时,才会执行提交部分。
It could be written as:它可以写成:
if ! git diff-index --quiet HEAD; then git commit -m 'bla'; fi
In other words, in your case:换句话说,在您的情况下:
if ! git diff-index --quiet HEAD; then
echo "changes_exist=true" >> $GITHUB_ENV
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.