Github Actions でコミットを作る

エラー

GitHub Actionsでコミットを作るにはuser.nameuser.emailの設定が必要となる。
設定していない場合はエラーとなる。

例えば

name: Commit

on:
  push:

jobs:
  commit:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          ref: main

      - name: Set up Git
        run: |
          git switch -c test-$(date "+%Y%m%d%H%M%S") main
          git commit --allow-empty -m "commit"
          git push origin HEAD          

この例ではuser.nameuser.emailを設定せずにgit commitしているため以下のようなエラーが出る。

Committer identity unknown

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got ...

修正

エラーメッセージにあるとおりuser.nameuser.emailを設定すればよい。

具体的には以下のようにgit configコマンドを使って設定する。

git config --global user.email "メールアドレス"
git config --global user.name "名前"

修正後の例

name: Commit

on:
  push:

jobs:
  commit:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          ref: main

      - name: Set up Git
        run: |
          git switch -c test-$(date "+%Y%m%d%H%M%S") main
          git config --global user.email "test@example.com"
          git config --global user.name "test"
          git commit --allow-empty -m "commit"
          git push origin HEAD          

環境変数を使う

設定値を変更するのが気持ち悪い場合はステップに閉じた環境変数を使う方法もある。

git commitコマンドは環境変数GIT_AUTHOR_NAMEGIT_AUTHOR_EMAILGIT_COMMITTER_NAMEGIT_COMMITTER_EMAILが設定されている場合はその値を使う。
該当ステップの環境変数に設定しておけばよい。

name: Commit

on:
  push:

jobs:
  commit:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          ref: main

      - name: Set up Git
        run: |
          git switch -c test-$(date "+%Y%m%d%H%M%S") main
          git commit --allow-empty -m "commit"
          git push origin HEAD          
        env:
          GIT_AUTHOR_NAME: test
          GIT_AUTHOR_EMAIL: test@example.com
          GIT_COMMITTER_NAME: test
          GIT_COMMITTER_EMAIL: test@example.com

特殊なメールアドレス

GitHub Actionsでコミットを作る際、自動で作成したコミットを識別するために特殊なユーザ名を使うと便利。

actions-user

GitHub Actionsでコミットをする場合、action@github.com、もしくは65916846+actions-user@users.noreply.github.comが広く使われている。

これらを指定した場合はactions-userとして表示され、アイコンがGitHub Actionsのものになる。

action-user

github-actions[bot]

41898282+github-actions[bot]@users.noreply.github.comを指定した場合はgithub-actions[bot]として表示される。

このメールアドレスを指定した場合はgithub-actions[bot]として表示され、アイコンがGitHubのものになる。

github-actions