How to push a git commit in Jenkins pipeline using Kubernetes and Git plugins

Jenkins setup:
Jenkins: 2.440.3
OS: Linux - 5.10.218-208.862.amzn2.x86_64
Java: 17.0.10 - Eclipse Adoptium (OpenJDK 64-Bit Server VM)
---
amazon-ecr:1.114.vfd22430621f5
analysis-model-api:12.3.0
antisamy-markup-formatter:162.v0e6ec0fcfcf6
apache-httpcomponents-client-4-api:4.5.14-208.v438351942757
apache-httpcomponents-client-5-api:5.3.1-1.0
asm-api:9.7-33.v4d23ef79fcc8
authentication-tokens:1.53.v1c90fd9191a_b_
authorize-project:1.7.1
aws-credentials:231.v08a_59f17d742
aws-java-sdk-ec2:1.12.696-451.v0651a_da_9ca_ec
aws-java-sdk-ecr:1.12.696-451.v0651a_da_9ca_ec
aws-java-sdk-minimal:1.12.696-451.v0651a_da_9ca_ec
blueocean:1.27.12
blueocean-autofavorite:1.2.5
blueocean-bitbucket-pipeline:1.27.12
blueocean-commons:1.27.12
blueocean-config:1.27.12
blueocean-core-js:1.27.12
blueocean-dashboard:1.27.12
blueocean-display-url:2.4.2
blueocean-events:1.27.12
blueocean-git-pipeline:1.27.12
blueocean-github-pipeline:1.27.12
blueocean-i18n:1.27.12
blueocean-jwt:1.27.12
blueocean-personalization:1.27.12
blueocean-pipeline-api-impl:1.27.12
blueocean-pipeline-editor:1.27.12
blueocean-pipeline-scm-api:1.27.12
blueocean-rest:1.27.12
blueocean-rest-impl:1.27.12
blueocean-web:1.27.12
bootstrap5-api:5.3.3-1
bouncycastle-api:2.30.1.77-225.v26ea_c9455fd9
branch-api:2.1163.va_f1064e4a_a_f3
build-user-vars-plugin:166.v52976843b_435
caffeine-api:3.1.8-133.v17b_1ff2e0599
checks-api:2.2.0
cloud-stats:336.v788e4055508b_
cloudbees-bitbucket-branch-source:883.v041fa_695e9c2
cloudbees-folder:6.901.vb_4c7a_da_75da_3
cobertura:1.17
code-coverage-api:4.99.0
command-launcher:107.v773860566e2e
commons-lang3-api:3.13.0-62.v7d18e55f51e2
commons-text-api:1.11.0-109.vfe16c66636eb_
config-file-provider:973.vb_a_80ecb_9a_4d0
configuration-as-code:1775.v810dc950b_514
configuration-as-code-groovy:1.1
copyartifact:722.v0662a_9b_e22a_c
coverage:1.14.0
credentials:1337.v60b_d7b_c7b_c9f
credentials-binding:657.v2b_19db_7d6e6d
cucumber-reports:5.8.1
data-tables-api:2.0.5-1
dependency-check-jenkins-plugin:5.5.0
display-url-api:2.200.vb_9327d658781
docker-commons:439.va_3cb_0a_6a_fb_29
docker-java-api:3.3.4-86.v39b_a_5ede342c
docker-workflow:572.v950f58993843
durable-task:555.v6802fe0f0b_82
ec2:1648.vf3d852e00486
echarts-api:5.5.0-1
envinject:2.908.v66a_774b_31d93
envinject-api:1.199.v3ce31253ed13
favorite:2.208.v91d65b_7792a_c
folder-properties:1.2.1
font-awesome-api:6.5.2-1
forensics-api:2.4.0
generic-webhook-trigger:2.2.0
git:5.2.1
git-client:4.7.0
git-parameter:0.9.19
git-server:114.v068a_c7cc2574
github:1.38.0
github-api:1.318-461.v7a_c09c9fa_d63
github-branch-source:1787.v8b_8cd49a_f8f1
gson-api:2.10.1-15.v0d99f670e0a_7
handy-uri-templates-2-api:2.1.8-30.v7e777411b_148
hidden-parameter:237.v4b_df26c7a_f0e
htmlpublisher:1.33
instance-identity:185.v303dc7c645f9
ionicons-api:70.v2959a_b_74e3cf
jackson2-api:2.17.0-379.v02de8ec9f64c
jacoco:3.3.6
jakarta-activation-api:2.1.3-1
jakarta-mail-api:2.1.3-1
javax-activation-api:1.2.0-6
javax-mail-api:1.6.2-9
jaxb:2.3.9-1
jdk-tool:73.vddf737284550
jenkins-design-language:1.27.12
jjwt-api:0.11.5-112.ve82dfb_224b_a_d
job-dsl:1.87
joda-time-api:2.12.7-29.v5a_b_e3a_82269a_
jquery:1.12.4-1
jquery3-api:3.7.1-2
jsch:0.2.16-86.v42e010d9484b_
json-api:20240303-41.v94e11e6de726
json-path-api:2.9.0-58.v62e3e85b_a_655
junit:1265.v65b_14fa_f12f0
keycloak:2.3.2
kubernetes:4203.v1dd44f5b_1cf9
kubernetes-cli:1.12.1
kubernetes-client-api:6.10.0-240.v57880ce8b_0b_2
kubernetes-credentials:0.11
mailer:472.vf7c289a_4b_420
matrix-auth:3.2.2
matrix-project:822.824.v14451b_c0fd42
metrics:4.2.21-449.v6960d7c54c69
mina-sshd-api-common:2.12.1-101.v85b_e08b_780dd
mina-sshd-api-core:2.12.1-101.v85b_e08b_780dd
minio:1.3.3-rc93.9e92f846d4cf
node-iterator-api:55.v3b_77d4032326
oic-auth:4.250.v5a_d993226437
okhttp-api:4.11.0-172.vda_da_1feeb_c6e
parameterized-scheduler:262.v00f3d90585cc
parameterized-trigger:787.v665fcf2a_830b_
pipeline-build-step:540.vb_e8849e1a_b_d8
pipeline-graph-analysis:216.vfd8b_ece330ca_
pipeline-groovy-lib:704.vc58b_8890a_384
pipeline-input-step:495.ve9c153f6067b_
pipeline-milestone-step:119.vdfdc43fc3b_9a_
pipeline-model-api:2.2198.v41dd8ef6dd56
pipeline-model-definition:2.2198.v41dd8ef6dd56
pipeline-model-extensions:2.2198.v41dd8ef6dd56
pipeline-rest-api:2.34
pipeline-stage-step:312.v8cd10304c27a_
pipeline-stage-tags-metadata:2.2198.v41dd8ef6dd56
pipeline-stage-view:2.34
pipeline-utility-steps:2.16.2
plain-credentials:179.vc5cb_98f6db_38
plugin-util-api:4.1.0
postbuildscript:3.2.0-550.v88192b_d3e922
prism-api:1.29.0-13
pubsub-light:1.18
scm-api:690.vfc8b_54395023
script-security:1335.vf07d9ce377a_e
slack:684.v833089650554
snakeyaml-api:2.2-111.vc6598e30cc65
sonar:2.17.2
sse-gateway:1.26
ssh-agent:367.vf9076cd4ee21
ssh-credentials:337.v395d2403ccd4
ssh-slaves:2.948.vb_8050d697fec
sshd:3.322.v159e91f6a_550
structs:337.v1b_04ea_4df7c8
token-macro:400.v35420b_922dcb_
trilead-api:2.142.v748523a_76693
uno-choice:2.8.3
validating-string-parameter:183.v3748e79b_9737
variant:60.v7290fc0eb_b_cd
warnings-ng:11.3.0
workflow-aggregator:596.v8c21c963d92d
workflow-api:1291.v51fd2a_625da_7
workflow-basic-steps:1049.v257a_e6b_30fb_d
workflow-cps:3894.vd0f0248b_a_fc4
workflow-durable-task-step:1336.v768003e07199
workflow-job:1400.v7fd111b_ec82f
workflow-multibranch:773.vc4fe1378f1d5
workflow-scm-step:427.v4ca_6512e7df1
workflow-step-api:657.v03b_e8115821b_
workflow-support:896.v175a_a_9c5b_78f

I am attempting to push a commit to a branch within a Jenkins pipeline and then I want to open a PR for this new branch into main.

I have encountered the following error:

+ git commit -m '[Jenkins] Updating build-tools to version 21-alpine3.20'
Author 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 'jenkins@docker-build-ifg8d-jwmq2-bkgb4.(none)')

I am using the Kubernetes plugin to dynamically create a new pod for each pipeline job and the git plugin for git features (the docker-build-{random-characters} is a randomly generated string to give each pod a unique name).

I have a user.name and user.email set within the “Git plugin” section within “Manage Jenkins”, but it doesn’t look like these values are pulled through successfully, when I attempt to print(env.GIT_COMMITER_NAME) or some other envVar included with the Git plugin, I just get null.

I tried doing a git config --global user.name Jenkins when SSHing into my Jenkins controller and cat ~/.gitconfig to check this was added and it was, I still got the same error in the pipeline.

The only thing I have found that works is executing the git config --global user.name Jenkins within the pipeline script.
This is ok because it actually works (yay), but it just seems wrong to need to do this. I thought the Git plugin would take care of this, thats why there are text inputs in “Manage Jenkins” > “Git plugin”.
I am thinking about creating a shared function so that anyone in my team can just add gitUtils.setGitConfig() into their pipeline that contains the 2 shell commands to set user.name and user.email but again, I don’t think this should be needed.

Am I missing something? I think maybe the Git plugin is broken (because the envVars come out as null), or it doesnt work well with the Kubernetes plugin, due to the error message obtained: (maybe the git plugin doesn’t work across dynamically created k8s pods?)

fatal: unable to auto-detect email address (got 'jenkins@docker-build-ifg8d-jwmq2-bkgb4.(none)')

Those values are used when the git plugin performs a commit from within the git plugin code. Since you’re calling command line git from your own shell step or bat step or powershell step, it is assumed that you’ll provide the necessary configuration.

The config file provider plugin can deliver configuration files to agent workspaces.

So I need to provide a .gitconfig file for the code block? Is that what you’re saying?

Where is the config file going to come from when the jenkins agent is span up automatically using the Kubernetes plugin?

That’s very close to what I’m saying. I’m saying that if you’re using a shell, bat, or powershell script to call command line git, then you need to configure things so that command line git will work. The user account of the agent that you’re running does not have a user level configuration for command line git’s user.name or user.email. You need to configure those settings. You can do it in one of several locations with one of several different methods:

  • Repository configuration - set user.name and user.email in the git configuration of the repository where the operations are being performed. Use this if method if different repositories need different values. This is the easiest to implement since the statements can be placed in the same sh, bat, or powershell step that is calling command line git.
    Use git config user.name 'My User Name'
    Use git config user.email me@example.com
  • User account configuration - set user.name and user.email in the .gitconfig file of the user that runs the agent. Use this configuration if the user account is always used for one purpose
    Use git config --global user.name 'My User Name'
    Use git config --global user.email me@example.com
  • Operating system configuration - set user.name and user.email in /etc/gitconfig if this operating system will only be used by one user (for example, a Docker container). Usually requires root user permissions
    Use git config --system user.name 'My User Name'
    Use git config --system user.email me@example.com

You can do that in the same shell (or bat or powershell) step that calls command line git or you can do it with the config file provider plugin or you can do it when defining the container image for the agent.

The config file provider plugin documentation describes how to use it to deliver configuration files to agents.

I tried passing in a git config file with the config file provider plugin and I still got the same results, so I’m still stuck with requiring to do a git config --global user.name Jenkins but I’m ok with that now.

Would be nice if there was a way to globally set this without requiring shell steps to set it or passing in a config file, even if it was by SSHing into Jenkins and setting it within Jenkins controller somehow.