【问题标题】:Git commit fails build if no changes, but exit code is still 0如果没有更改,Git 提交将失败构建,但退出代码仍为 0
【发布时间】:2020-12-02 04:30:36
【问题描述】:

在编写提交构建工件并将其推送到另一个存储库的 GitHub 操作时,我遇到了我不理解的 git push 的奇怪行为。

这是我的构建脚本:

#!/bin/sh

# Exit immediately if a command exits with a non-zero status:
set -e

### Creation of artifacts happens here. They are written to ${GITHUB_WORKSPACE}/output/

echo "Cloning $artifacts_repo ..."
git clone $artifacts_repo "${GITHUB_WORKSPACE}/artifacts_repo"
echo "DEBUG: exit code of 'git clone' operation:"
echo $?

echo "Moving generated files to ${GITHUB_WORKSPACE}/artifacts_repo/auto-generated ..."
mkdir -p "${GITHUB_WORKSPACE}/artifacts_repo/auto-generated"
yes | cp --recursive --force "${GITHUB_WORKSPACE}/output/." "${GITHUB_WORKSPACE}/artifacts_repo/auto-generated"

echo "Committing artifacts ..."
cd "${GITHUB_WORKSPACE}/artifacts_repo"

git status
echo "DEBUG: exit code of 'git status' operation:"
echo $?

git add .
echo "DEBUG: exit code of 'git add' operation:"
echo $?

git commit -m"Auto-generated artifacts"
echo "DEBUG: exit code of 'git commit' operation:"
echo $?

echo "Pushing artifacts ..."
git push
echo "DEBUG: exit code of 'git push' operation:"
echo $?

如果有要提交的更改,则脚本会成功运行。所有退出代码都是0

如果有 没有 更改要提交(因为生成的文件与存储库中已经存在的文件匹配),则脚本在 git commit 操作时失败,而不会打印退出代码。其他退出代码仍然是0

我启用了set -e 以使构建在第一个非零退出代码处失败。

如果我删除set -e 来调试它,脚本会运行,但即使没有更改提交,git commit 的退出代码是0。那么为什么构建会失败呢?

这是set -e 被禁用时的输出:

Cloning https://***@github.com/my-org/my-repo.git ...
Cloning into '/github/workspace/artifacts_repo'...
DEBUG: exit code of 'git clone' operation:
0
Moving generated files to /github/workspace/artifacts_repo/auto-generated ...
Committing artifacts ...
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
DEBUG: exit code of 'git status' operation:
0
DEBUG: exit code of 'git add' operation:
0
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
DEBUG: exit code of 'git commit' operation:
0
Pushing artifacts ...
Everything up-to-date
DEBUG: exit code of 'git push' operation:
0

有人知道为什么会这样吗? Git有什么问题吗?这与 GitHub 操作的工作方式有关(我是新手)吗?我做错了吗?

【问题讨论】:

  • 您确定构建在git push 之后退出,而不是在git commit 之后退出?如果没有要提交的内容,git commit 将以非零值退出。
  • 你是对的,启用 'set -e' 后它会在 'git commit' 后退出。很抱歉把它弄混了。但是,在禁用“set -e”的情况下,“git commit”的退出代码仍然是“0”,尽管没有任何东西可以提交。所以这对我来说仍然很困惑:-P
  • 我添加了 'set -e' 的调试输出禁用,没有更改提交。
  • 好吧:我只是为了这个东西傻了:-P退出代码总是0,因为它是之前'echo'操作的退出代码 facepalm 是删除愚蠢的问题是个好习惯,还是应该添加一个大的警告标志?
  • 我认为这不是很愚蠢...有时会发生。

标签: git continuous-integration sh github-actions


【解决方案1】:
git commit -m"Auto-generated artifacts"
echo "DEBUG: exit code of 'git commit' operation:"
echo $?

回声$?表示之前执行命令的状态码。所以这意味着我们已经执行了echo "DEBUG: exit code of 'git commit' operation:",所以状态码是零。

启用 set -e 后,它会监控每个命令的执行,以便 git commit 给出非零代码构建失败。你所有的 echo $?声明。

希望清楚。

【讨论】:

  • 我意识到我自己 :-D 我正要删除那个问题,因为它具有误导性。由于您的回答正确指出了我的错误,我会保留它。感谢您的努力!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-02
  • 1970-01-01
  • 2012-06-22
  • 1970-01-01
  • 2019-03-05
  • 1970-01-01
相关资源
最近更新 更多