GitHub Actions - 从最近的提交中复制 git 的 `user.name` 和 `user.email`

7
我有一个GitHub Action,它在我的repo上推送时运行。基本上,它会编译repo文件,提交已编译的文件,将该提交压缩到前一个提交中,然后强制推送到repo。
本质上,这个想法是无缝地创建构建并将其添加到repo中,使它看起来像这是原始推送的一部分。
我唯一遇到的问题是git config。我希望能够从最后一次提交复制user.nameuser.email,以便当我的Action执行提交时,它看起来像是由推送的用户完成的。我知道我可以使用GITHUB_ACTOR环境变量获取用户名,但似乎没有环境变量提供电子邮件。而且GitHub会发现它们不相同:

enter image description here

那么,我该如何设置git config来使用最后一次提交的user.nameuser.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

请参考以下链接中的代码示例:https://dev59.com/Bl0a5IYBdhLWcg3w2717#29876744。该链接提供了一种使用Git命令获取作者名称的方法。 - phd
@phd 这篇文章说“我想能够复制最后一次提交的凭据”,但是看着yaml文件,确实只有用户名/电子邮件。 - Nadir
凭据指的是user.nameuser.email - Jordan Mitchell Barrett
@JordanMitchellBarrett 这些不是凭据,它们用于提交标识,而凭据用于身份验证(登录)。 - phd
1
好的,我的错,我已经编辑了问题,删除了“凭据”这个词。 - Jordan Mitchell Barrett
显示剩余2条评论
3个回答

14

你可以从git手册中找到运行以下命令以获取最后一次提交的用户名和电子邮件地址:

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)"

请注意,对于一次性操作(单个git commit),您可以使用git -c user.name=$name -c user.email=$email commit ...(假设适当的$name等已设置)。 - torek
你也可以用git commit --amend --no-edit来获得类似软重置和重用提交消息的等效操作。将它们组合起来,你只需要大约4个命令(两个 "git log"获取 %an 和 %ae 值,一个 "git add",以及一个 "git -c …commit --amend …")便可完成推送步骤之前的操作。请注意,整个方案自动替换一个提交,而这个提交不是最终存在的提交,因此用户必须意识到这一点才可以使用。 - torek

12
git config --global user.name "${GITHUB_ACTOR}"
git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"

2
这并不是对原问题的回答,但却正是我所寻找的! - oelna
3
这两个选项不是颠倒了吗? - Dave Cross
5
根据最新文档,"${GITHUB_ACTOR_ID}+${GITHUB_ACTOR}@users.noreply.github.com" 可能是更好的选择: https://docs.github.com/en/account-and-profile/setting-up-and-managing-your-personal-account-on-github/managing-email-preferences/setting-your-commit-email-address#about-commit-email-addresses - Sophie Alpert

1
如果你想使用在个人资料中设置的名字而不是用户名,你可以使用GitHub API并查询你自己的个人资料:
name: git-config

on:
  push:
    branches: [ "main" ]
  workflow_dispatch:

jobs:
  git:
    runs-on: ubuntu-latest
    steps:
      - name: set git config
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          git config --global user.email "${GITHUB_ACTOR_ID}+${GITHUB_ACTOR}@users.noreply.github.com"
          git config --global user.name "$(gh api /users/${GITHUB_ACTOR} | jq .name -r)"
          git config -l

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接