1. Problem Description
When a Webhook was configured to automatically trigger a Jenkins pipeline, the following error occurred:
| |
2. How Git Manages Versions
Git is a content-addressable filesystem. Git maintains a sha tree, and any historical node can be traced back to via its sha value. Let’s look at the commit records first:
Run the command:
| |
You can see that every time you commit, git generates a commitId, which is a sha. The parentheses immediately after the commitId explain the changes in the branch that the commit content belongs to.
So what do these (origin/master, origin/HEAD, fix_apiserver_devops) mean?
In .git/refs/, Git stores pointers to the commit objects that point to data (branches).
| |
| |
To Git, branches and tags are just pointers to a certain sha in the sha tree.
3. Cause Analysis
The chance of Jenkins or a related Git plugin being at fault is small; of course, you can also check the Git version. Using the controlled-variable method, first run the failing command outside of Jenkins.
| |
After understanding how Git works, the cause can probably be analyzed: people use branchName and tagName, while Git uses sha. reference not a tree means that the sha tree changed between the trigger stage and the execution stage. At trigger time, the sha corresponding to branchName did not exist at the execution stage. Below is some possible analysis based on scenarios others have encountered:
- Branch name and tag name are the same
The difference between a branch and a tag is that a branch can continue to receive commits, while a tag cannot be changed further. When the two have the same name, some odd behavior occurs, for example branchName disappearing.
- Caused by rebase
rebase is usually used to merge code from upstream, and also to merge multiple linear commits into one. For example, the command below merges the commits of the first 3 versions into 1:
| |
This causes commits to be lost, and the lost commit may have been exactly what triggered some action in Jenkins.
- After a PR is merged, the branch is deleted
Based on the idea of feature development, trunk integration, branch release, in the continuous integration process the best practice is to delete the branch immediately after the code is merged; GitHub has this flow wired up.
After a feature branch triggers a Jenkins pipeline, the branch is deleted, and when execution reaches pulling the code, an error is also reported.
- Subumodule
When the commit records of a sub-repository change, operations such as git update may also report errors.
- Not the latest code
Cached code was used, and the latest commit cannot be found on the old code.
Because usage and scenarios differ in Jenkins practice, unless the error is reproducible every time, troubleshooting needs to center on the crux of the problem and screen it out step by step with controlled variables.
