I create NEW branch B from branch A.
In gitlab CICD yaml $CI_COMMIT_REF_NAME="B"
(TARGET branch, the new branch),
but how-to get the SOURCE name branch (A)
I create NEW branch B from branch A.
In gitlab CICD yaml $CI_COMMIT_REF_NAME="B"
(TARGET branch, the new branch),
but how-to get the SOURCE name branch (A)
Can you explain, what source branch you need?
$CI_COMMIT_BRANCH
is the branch, that you commit → which is why the pipeline is running if not triggered.
See: Predefined CI/CD variables reference | GitLab
To get the upstream branch maybe try git if its possible.
$CI_COMMIT_BRANCH
will be the target branch: release/0.1.0
we need the source branch, the value feature/component-releasemanagement
That is not possible sadly, since git does not save the source branch.
What you can do is compare the git commit history but its heuristic and therefore not definitive → if you add commits to the source branch, this algorithm does not work, therefore i would not recommend it.
Maybe using:
git log --oneline --decorate --graph -n 5
results:
Current branch release/0.1.52
Source branch: origin/feature/component-releasemanagement
for branch in $(git branch -r | grep -v '\->'); do
git merge-base --is-ancestor $branch $CI_COMMIT_SHA && echo "Branch source: $branch"
done
results
Branch source: origin/feature/component-releasemanagement
Branch source: origin/release/0.1.35
Branch source: origin/release/0.1.38
Branch source: origin/release/0.1.39
Branch source: origin/release/0.1.42
Branch source: origin/release/0.1.44
Branch source: origin/release/0.1.47
Branch source: origin/release/0.1.48
Branch source: origin/release/0.1.49
Branch source: origin/release/0.1.50
Branch source: origin/release/0.1.51
Branch source: origin/release/0.1.52
echo " 1) fetch"
git fetch --all
results
1) fetch
From https://gitlab.xxxxxx.es/zzzz/devops/ci-components/releasemanagement
885b10d..0b6b547 feature/component-releasemanagement -> origin/feature/component-releasemanagement
troubleshooting
git rev-list --max-parents=0 HEAD
git rev-list HEAD
git rev-parse HEAD
git rev-parse --short $CI_COMMIT_SHA
git rev-parse $CI_COMMIT_SHA
short_sha=$(git rev-parse --short $CI_COMMIT_SHA)
echo "Using short SHA: $short_sha"
git branch --contains $(git rev-list --max-parents=0 HEAD)
echo "git branch -r --contains $CI_COMMIT_SHA"
git branch -r --contains $CI_COMMIT_SHA
# Lista los commits recientes en el branch actual y encuentra el merge-base
echo "git log"
git log --oneline --decorate --graph -n 5
# Lista los commits recientes en el branch actual
#git log --oneline
echo "git rev-parse"
# Mostrar el commit id más reciente en la rama actual
git rev-parse HEAD
echo "script branch"
for branch in $(git branch -r | grep -v '\->'); do
git merge-base --is-ancestor $branch $CI_COMMIT_SHA && echo "Branch source: $branch"
done
#source_branch=$(git branch --contains $(git rev-list --max-parents=0 HEAD) | grep -v HEAD | grep -v ${CI_COMMIT_REF_NAME} | head -n 1)
#echo "Branch source: $source_branch"
echo "revisar el historial de commits "
git log --oneline --decorate --graph --all
echo "git reflog show --all"
git reflog show --all
# Traer todos los branches remotos
# git fetch origin
# git fetch --all
# git status
echo "git merge-base origin"
#git merge-base origin/master $CI_COMMIT_SHA FALLA *********
#fatal: Not a valid object name origin/main
echo " 1) fetch"
git fetch --all
echo " 2) branch -r"
git branch -r
echo " 2) GREP"
git branch -r | grep 'origin/master'
echo " 3) git log"
git log --oneline
echo " 3) git log"
git log --pretty=oneline
echo " 3) GREP $CI_COMMIT_SHA"
git show $CI_COMMIT_SHA
git log --oneline | grep $CI_COMMIT_SHA || echo "Commit $CI_COMMIT_SHA not found in log"
git log --oneline | grep $short_sha || echo "Commit $CI_COMMIT_SHA not found in log"
#git log --oneline | grep $CI_COMMIT_SHA
If you know the name of the branch, you could also set it in the CI/CD variables. That would not be dynamic at all though
ChatGPT gives me:
find_source_branch:
stage: find_source_branch
script:
- git fetch --all
- for branch in $(git for-each-ref --format='%(refname:short)' refs/heads/); do
echo "Comparing with $branch";
git rev-list --left-right --count $branch...HEAD;
done | sort -n | head -n 1
(not tested!)
Not works
Troubleshooting:
git for-each-ref --format='%(refname:short)' refs/heads/)
returns NOTHING
git fetch -all returns:
From https://gitlab.xxx.es/zzz/devops/ci-components/releasemanagement
66dbdd4..374436f feature/component-releasemanagement -> origin/feature/component-releasemanagement
git branch returns:
* (HEAD detached at 374436f)
git log returns: (first line)
* 374436f (HEAD, origin/release/0.1.57, origin/feature/component-releasemanagement) git branch1
What is HEAD detached?
Create New branch in Gitlab
source: feature/component-releasemanagement
target: release/0.1.57
new branch in remote only, not in local?
* 374436f (HEAD, origin/release/0.1.57, origin/feature/component-releasemanagement) git branch1
Has 2 branches: origin/release/0.1.57, origin/feature/component-releasemanagement
differences: remotes/origin/release/0.1.62 and origin/release/0.1.62
git for-each-ref --format='%(upstream:short)' refs/heads/$CI_COMMIT_BRANCH returns nothing
git rev-parse --abbrev-ref --symbolic-full-name @{u} returns:
error fatal: HEAD does not point to a branch
git Status returns:
HEAD detached at 61c91bb
nothing to commit, working tree clean
git branch -vv returns:
* (HEAD detached at 61c91bb) 61c91bb git branch -vv
git branch returns:
* (HEAD detached at 61c91bb)
git branch -a returns::
* (HEAD detached at 3825c23)
remotes/origin/dev/0.1.0
remotes/origin/develop
remotes/origin/feature/component-releasemanagement
remotes/origin/master
remotes/origin/release/0.1.0
remotes/origin/release/0.1.10
...
remotes/origin/release/0.1.162
branch local: release/0.1.57 - remote: origin/release/0.1.57
git fetch origin y después git log --oneline --graph --decorate release/0.1.57..origin/release/0.1.57 returns:
fatal: ambiguous argument 'release/0.1.57..origin/release/0.1.57': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'