Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with branch being up to date in a monorepo #245

Open
thekevinscott opened this issue May 9, 2024 · 5 comments
Open

Issues with branch being up to date in a monorepo #245

thekevinscott opened this issue May 9, 2024 · 5 comments

Comments

@thekevinscott
Copy link

I have a monorepo with two packages, foo and bar. I have a pnpm-lock file at the root.

Changes to one or the other package work beautifully; however, I've set up changes to pnpm-lock to trigger a version for both packages. This fails because the branch is out of date:

runInWorkspace | command: git args: [
  'push',
  '***github.com/thekevinscott/MyRepo.git',
  '--follow-tags'
]
✖  fatal     To https://github.com/thekevinscott/MyRepo.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/thekevinscott/MyRepo.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

git exited with code 1
✖  fatal     Failed to bump version

I'm not sure the right way around this. Ideally I would do a pull, rebase, and then push. But I don't see support for that in the code.

I suppose my question is two-fold:

  1. Is there a recommended approach for working with monorepos that may version multiple packages at the same time?
  2. If not, would you accept a PR that modifies the script to first do a git pull if an option is specified?
@phips28
Copy link
Owner

phips28 commented May 13, 2024

the action works for others in monorepo afaik. I cant say much without seeing your action file.
when you run the action for both packages, then the action pushes the version for foo, and then when it runs at bar it will fail, because the checked-out repo is no longer up to date.
so you need to tell the action to not push, https://github.com/phips28/gh-action-bump-version?tab=readme-ov-file#skip-push and add a action step on the bottom to push the changes. I never used it in a monorepo myself. check the issues, maybe you find something related.

@douglasg14b
Copy link

I have the same problem, when this runs as part of a matrix and there are multiple packages updated, all packages after the first fail.

It even fails if a step is added before it to git pull which is weird. Perhaps this lib could include a flag that lets it either fast-forward or git pull before it makes it's commit & pushes it?

@thekevinscott
Copy link
Author

thekevinscott commented Jun 27, 2024

Forgot to update this thread.

This yaml works for my use case (this is in a shared action file):

jobs:
  version:
    runs-on: "ubuntu-latest"
    if: ${{ ! endsWith(github.event.head_commit.message, '[skip-version]') }}

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          persist-credentials: false

      - name: 'Automated Version Bump'
        id: version-bump
        uses: 'phips28/gh-action-bump-version@master'
        with:
          tag-prefix: ${{ inputs.tag_prefix }}
          skip-push:  'true'
        env:
          PACKAGEJSON_DIR:  ${{ inputs.package }}
          version-type:  'patch'

      - name: 'New Tag'
        env:
          NEW_TAG: ${{ steps.version-bump.outputs.newTag }}
        run: echo "new tag $NEW_TAG"

      - name: 'Pull, rebase, and push'
        run: |
          git remote set-url origin https://${{ secrets.GH_TOKEN}}@github.com/thekevinscott/my-repo.git
          git pull --rebase
          git push origin HEAD:main
          git push origin tag $NEW_TAG
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
          NEW_TAG: ${{ steps.version-bump.outputs.newTag }}

It's kind of a pain in the butt to get all the GITHUB_TOKEN secrets playing nicely but once that was sorted, this does the job for me.

I agree with @douglasg14b I would prefer to have this behavior behind a flag for simplicity's sake, but in lieu of that this issue could be marked closed for my use case.

@douglasg14b
Copy link

I also just discovered the reason this was not working for me:

  1. When using a matrix for version bumps, where each entry calls another workflow that performs it's own checkout, the concurrency needs to be set to 1
  2. Each run of the workflow that does the bumping needs to perform a git pull BEFORE the gh-actions-bump-version action runs.
    • If I do this after, I get conflicts and cant fast-forward, didn't try rebase though

@yuvalshilo
Copy link
Contributor

Same issue here after moving to a monorepo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants