I'm trying to automate the git merge process using shell script and gitlab ci yaml file.
issue is, if the merge conflict occurs, git merge command fails and the entire scripts exits with non-zero exit status.
what I need is , if the merge conflicts happens, it OK, but atleast it should create the merge request before exiting the shell script . can this be possible ?
following are the commands :
script:
- pwd
- ls -al
- dir
- git fetch
- echo $CI_COMMIT_SHA
- echo "${GITLAB_USER_NAME}"
- echo "${GITLAB_USER_EMAIL}"
- git checkout hotfix_master
- git pull
- git checkout -b hotfix_master_$CI_COMMIT_SHA HEAD
- git fetch
- git remote -v
- git remote set-url origin https://oauth2:<token>@gitlab.com/xxx/xxx-project.git
- git remote -v
- git branch --set-upstream-to=origin/hotfix_master hotfix_master_$CI_COMMIT_SHA
- git push -u origin hotfix_master_$CI_COMMIT_SHA
- git diff --name-only origin/hotfix_master_$CI_COMMIT_SHA...$CI_COMMIT_SHA >> /tmp/difffile
- "cat /tmp/difffile"
- >
if [ -s /tmp/difffile ]; then
date +"%Y-%m-%d"
git merge origin/master $CI_COMMIT_SHA
git push https://gitlab_token1:<token>@gitlab.com/xxx/xxx-project.git hotfix_master_$CI_COMMIT_SHA -o merge_request.create -o merge_request.target=hotfix_master
else
echo "no merge"
fi
please suggest
EDIT 1
the script :
- git diff --name-only origin/hotfix_master_$CI_COMMIT_SHA...$CI_COMMIT_SHA >> /tmp/difffile
- "cat /tmp/difffile"
- >
if [ -s /tmp/difffile ]; then
date +"%Y-%m-%d"
git merge origin/master $CI_COMMIT_SHA || true
git push https://gitlab_token1:glpat-EsJ2H2esRiKGWqYz84Gr@gitlab.com/xxx/xxx-project.git hotfix_master_$CI_COMMIT_SHA -o merge_request.create -o merge_request.target=hotfix_master
else
echo "Merge is not required. master and hotfix_master are in sync"
fi
this is the output :
$ git diff --name-only origin/hotfix_master_$CI_COMMIT_SHA...$CI_COMMIT_SHA >> /tmp/difffile
$ cat /tmp/difffile
.gitlab-ci.yml
a.txt
b.txt
$ if [ -s /tmp/difffile ]; then # collapsed multi-line command
2021-12-24
Auto-merging b.txt
CONFLICT (content): Merge conflict in b.txt
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Automatic merge failed; fix conflicts and then commit the result.
Everything up-to-date
Cleaning up project directory and file based variables
00:01
Job succeeded