簡體   English   中英

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

[英]GitHub Actions - copy git `user.name` and `user.email` from last commit

我有一個 GitHub Action 可以在任何推送到我的倉庫時運行。 基本上,它編譯 repo 文件,提交已編譯的文件,將提交的文件壓縮到前一個文件中,然后強制推送到 repo。

從本質上講,這個想法是它無縫地創建一個構建並將其添加到存儲庫中,使其看起來像是原始推動的一部分。

我遇到的唯一問題是git config 我希望能夠從上次提交中復制user.nameuser.email ,這樣當我的操作執行其提交時,它看起來就像是由推送的用戶完成的。 我知道我可以使用GITHUB_ACTOR環境變量來獲取用戶名,但似乎沒有提供電子郵件的環境變量。 GitHub 發現它們並不相同:

在此處輸入圖像描述

那么,如何將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

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 config --global user.email "${GITHUB_ACTOR}"
git config --global user.name "${GITHUB_ACTOR}@users.noreply.github.com"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM