如何在 GitHub Action 中检查文件与上一次提交的差异?

huangapple go评论111阅读模式
英文:

how to check diff of a file compared to the previous commit on github action

问题

我有一个存储时间戳的文件。
为了检查该文件是否与先前的提交有所更改,我只需在本地运行 git diff,然后解析输出:

  git diff --word-diff=porcelain HEAD~1 -- dev/tests/artifact_build_timestamp.info  > /tmp/timestamp-diff.txt || exit 2;

即使在工作目录中有一个新的未提交的时间戳,这也是有用的。

但是:在 GitHub Actions 中,这是不可能的。在检出时,它处于分离模式,我无法与 HEAD~1 进行比较:

通过使用 actions/checkout@v3 操作,它执行以下操作(我希望它执行)。为了获得完整的历史记录,我设置了 fetch-depth: 0

Checking out the ref
  /usr/bin/git checkout --progress --force refs/remotes/pull/4/merge
  Note: switching to 'refs/remotes/pull/4/merge'.

我想知道与其先前的提交进行比较的最佳方法是什么。我有一个想法:

if [[ ! -z "$CI" ]]; then
  BRANCH_NAME=${GITHUB_HEAD_REF}
  git pull origin ${BRANCH_NAME} || exit 1;
  git diff --word-diff=porcelain origin/${BRANCH_NAME}~1 origin/${BRANCH_NAME} -- dev/tests/artifact_build_timestamp.info  > /tmp/timestamp-diff.txt || exit 2;
fi

但我不知道在任何情况下运行操作是否安全,甚至是否有更好的方法。

英文:

I have a file with a timestamp stored in it.
In order to check if that file has changed compared to the previous commit, I simply git diff on local, and then I parse the output:

  git diff --word-diff=porcelain HEAD~1 -- dev/tests/artifact_build_timestamp.info  > /tmp/timestamp-diff.txt || exit 2;

This is useful even when I have a new uncommited timestamp on the WD too.

But: on github actions this is not possible. When checking out, it is in detached mode and I can't compare against HEAD~1:

By using the actions/checkout@v3 action this is what it performs (and I want it to perform). In order to have the full history I set the fetch-depth: 0

Checking out the ref
  /usr/bin/git checkout --progress --force refs/remotes/pull/4/merge
  Note: switching to 'refs/remotes/pull/4/merge'.

I'm wondering what's the best way to compare against its previous commit. An idea I have is:

if [[ ! -z "$CI" ]]; then
  BRANCH_NAME=${GITHUB_HEAD_REF}
  git pull origin ${BRANCH_NAME} || exit 1;
  git diff --word-diff=porcelain origin/${BRANCH_NAME}~1 origin/${BRANCH_NAME} -- dev/tests/artifact_build_timestamp.info  > /tmp/timestamp-diff.txt || exit 2;
fi

But I don't know if it's safe on any case an action could be run, or even if there is a better way.

答案1

得分: 2

默认情况下,actions/checkout 只会检出一个提交,使得检出是浅层的。

如果你想要额外检出一个(之前的)提交,将 fetch-depth 输入设置为 1

- uses: actions/checkout@v2
  with:
    fetch-depth: 1

如果你想要整个历史记录(所有提交),将 fetch-depth 输入设置为 0

- uses: actions/checkout@v2
  with:
    fetch-depth: 0

请参考文档

当进行检出时,它处于分离状态

这是无关紧要的。无论是分离的 HEAD 还是在分支上 — 如果你有两个提交,你可以运行 git diff HEAD~。如果你只有一个提交,你不能运行 — 因为没有 HEAD~

英文:

By default actions/checkout only checks out a single commit, making the checkout shallow.

If you want one additional (previous) commit set the fetch-depth input to 1:

- uses: actions/checkout@v2
  with:
    fetch-depth: 1

If you want the entire history (all commits) set the fetch-depth input to 0:

- uses: actions/checkout@v2
  with:
    fetch-depth: 0

See the docs.

> When checking out, it is in detached mode

That's irrelevant. Detached HEAD or on branch — if you have 2 commits you can run git diff HEAD~. If you only have 1 commit you cannot — there is no HEAD~.

huangapple
  • 本文由 发表于 2023年8月9日 01:21:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定