Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(git config): Set default user.name and user.email in git config #2012

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,6 @@ jobs:
- run: |
date > generated.txt
# Note: the following account information will not work on GHES
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add .
git commit -m "generated"
git push
Expand All @@ -305,8 +303,6 @@ jobs:
- run: |
date > generated.txt
# Note: the following account information will not work on GHES
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add .
git commit -m "generated"
git push
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ inputs:
[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)
default: ${{ github.token }}
configure-user:
description: >
Whether to configure user.name and user.email in the local git config.
This is required to push a commit from a Github Action Workflow.
Set to `false` to disable the config.
default: true
ssh-key:
description: >
SSH key used to fetch the repository. The SSH key is configured with the local
Expand Down
3 changes: 3 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,9 @@ function getInputs() {
core.debug(`recursive submodules = ${result.nestedSubmodules}`);
// Auth token
result.authToken = core.getInput('token', { required: true });
// Configure user
result.configureUser =
(core.getInput('configure-user') || 'true').toUpperCase() === 'TRUE'
// SSH
result.sshKey = core.getInput('ssh-key');
result.sshKnownHosts = core.getInput('ssh-known-hosts');
Expand Down
8 changes: 8 additions & 0 deletions src/git-source-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
settings.commit,
settings.githubServerUrl
)
if (settings.configureUser) {
if (!await git.configExists('user.name', true)) {
await git.config('user.name', 'github-action[bot]', true)
}
if (!await git.configExists('user.email', true)) {
await git.config('user.email', '41898282+github-actions[bot]@users.noreply.github.com', true)
}
}
} finally {
// Remove auth
if (authHelper) {
Expand Down
5 changes: 5 additions & 0 deletions src/git-source-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ export interface IGitSourceSettings {
*/
authToken: string

/**
* Indicates whether to set a default user name and email in the local git config
*/
configureUser: boolean

/**
* The SSH key to configure
*/
Expand Down
4 changes: 4 additions & 0 deletions src/input-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ export async function getInputs(): Promise<IGitSourceSettings> {
// Auth token
result.authToken = core.getInput('token', {required: true})

// Configure user
result.configureUser =
(core.getInput('configure-user') || 'true').toUpperCase() === 'TRUE'

// SSH
result.sshKey = core.getInput('ssh-key')
result.sshKnownHosts = core.getInput('ssh-known-hosts')
Expand Down