GitHub Actions - 从上次提交中复制 git `user.name` 和 `user.email`

Jor*_*ett 7 git github github-actions

我有一个 GitHub Action,它可以在任何推送到我的存储库时运行。基本上,它编译存储库文件,提交编译的文件,压缩提交到前一个文件,然后强制推送到存储库。

从本质上讲,这个想法是它无缝地创建一个构建并将其添加到存储库中,使其看起来像是原始推送的一部分。

我遇到的唯一问题是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
Run Code Online (Sandbox Code Playgroud)

Jor*_*ett 16

git 手册中,您可以通过运行以下命令获取上次提交的用户的用户名和电子邮件:

git log -n 1 --pretty=format:%an    # username
git log -n 1 --pretty=format:%ae    # email
Run Code Online (Sandbox Code Playgroud)

因此,您可以通过将输出替换为命令来根据需要设置配置git config

git config user.name "$(git log -n 1 --pretty=format:%an)"
git config user.email "$(git log -n 1 --pretty=format:%ae)"
Run Code Online (Sandbox Code Playgroud)


小智 14

git config --global user.name "${GITHUB_ACTOR}"
git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
Run Code Online (Sandbox Code Playgroud)

  • 根据最新的文档,“${GITHUB_ACTOR_ID}+${GITHUB_ACTOR}@users.noreply.github.com”可能是更好的选择:https://docs.github.com/en/account-and-profile /在-github上设置和管理您的个人帐户/管理电子邮件首选项/设置您的提交电子邮件地址#about-commit-email-addresses (7认同)
  • 这实际上并不是原始问题的答案,但这正是我正在寻找的! (3认同)
  • 这两个选项是不是背道而驰了? (3认同)