Skip to content

Commit f3b199b

Browse files
committedDec 19, 2024
feat(git config): Set default user.name and user.email in git config
1 parent cbb7224 commit f3b199b

6 files changed

+26
-4
lines changed
 

‎README.md

-4
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,6 @@ jobs:
281281
- run: |
282282
date > generated.txt
283283
# Note: the following account information will not work on GHES
284-
git config user.name "github-actions[bot]"
285-
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
286284
git add .
287285
git commit -m "generated"
288286
git push
@@ -305,8 +303,6 @@ jobs:
305303
- run: |
306304
date > generated.txt
307305
# Note: the following account information will not work on GHES
308-
git config user.name "github-actions[bot]"
309-
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
310306
git add .
311307
git commit -m "generated"
312308
git push

‎action.yml

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ inputs:
2222
2323
[Learn more about creating and using encrypted secrets](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets)
2424
default: ${{ github.token }}
25+
configure-user:
26+
description: >
27+
Whether to configure user.name and user.email in the local git config.
28+
This is required to push a commit from a Github Action Workflow.
29+
Set to `false` to disable the config.
30+
default: true
2531
ssh-key:
2632
description: >
2733
SSH key used to fetch the repository. The SSH key is configured with the local

‎dist/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,9 @@ function getInputs() {
18131813
core.debug(`recursive submodules = ${result.nestedSubmodules}`);
18141814
// Auth token
18151815
result.authToken = core.getInput('token', { required: true });
1816+
// Configure user
1817+
result.configureUser =
1818+
(core.getInput('configure-user') || 'true').toUpperCase() === 'TRUE'
18161819
// SSH
18171820
result.sshKey = core.getInput('ssh-key');
18181821
result.sshKnownHosts = core.getInput('ssh-known-hosts');

‎src/git-source-provider.ts

+8
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,14 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
274274
settings.commit,
275275
settings.githubServerUrl
276276
)
277+
if (settings.configureUser) {
278+
if (!await git.configExists('user.name', true)) {
279+
await git.config('user.name', 'github-action[bot]', true)
280+
}
281+
if (!await git.configExists('user.email', true)) {
282+
await git.config('user.email', '41898282+github-actions[bot]@users.noreply.github.com', true)
283+
}
284+
}
277285
} finally {
278286
// Remove auth
279287
if (authHelper) {

‎src/git-source-settings.ts

+5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ export interface IGitSourceSettings {
7979
*/
8080
authToken: string
8181

82+
/**
83+
* Indicates whether to set a default user name and email in the local git config
84+
*/
85+
configureUser: boolean
86+
8287
/**
8388
* The SSH key to configure
8489
*/

‎src/input-helper.ts

+4
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ export async function getInputs(): Promise<IGitSourceSettings> {
138138
// Auth token
139139
result.authToken = core.getInput('token', {required: true})
140140

141+
// Configure user
142+
result.configureUser =
143+
(core.getInput('configure-user') || 'true').toUpperCase() === 'TRUE'
144+
141145
// SSH
142146
result.sshKey = core.getInput('ssh-key')
143147
result.sshKnownHosts = core.getInput('ssh-known-hosts')

0 commit comments

Comments
 (0)
Failed to load comments.