0

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
user2315104
  • 2,378
  • 7
  • 35
  • 54
  • https://stackoverflow.com/search?q=%5Bshell%5D+ignore+error – phd Dec 24 '21 at 20:50
  • `git merge || true` + `git commit` or just `git merge || git commit` – phd Dec 24 '21 at 20:50
  • Thanks . i added the || true after git merge command , still the issue is there . please see EDIT 1 – user2315104 Dec 24 '21 at 21:23
  • "*Everything up-to-date*" is from `git push` after `merge`. You need to add and commit the failed files after `merge` then `push`. – phd Dec 24 '21 at 21:35
  • I have two files in repo and both are in conflicting state . so , i don't have anything as such to commit and add after git merge unless I resolve the conflicts . can I still create merge request through script before resolving conflicts ? – user2315104 Dec 24 '21 at 21:52
  • Wat do you want to request in the merge request? There must be at least one commit and you don't want to commit merge conflicts; then what? – phd Dec 24 '21 at 23:39

0 Answers0