git - 如果没有更改,Git 提交将失败构建,但退出代码仍为 0
问题描述
在编写提交构建工件并将其推送到另一个存储库的 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 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 $? 陈述。
希望很清楚。
推荐阅读
- spring - SCDF - 在不同的模式中创建元数据表并使用不同的模式处理批处理作业
- javascript - 将数组从平面一维转换为树,如 javascript/lodash
- reactjs - 组件或 mapStateToProps 中的逻辑
- python - Plotly:如何在随机生成的时间序列上实现 X 轴上的日期时间范围
- javascript - 使用 innerwidth 排版由 p5.js 函数组成的字体
- javascript - 在 savePost 运行之前更新 Gutenberg 块内容
- sql - SQL 批量插入 - ROWTERMINATOR 'Ox0A' 与 '\r\n'
- c++ - 如何为 C++ 2D Snake 游戏生成随机水果
- anaconda - spyder 4.1.0 在 conda 更新后崩溃 --all
- python-3.x - 将每行两列的值相乘