7

I have a GitHub Action which runs on any push to my repo. Basically, it compiles the repo files, commits the compiled files, squashes that commit into the previous one, then force pushes to the repo.

Essentially, the idea is that it seamlessly creates a build and adds it to the repo, to make it look like this was part of the original push.

The only problem I'm having is with git config. I want to be able to copy the user.name and user.email from the last commit, so that when my action performs its commit, it looks like it was done by the user who pushed. I know I can use the GITHUB_ACTOR environment variable to get the username, but there doesn't seem to be an env variable which gives the email. And GitHub works out that they're not the same:

enter image description here

So, how can I set git config to use the user.name and user.email from the last commit?


Here is the action's .yaml file for completeness:

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
  • If that would be possible it would mean a huge security hole, allowing some tooling to simply sniff out user credentials, and then use them for some malicious things – Nadir Nov 04 '21 at 13:19
  • https://stackoverflow.com/a/29876744/7976758 Found in https://stackoverflow.com/search?q=%5Bgit%5D+get+author+name – phd Nov 04 '21 at 13:21
  • @Nadir The OP wants `user.name`/`user.email`, not login credentials. – phd Nov 04 '21 at 13:21
  • @phd the post says `I want to be able to copy the credentials from the last commit` so... but looking at the yaml indeed its only username/email – Nadir Nov 04 '21 at 13:28
  • By credentials, I meant just `user.name` and `user.email`. – Jordan Mitchell Barrett Nov 04 '21 at 13:30
  • @JordanMitchellBarrett Those are not credentials, they're for commit identification while credentials are for authentication (login). – phd Nov 04 '21 at 13:32
  • 1
    Okay my bad, I've edited the question to remove the word "credentials". – Jordan Mitchell Barrett Nov 04 '21 at 13:36

3 Answers3

14

From the git manual, you can get the username and email of the user who made the last commit by running:

git log -n 1 --pretty=format:%an    # username
git log -n 1 --pretty=format:%ae    # email

So, you can set the config as required by substituting the output into the git config command:

git config user.name "$(git log -n 1 --pretty=format:%an)"
git config user.email "$(git log -n 1 --pretty=format:%ae)"
  • Note that for a one-time operation (a single `git commit`), you can use `git -c user.name=$name -c user.email=$email commit ...` (given appropriate `$name` etc). – torek Nov 04 '21 at 20:37
  • You can also get the equivalent of a soft-reset-and-re-use-commit-message with `git commit --amend --no-edit`. Combining these will get you down to about 4 commands (two `git log`s to get %an and %ae values, one `git add`, and one `git -c ... commit --amend ...`) before the push step. Note that this whole scheme, of auto-replacing a commit, is ... OK, but users have to be aware that the commit they sent is not the one that will be there eventually. – torek Nov 04 '21 at 20:47
10
git config --global user.name "${GITHUB_ACTOR}"
git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
SANTHOSH.SJ
  • 343
  • 1
  • 4
  • 7
  • 2
    This isn't really an answer to the original question, but exactly what I was looking for! – oelna Nov 21 '22 at 09:17
  • 3
    Aren't the two options the wrong way round? – Dave Cross Dec 14 '22 at 10:32
  • 3
    Based on the latest docs, `"${GITHUB_ACTOR_ID}+${GITHUB_ACTOR}@users.noreply.github.com"` may be a better bet: 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 Apr 20 '23 at 00:42
1

If you want to use the name which you set in your profile instead of you user name you can use the github api and query your own profile:

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
Nils
  • 382
  • 3
  • 12