saitho / semantic-release-backmerge

:twisted_rightwards_arrows: semantic-release plugin to back-merge a release into a develop branch
https://saitho.github.io/semantic-release-backmerge/
56 stars 11 forks source link

Getting failed to push, tip of your current branch is behind #43

Open mehdihadeli opened 1 year ago

mehdihadeli commented 1 year ago

Hi @saitho, First thanks for this useful plugin :) I get below error when I push to my main branch and backmerge, merge this branch to my dev branch. My dev branch has some changes that not exists in main branch and I used Xours for merging in dev branch and prioritizing dev changes in auto merge. This is my config:

  - - '@saithodev/semantic-release-backmerge'
    - branches:
      - from: main
        to: dev
      backmergeStrategy: merge
      mergeMode: ours 
      clearWorkspace: false

Error:

[6:00:39 PM] [semantic-release] [@saithodev/semantic-release-backmerge] › ℹ  Found 0 staged files for back-merge commit
[6:00:39 PM] [semantic-release] [@saithodev/semantic-release-backmerge] › ℹ  Pushing backmerge to develop branch dev
[6:00:40 PM] [semantic-release] [@saithodev/semantic-release-backmerge] › ✘  Process aborted due to an error while backmerging a branch.
[6:00:40 PM] [semantic-release] [@saithodev/semantic-release-backmerge] › ✘  Error: Command failed with exit code 1: git push https://x-access-token:[secure]@github.com/my-repository HEAD:dev 
To https://github.com/my-repository
 ! [rejected]        HEAD -> dev (non-fast-forward)
error: failed to push some refs to 'https://github.com/my-repository'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    at makeError (/home/runner/.npm/_npx/facb4afc4d9f7a12/node_modules/execa/lib/error.js:60:11)
    at handlePromise (/home/runner/.npm/_npx/facb4afc4d9f7a12/node_modules/execa/index.js:118:26)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

I think the problem is using git checkout -B dev instead of git checkout dev in your code, and in your code -B doesn't pull the remote branch in the local (creates new branch), and we get error here because check branch doesn't have changes in the remote.

I tested following commands in my local and it works:

git checkout dev    
git merge origin/main -Xours
git push origin  HEAD:dev 

but when I use these commands like your approach and using -B flag, I get mentioned error:

git checkout -B dev 

Your branch and 'origin/dev' have diverged,
and have 1 and 15 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

git merge origin/main -Xours
git push origin HEAD:dev 

Error in my local:

 ! [rejected]        HEAD -> dev (non-fast-forward)
error: failed to push some refs to 'https://github.com/my-repository'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Here (with using git checkout -B dev) we need an extra git pull origin dev -Xours command after checkout and before push, to this works for me.

I think, this #36 issue has same problem

jagiella commented 1 year ago

I am having the same problem with the following config:

{
  "plugins": [
    "@semantic-release/commit-analyzer",
    "@semantic-release/release-notes-generator",
    ["@semantic-release/changelog", {
        "changelogFile": "CHANGELOG.md"
    }],
    ["@semantic-release/git", {
        "assets": ["CHANGELOG.md"]
    }],
    ["@semantic-release/gitlab", {
      "assets": [
        {"path": "gitlab-assets/*.pdf", "type": "other"}
      ]
    }],
    ["@semantic-release/exec", {
      "verifyReleaseCmd": "echo ${nextRelease.version} > VERSION && bash bintools/bump-version ${nextRelease.version}"
    }],
    ["@saithodev/semantic-release-backmerge", {
      "backmergeBranches": [
        {"from": "production", "to": "main"},
        {"from": "pre-production", "to": "main"}],
      "mergeMode": "ours"
    }]
  ],
  "branches": [
    {"name": "main", "prerelease": "dev"},
    {"name": "pre-production", "prerelease": "rc"},
    {"name": "production", "prerelease": false}
  ]
}

Releases on any of the triggering branches pre-production or production lead to failing back-merge attempts:

[4:28:19 PM] [semantic-release] [@saithodev/semantic-release-backmerge] › ✘  Process aborted due to an error while backmerging a branch.
[4:28:19 PM] [semantic-release] [@saithodev/semantic-release-backmerge] › ✘  Error: Command failed with exit code 1: git push [https://gitlab-ci-token:[secure]@REPO_URL.git](https://gitlab-ci-token:%5Bsecure%5D@REPO_URL.git) HEAD:main 
To https://REPO_URL.git
 ! [rejected]        HEAD -> main (non-fast-forward)
error: failed to push some refs to 'https://REPO_URL.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    at makeError (/builds/REPO/node_modules/execa/lib/error.js:60:11)
    at handlePromise (/build/REPO/node_modules/execa/index.js:118:26)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async file:///builds/REPO/node_modules/@saithodev/semantic-release-backmerge/dist/helpers/git.js:16:32
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit code 1