diff --git a/README.md b/README.md index e86c8ca..9873d01 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,8 @@ This action supports three tag levels for flexible versioning: amend: false commit_prefix: "[AUTO]" commit_message: "Automatic commit" + user_name: "" + user_email: "" signing_mode: "" signing_key: "" signing_passphrase: "" @@ -79,6 +81,8 @@ This action supports three tag levels for flexible versioning: | `amend` | No | `false` | Whether to make an amendment to the previous commit (`--amend`). Can be combined with `commit_message` to change the commit message. | | `commit_prefix` | No | `""` | Prefix added to commit message. Combines with `commit_message`. | | `commit_message` | No | `""` | Commit message to set. Combines with `commit_prefix`. Can be used with `amend` to change the commit message. | +| `user_name` | No | `""` | Git `user.name` used for created commits. Defaults to `${{ github.actor }}` when empty. | +| `user_email` | No | `""` | Git `user.email` used for created commits. Defaults to `${{ github.actor }}@users.noreply.` when empty. | | `signing_mode` | No | `""` | Commit signing mode. Supported values are `gpg` and `ssh`. Leave empty to disable signing. | | `signing_key` | No | `""` | Signing key material. For `gpg`, provide an ASCII-armored private key export. For `ssh`, provide a private key in OpenSSH or PEM format. | | `signing_passphrase` | No | `""` | Optional passphrase for the signing key. Passphrase-protected GPG keys are supported. Encrypted SSH signing keys are rejected in the current runtime. | @@ -222,6 +226,24 @@ jobs: commit_message: "Update README" ``` +### 👤 Custom commit identity example +Override the git author/committer identity used by the action. + +```yaml +- name: Commit and push with custom identity + uses: devops-infra/action-commit-push@v1.4.0 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + commit_message: "test(commit-push): custom identity" + user_name: "Release Automation" + user_email: "release-bot@example.com" +``` + +When `user_name` and `user_email` are empty, the action defaults to: + +- `user.name = ${{ github.actor }}` +- `user.email = ${{ github.actor }}@users.noreply.` + ## 🔏 Commit Signing This action can sign generated commits by configuring repository-local git signing settings at runtime. diff --git a/action.yml b/action.yml index 23f8bbc..8eb8a27 100644 --- a/action.yml +++ b/action.yml @@ -22,6 +22,14 @@ inputs: description: Commit message to set required: false default: "" + user_name: + description: Git user.name to use for created commits. Defaults to GITHUB_ACTOR when empty. + required: false + default: "" + user_email: + description: Git user.email to use for created commits. Defaults to GITHUB_ACTOR@users.noreply. when empty. + required: false + default: "" signing_mode: description: Commit signing mode. Supported values are gpg and ssh. required: false diff --git a/entrypoint.sh b/entrypoint.sh index ee3e62e..4832355 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -10,6 +10,8 @@ echo " add_timestamp: ${INPUT_ADD_TIMESTAMP}" echo " amend: ${INPUT_AMEND}" echo " commit_prefix: ${INPUT_COMMIT_PREFIX}" echo " commit_message: ${INPUT_COMMIT_MESSAGE}" +echo " user_name: ${INPUT_USER_NAME}" +echo " user_email: ${INPUT_USER_EMAIL}" echo " signing_mode: ${INPUT_SIGNING_MODE}" echo " force: ${INPUT_FORCE}" echo " force_with_lease: ${INPUT_FORCE_WITH_LEASE}" @@ -242,8 +244,12 @@ echo "[INFO] Using repository path: ${REPO_DIR}" # Set git credentials git -C "${REPO_DIR}" remote set-url origin "https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@${INPUT_ORGANIZATION_DOMAIN}/${GITHUB_REPOSITORY}" -git -C "${REPO_DIR}" config user.name "${GITHUB_ACTOR}" -git -C "${REPO_DIR}" config user.email "${GITHUB_ACTOR}@users.noreply.${INPUT_ORGANIZATION_DOMAIN}" +git -C "${REPO_DIR}" config user.name "${INPUT_USER_NAME:-${GITHUB_ACTOR}}" +git -C "${REPO_DIR}" config user.email "${INPUT_USER_EMAIL:-${GITHUB_ACTOR}@users.noreply.${INPUT_ORGANIZATION_DOMAIN}}" +export GIT_AUTHOR_NAME="${INPUT_USER_NAME:-${GITHUB_ACTOR}}" +export GIT_AUTHOR_EMAIL="${INPUT_USER_EMAIL:-${GITHUB_ACTOR}@users.noreply.${INPUT_ORGANIZATION_DOMAIN}}" +export GIT_COMMITTER_NAME="${INPUT_USER_NAME:-${GITHUB_ACTOR}}" +export GIT_COMMITTER_EMAIL="${INPUT_USER_EMAIL:-${GITHUB_ACTOR}@users.noreply.${INPUT_ORGANIZATION_DOMAIN}}" setup_commit_signing cd "${REPO_DIR}" diff --git a/tests/docker/local-image.yml b/tests/docker/local-image.yml index f723910..7e80be2 100644 --- a/tests/docker/local-image.yml +++ b/tests/docker/local-image.yml @@ -134,6 +134,42 @@ commandTests: git -C /tmp/ws config gpg.format ssh git -C /tmp/ws config gpg.ssh.allowedSignersFile /tmp/allowed_signers git -C /tmp/ws verify-commit HEAD + + - name: Entrypoint overrides commit identity + command: bash + args: + - -lc + - | + set -euo pipefail + rm -rf /tmp/ws /tmp/remote.git /tmp/github_output.txt + mkdir -p /tmp/ws + git init /tmp/ws + git -C /tmp/ws config user.name test + git -C /tmp/ws config user.email test@example.com + touch /tmp/ws/.keep + git -C /tmp/ws add . + git -C /tmp/ws commit -m init + git init --bare /tmp/remote.git + git -C /tmp/ws remote add origin /tmp/remote.git + echo "identity override" > /tmp/ws/e2e-identity.txt + export GITHUB_WORKSPACE=/tmp/ws + export GITHUB_ACTOR=tester + export GITHUB_REPOSITORY=owner/repo + export GITHUB_OUTPUT=/tmp/github_output.txt + export GITHUB_TOKEN=fake + export INPUT_ORGANIZATION_DOMAIN=github.com + export INPUT_REPOSITORY_PATH=. + export INPUT_AMEND=false + export INPUT_ALLOW_EMPTY_COMMIT=false + export INPUT_TARGET_BRANCH='' + export INPUT_COMMIT_MESSAGE='identity override commit' + export INPUT_USER_NAME='Release Automation' + export INPUT_USER_EMAIL='release-bot@example.com' + /entrypoint.sh || true + test "$(git -C /tmp/ws log -1 --format=%an)" = "Release Automation" + test "$(git -C /tmp/ws log -1 --format=%ae)" = "release-bot@example.com" + test "$(git -C /tmp/ws log -1 --format=%cn)" = "Release Automation" + test "$(git -C /tmp/ws log -1 --format=%ce)" = "release-bot@example.com" fileExistenceTests: - name: entrypoint exists path: /entrypoint.sh