如果没有更改,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 $?声明。

希望很清楚。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...