[英]Is there is any way to push my database changes to github using git bash?
[英]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
如果在沒有更改的情況下有提交語句,那么它將返回退出代碼 1,這將導致 GitHub 工作流失敗。
作為一種解決方法,我嘗試修改此SO 帖子中的解決方案以及基本上要使用的答案-
git diff-index --quiet HEAD
在我本地的 bash 上,它按預期工作。 當我運行echo "$?"
, 1 或 0 的 output 符合我的預期。
基於此以及有關在 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
但是,只要有要提交或跟蹤的更改,環境變量changes_exist就會為false
,反之亦然。
這與我的預期正好相反。
作為一種解決方法,直到我弄清楚這一點,我使用以下在工人中完美工作的方法 -
git diff-index --quiet HEAD || git commit -m "updating datasets"
知道為什么上述方法有效,而使用if - then - else
的行為與應有的行為相反嗎?
您正在引用這個答案,它顯示:
git diff-index --quiet HEAD || git commit -m 'bla'
這意味着,如果! git diff
! git diff
:僅當git diff
部分為 false、失敗、不同於 0 時,才會執行提交部分。
它可以寫成:
if ! git diff-index --quiet HEAD; then git commit -m 'bla'; fi
換句話說,在您的情況下:
if ! git diff-index --quiet HEAD; then
echo "changes_exist=true" >> $GITHUB_ENV
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.