jenkinsci / jenkinsci/git-plugin

[JENKINS-60461] GIT_COMMIT shouldn't be set when scm git failed on a wrong treeish

Open
#3,501 1 comment 0 reactions 0 assignees View on GitHub
component:git-plugin imported-jira-issue priority:major resolution:unresolved
Dominant language
Java
Stars
694
Forks
1.1k
Avg merge
1h 29m
Merged PRs (30d)
3

Description

When a job is started & trying to checkout an unavailable commitId on a valid repository, then the GIT_COMMIT environment variable is still set but to the last fetched commit value.

As side effect, all post job steps or plugin working later will get a wrong value from the GIT_COMMIT & act on that wrong commit (github status events, etc...).

When a scm checkout fails & stop the build, the GIT_COMMIT value should not be set.


How to reproduce


scenario 1 in one step( see One step config.xml attached file)

This one will fail even on a clean/new workspace if nothing was initially fetched or if everything was wiped out



  • just run "build with parameters" & set "unvalid treeish value" as TREEISH value

Started by user unknown or anonymous

Rebuilds build #12

Running as SYSTEM

[EnvInject] - Loading node environment variables.

Building in workspace c:\w\test wrong commit_id

No credentials specified

Wiping out workspace first.

Cloning the remote Git repository

Cloning repository https://github.com/github/gitignore

> git.exe init c:\w\test wrong commit_id # timeout=10

Fetching upstream changes from https://github.com/github/gitignore

> git.exe --version # timeout=10

> git.exe fetch --tags --progress https://github.com/github/gitignore +refs/heads/:refs/remotes/origin/ # timeout=180

> git.exe config remote.origin.url https://github.com/github/gitignore # timeout=10

> git.exe config --add remote.origin.fetch +refs/heads/:refs/remotes/origin/ # timeout=10

> git.exe config remote.origin.url https://github.com/github/gitignore # timeout=10

Fetching upstream changes from https://github.com/github/gitignore

> git.exe fetch --tags --progress https://github.com/github/gitignore +refs/heads/:refs/remotes/origin/ # timeout=180

> git.exe rev-parse "origin/unvalid treeish value^

Unknown macro: {commit}

" # timeout=10

> git.exe rev-parse "unvalid treeish value^

" # timeout=10

ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.

GIT COMMIT is: 21e9f06539cdbc1ccbbb2ce59cd667be3e172fc8

Finished: FAILURE


GIT_COMMIT is set anyway to 21e9f06539cdbc1ccbbb2ce59cd667be3e172fc8 ! shouldn't be set !


scenario 2, restarting a build by setting a wrong TREEISH the second time (attached sample config 1.xml file)



  1. Run the job a first time on master to initialize the workspace

    Started by an SCM change

    Running as SYSTEM

    [EnvInject] - Loading node environment variables.

    Building in workspace c:\w\test wrong commit_id

    No credentials specified

    > git.exe rev-parse --is-inside-work-tree # timeout=10

    Fetching changes from the remote Git repository

    > git.exe config remote.origin.url https://github.com/github/gitignore # timeout=10

    Fetching upstream changes from https://github.com/github/gitignore

    > git.exe --version # timeout=10

    > git.exe fetch --tags --progress https://github.com/github/gitignore +refs/heads/:refs/remotes/origin/ # timeout=10

    > git.exe rev-parse "origin/master^

    Unknown macro: {commit}

    " # timeout=10

    Checking out Revision 21e9f06539cdbc1ccbbb2ce59cd667be3e172fc8 (origin/master)

    > git.exe config core.sparsecheckout # timeout=10

    > git.exe checkout -f 21e9f06539cdbc1ccbbb2ce59cd667be3e172fc8 # timeout=10

    Commit message: "Added jarRepositories.xml as an optional ignore (#3254)"

    First time build. Skipping changelog.

    GIT COMMIT is: 21e9f06539cdbc1ccbbb2ce59cd667be3e172fc8

    Finished: SUCCESS


  2. Run a second time with a bad treeish value "unvalid treeish value"

    Started by user unknown or anonymous

    Rebuilds build #8

    Running as SYSTEM

    [EnvInject] - Loading node environment variables.

    Building in workspace c:\w\test wrong commit_id

    No credentials specified

    > git.exe rev-parse --is-inside-work-tree # timeout=10

    Fetching changes from the remote Git repository

    > git.exe config remote.origin.url https://github.com/github/gitignore # timeout=10

    Fetching upstream changes from https://github.com/github/gitignore

    > git.exe --version # timeout=10

    > git.exe fetch --tags --progress https://github.com/github/gitignore +refs/heads/:refs/remotes/origin/ # timeout=10

    > git.exe rev-parse "origin/unvalid treeish value^

    Unknown macro: {commit}

    " # timeout=10

    > git.exe rev-parse "unvalid treeish value^

    " # timeout=10

    ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.

    GIT COMMIT is: 21e9f06539cdbc1ccbbb2ce59cd667be3e172fc8

    Finished: FAILURE


    The GIT_COMMIT env var is set anyway to the previous active fetched versoin of the previous build ! instead of not being set as the checkout out failed.


 

Extra info

Groovy Post build step to display the GIT_COMMIT env value:

def v = manager.envVars.get("GIT_COMMIT")

manager.listener.logger.println("GIT COMMIT is: "+v)

Thx in advance,

Best regards

---
Originally reported by llecaroz, imported from: GIT_COMMIT shouldn't be set when scm git failed on a wrong treeish


  • status: Open
  • priority: Major
  • component(s): git-plugin
  • label(s): git, jenkins, plugins
  • resolution: Unresolved
  • votes: 0
  • watchers: 2
  • imported: 2025-12-02

Raw content of original issue

When a job is started & trying to checkout an unavailable commitId on a valid repository, then the GIT_COMMIT environment variable is still set but to the last fetched commit value.

As side effect, all post job steps or plugin working later will get a wrong value from the GIT_COMMIT & act on that wrong commit (github status events, etc...).

When a scm checkout fails & stop the build, the GIT_COMMIT value should not be set.


How to reproduce


scenario 1 in one step( see One step config.xml attached file)

This one will fail even on a clean/new workspace if nothing was initially fetched or if everything was wiped out



  • just run "build with parameters" & set "unvalid treeish value" as TREEISH value

Started by user unknown or anonymous
Rebuilds build #12
Running as SYSTEM
[EnvInject] - Loading node environment variables.
Building in workspace c:\w\test wrong commit_id
No credentials specified
Wiping out workspace first.
Cloning the remote Git repository
Cloning repository https://github.com/github/gitignore
> git.exe init c:\w\test wrong commit_id # timeout=10
Fetching upstream changes from https://github.com/github/gitignore
> git.exe --version # timeout=10
> git.exe fetch --tags --progress https://github.com/github/gitignore +refs/heads/:refs/remotes/origin/ # timeout=180
> git.exe config remote.origin.url https://github.com/github/gitignore # timeout=10
> git.exe config --add remote.origin.fetch +refs/heads/:refs/remotes/origin/ # timeout=10
> git.exe config remote.origin.url https://github.com/github/gitignore # timeout=10
Fetching upstream changes from https://github.com/github/gitignore
> git.exe fetch --tags --progress https://github.com/github/gitignore +refs/heads/:refs/remotes/origin/ # timeout=180
> git.exe rev-parse "origin/unvalid treeish value^
Unknown macro: {commit}
" # timeout=10
> git.exe rev-parse "unvalid treeish value^

" # timeout=10
ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.
GIT COMMIT is: 21e9f06539cdbc1ccbbb2ce59cd667be3e172fc8
Finished: FAILURE


GIT_COMMIT is set anyway to 21e9f06539cdbc1ccbbb2ce59cd667be3e172fc8 ! shouldn't be set !


scenario 2, restarting a build by setting a wrong TREEISH the second time (attached sample config 1.xml file)



  1. Run the job a first time on master to initialize the workspace

    Started by an SCM change
    Running as SYSTEM
    [EnvInject] - Loading node environment variables.
    Building in workspace c:\w\test wrong commit_id
    No credentials specified
    > git.exe rev-parse --is-inside-work-tree # timeout=10
    Fetching changes from the remote Git repository
    > git.exe config remote.origin.url https://github.com/github/gitignore # timeout=10
    Fetching upstream changes from https://github.com/github/gitignore
    > git.exe --version # timeout=10
    > git.exe fetch --tags --progress https://github.com/github/gitignore +refs/heads/:refs/remotes/origin/ # timeout=10
    > git.exe rev-parse "origin/master^
    Unknown macro: {commit}
    " # timeout=10
    Checking out Revision 21e9f06539cdbc1ccbbb2ce59cd667be3e172fc8 (origin/master)
    > git.exe config core.sparsecheckout # timeout=10
    > git.exe checkout -f 21e9f06539cdbc1ccbbb2ce59cd667be3e172fc8 # timeout=10
    Commit message: "Added jarRepositories.xml as an optional ignore (#3254)"
    First time build. Skipping changelog.
    GIT COMMIT is: 21e9f06539cdbc1ccbbb2ce59cd667be3e172fc8
    Finished: SUCCESS


  2. Run a second time with a bad treeish value "unvalid treeish value"

    Started by user unknown or anonymous
    Rebuilds build #8
    Running as SYSTEM
    [EnvInject] - Loading node environment variables.
    Building in workspace c:\w\test wrong commit_id
    No credentials specified
    > git.exe rev-parse --is-inside-work-tree # timeout=10
    Fetching changes from the remote Git repository
    > git.exe config remote.origin.url https://github.com/github/gitignore # timeout=10
    Fetching upstream changes from https://github.com/github/gitignore
    > git.exe --version # timeout=10
    > git.exe fetch --tags --progress https://github.com/github/gitignore +refs/heads/:refs/remotes/origin/ # timeout=10
    > git.exe rev-parse "origin/unvalid treeish value^
    Unknown macro: {commit}
    " # timeout=10
    > git.exe rev-parse "unvalid treeish value^

    " # timeout=10
    ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.
    GIT COMMIT is: 21e9f06539cdbc1ccbbb2ce59cd667be3e172fc8
    Finished: FAILURE


    The GIT_COMMIT env var is set anyway to the previous active fetched versoin of the previous build ! instead of not being set as the checkout out failed.


 

Extra info

Groovy Post build step to display the GIT_COMMIT env value:



def v = manager.envVars.get("GIT_COMMIT")

manager.listener.logger.println("GIT COMMIT is: "+v)


Thx in advance,
Best regards

2 attachments

- [One step config.xml](https://issues.jenkins.io/secure/attachment/49742/One%20step%20config.xml)
- [sample config 1.xml](https://issues.jenkins.io/secure/attachment/49743/sample%20config%201.xml)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.