GitHub 操作 - 从上次提交复制 git `user.name` 和 `user.email`
GitHub Actions - copy git `user.name` and `user.email` from last commit
我有一个 GitHub 操作,它在任何推送到我的存储库时运行。基本上,它编译 repo 文件,提交编译后的文件,将提交的文件压缩到前一个文件中,然后强制推送到 repo。
本质上,这个想法是它无缝地创建一个构建并将其添加到存储库中,让它看起来像是原始推送的一部分。
我遇到的唯一问题是 git config
。我希望能够从上次提交中复制 user.name
和 user.email
,这样当我的操作执行提交时,它看起来就像是由推送的用户完成的。我知道我可以使用 GITHUB_ACTOR
环境变量来获取用户名,但似乎没有提供电子邮件的环境变量。 GitHub 结果发现它们不一样:
那么,如何设置 git config
以使用上次提交的 user.name
和 user.email
?
为了完整性,这里是操作的 .yaml
文件:
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Set up Git repository
uses: actions/checkout@v2
with:
fetch-depth: 2
- (... step that creates build files ...)
- name: Upload build files to repo
run: |
# Configure git
git config user.name "$GITHUB_ACTOR"
git config user.email "<>"
# Roll back git history to before last commit
# This also adds all changes in the last commit to the working tree
git reset --soft HEAD~1
# Add build files to working tree
git add (build files)
# Commit changes and build files using same commit info as before
git commit -C HEAD@{1}
# Force push to overwrite git history
git push --force
从 git manual 中,您可以获取最后一次提交 运行 的用户的用户名和电子邮件:
git log -n 1 --pretty=format:%an # username
git log -n 1 --pretty=format:%ae # email
因此,您可以通过将输出替换为 git config
命令来根据需要设置配置:
git config user.name "$(git log -n 1 --pretty=format:%an)"
git config user.email "$(git log -n 1 --pretty=format:%ae)"
我有一个 GitHub 操作,它在任何推送到我的存储库时运行。基本上,它编译 repo 文件,提交编译后的文件,将提交的文件压缩到前一个文件中,然后强制推送到 repo。
本质上,这个想法是它无缝地创建一个构建并将其添加到存储库中,让它看起来像是原始推送的一部分。
我遇到的唯一问题是 git config
。我希望能够从上次提交中复制 user.name
和 user.email
,这样当我的操作执行提交时,它看起来就像是由推送的用户完成的。我知道我可以使用 GITHUB_ACTOR
环境变量来获取用户名,但似乎没有提供电子邮件的环境变量。 GitHub 结果发现它们不一样:
那么,如何设置 git config
以使用上次提交的 user.name
和 user.email
?
为了完整性,这里是操作的 .yaml
文件:
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Set up Git repository
uses: actions/checkout@v2
with:
fetch-depth: 2
- (... step that creates build files ...)
- name: Upload build files to repo
run: |
# Configure git
git config user.name "$GITHUB_ACTOR"
git config user.email "<>"
# Roll back git history to before last commit
# This also adds all changes in the last commit to the working tree
git reset --soft HEAD~1
# Add build files to working tree
git add (build files)
# Commit changes and build files using same commit info as before
git commit -C HEAD@{1}
# Force push to overwrite git history
git push --force
从 git manual 中,您可以获取最后一次提交 运行 的用户的用户名和电子邮件:
git log -n 1 --pretty=format:%an # username
git log -n 1 --pretty=format:%ae # email
因此,您可以通过将输出替换为 git config
命令来根据需要设置配置:
git config user.name "$(git log -n 1 --pretty=format:%an)"
git config user.email "$(git log -n 1 --pretty=format:%ae)"