Git 如果没有更改,提交失败构建,但退出代码仍然是 0
Git commit fails build if no changes, but exit code is still 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 给出 non-zero 代码构建失败。同样的逻辑适用于你所有的 echo $?声明。
希望清楚。
在编写一个 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 给出 non-zero 代码构建失败。同样的逻辑适用于你所有的 echo $?声明。
希望清楚。