Skip to content
Snippets Groups Projects

Add CI_COMMIT_FORCE_PUSH variable

Open Tyler Kellen requested to merge scaleoutllc/gitlab:detect-force-push into master
8 unresolved threads

What does this MR do and why?

This (untested) change exposes a variable to CI pipelines that allow conditional branching when a force push occurs. The current version of gitlab incorrectly determines the list of files changed when a force push occurs. It would be possible to work around this incorrect handling by giving pipeline authors a variable for branching when a force push has occurred. This MR implements that variable.

How to set up and validate locally

Configure a .gitlab-ci.yml file with the following:

test:
  stage: test
  script:  
  - echo $CI_COMMIT_FORCE_PUSH

Run the following:

git init
touch foo
git add .
git commit -m "foo"
touch bar
git commit -m "bar"
git push origin main
git reset --hard HEAD~1
touch baz
git add .
git commit -m "baz"
git push origin main --force

Observe that the final push triggers a pipeline wherein CI_COMMIT_FORCE_PUSH is set to "true".

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Marcel Amirault

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • Marcel Amirault changed the description

    changed the description

  • requested review from @jocelynjane and @morefice

  • Marcel Amirault removed review request for @marcel.amirault

    removed review request for @marcel.amirault

  • Jocelyn Eillis approved this merge request

    approved this merge request

  • Mark Nuzzo requested review from @alberts-gitlab

    requested review from @alberts-gitlab

  • Note to @alberts-gitlab, I have not actually run this code to test the validation steps provided in the MR description. Given the tiny size of the changeset and limited time on my part, I was hopeful that a reviewer who runs Gitlab in development more regularly could validate what I have done.

  • Tyler Kellen added 2 commits

    added 2 commits

    • 376c7ea3 - Add predefined variable CI_COMMIT_FORCE_PUSH.
    • 349dbdcb - Add expected version number for CI_COMMIT_FORCE_PUSH.

    Compare with previous version

  • Tyler Kellen added 550 commits

    added 550 commits

    • 349dbdcb...1cc5d94b - 548 commits from branch gitlab-org:master
    • 9c24dcdf - Add predefined variable CI_COMMIT_FORCE_PUSH.
    • 335476d7 - Add expected version number for CI_COMMIT_FORCE_PUSH.

    Compare with previous version

  • Tyler Kellen added 2 commits

    added 2 commits

    • 762249ee - Add predefined variable CI_COMMIT_FORCE_PUSH.
    • f5af0d2c - Add expected version number for CI_COMMIT_FORCE_PUSH.

    Compare with previous version

    • @morefice, @alberts-gitlab, @jocelynjane, this Community contribution was recently assigned to you for review.

      • Do you still have capacity to review this? We are mindful of your time, so if you are not able to take this on, please re-assign to one or more other reviewers.
      • Add the workflowin dev label if the merge request needs action from the author.

      This message was generated automatically. You're welcome to improve it.

    • @morefice - please feel free to reassign - not sure what the capacity distribution is on the team.

    • @alberts-gitlab @iamricecake @dbiryukov

      Thoughts about introducing this new variable?

    • I'd like to get a better understanding of the reason for this suggested new variable:

      The current version of gitlab incorrectly determines the list of files changed when a force push occurs.

      If this is truly a bug, then maybe we should fix this instead? Or if this is by design and by intention, then we can add the CI variable for special cases depending on what users need. Who would be the rightful group to confirm this behavior?

    • I agree with @iamricecake.

      The current version of gitlab incorrectly determines the list of files changed when a force push occurs.

      This new variable seems to be a workaround for the bug referred in #357394.

      I'm hesitant to add a new variable simply for a workaround, because it will need to be maintained. I think we should also look at the use case described in #357394 and solve that problem. In that issue, the user uses CI_COMMIT_BEFORE_SHA to get a list of files that have been changed. I tried to get more information in this comment, as to what is the expected list of file change when a force push happens, but haven't got a reply yet.

    • Please register or sign in to reply
    • @iamricecake / @alberts-gitlab I can appreciate your shared concern around maintaining another variable over the long term. At the same time, I would like to request that you consider the complexity of the variable itself. It makes accessible a boolean value that is retrieved using a function that is already tested maintained for numerous other purposes. Would you agree it is a fair to say that the maintenance burden is rather small?

      As it pertains to fixing the underlying issue with the list of files changed for force pushes, the resolution cascades back to a technical decision Gitlab has already given a lot of consideration to--the decision to set CI_COMMIT_BEFORE_SHA to all zeros on initial pushes to branches. It's my understanding this approach was chosen because without a MR there isn't a definitive source branch to compare to. The question of what to compare to is the same for a force push, in my estimation.

      If force pushes set CI_COMMIT_BEFORE_SHA to all zeros, that would resolve the primary use case I need to solve using CI_COMMIT_FORCE_PUSH (controlling the value sent to compare_to to ensure pipelines behave in manner consistent with my client's specific use cases). However, the abstraction would be leaky insomuch as pipeline authors would be unable to differentiate between force pushes and initial pushes of a new branch. I don't personally need to differentiate between them, but I can imagine other pipeline authors might.

    • @wescaleout After reviewing the Gitlab::Checks::ForcePush.force_push? implementation, I think I'm ok with adding this variable. However, I have one comment on the variable name.

      Could you also please add the relevant tests? For more detail, please refer to the test guidelines: https://docs.gitlab.com/ee/development/testing_guide/

      @iamricecake what do you think?

      Edited by Albert
    • Please register or sign in to reply
  • 47 47 variables.append(key: 'CI_COMMIT_SHA', value: pipeline.sha)
    48 48 variables.append(key: 'CI_COMMIT_SHORT_SHA', value: pipeline.short_sha)
    49 49 variables.append(key: 'CI_COMMIT_BEFORE_SHA', value: pipeline.before_sha)
    50 variables.append(key: 'CI_COMMIT_FORCE_PUSH', value: pipeline.force_push?.to_s)
    • Would CI_COMMIT_CHANGED_HISTORY be more appropriate? Technically a git push --force may not necessarily change the history.

      I'm aware the code implementation is called Gitlab::Checks::ForcePush.force_push?, but it actually checks for changes in history. We can keep that for the time being, but we should use the more appropriate name for the variable. Once the variable is in place, we cannot change it easily.

    • Technically a git push --force may not necessarily change the history.

      @alberts-gitlab is it actually possible to push force without changing history? When I tried it, it seems it didn't push to remote at all and just responded with Everything up-to-date.

    • @iamricecake Given the chain A -> B -> C locally, and remote has A -> B, wouldn't git push --force just pushes C as normal?

    • wouldn't git push --force just pushes C as normal?

      @alberts-gitlab oh, do you mean when force pushing a new branch?

    • Please register or sign in to reply
  • Albert removed review request for @alberts-gitlab

    removed review request for @alberts-gitlab

  • Max Orefice removed review request for @morefice

    removed review request for @morefice

  • Jocelyn Eillis removed review request for @jocelynjane

    removed review request for @jocelynjane

    • @wescaleout, it seems we're waiting on an action from you for approximately two weeks.

      This message was generated automatically. You're welcome to improve it.

    • I have tried running the GDK to implement the tests requested several times. Each time I have moved on to other tasks because the provisioning steps of the Vagrantfile fail and I haven't been willing to dedicate the time to debugging why (nor the time required to open an issue describing the failures).

      This feature is fairly important from my perspective but the barrier of entry to writing tests is currently too high to justify the level of effort.

      If anyone monitoring the comments of this thread already has a functioning development environment I would deeply appreciate their willingness to confirm the code works and to drop a few tests in. The level of effort for actually implementing tests should be incredibly small.

      Please leave this MR open. I will get to it eventually if someone else doesn't first.

    • @alberts-gitlab could you help Tyler out with tests here?

    • Would still love to see this feature land. Can you please help @alberts-gitlab? I know that writing tests for this functionality would be a very, very easy job.

      Unfortunately I am still unable to commit the time to getting the local gitlab development environment running.

    • Hi @wescaleout I'm following up this and see if you still need time or help to write tests? Here is some information that might be useful:

      • Here is a similar MR that adds the variable $CI_PIPELINE_NAME.

        I think probably you will need to add the tests to spec/lib/gitlab/ci/variables/builder/pipeline_spec.rb and commit the change to gitlab.com so that the pipeline can run the tests for you if you're having issues setting up the local development environment (if you are running out of free build time, let me know and I can run the pipeline for you).

      • If you are still interested in setting up the local development environment, you can take a look at the GitLab Development Kit (GDK) and follow the instruction to install one.

      Let me know if you have other questions, I'm more than happy to answer them for you :grin:

      Thank you again for your contribution!

    • Hi @tianwenchen! Thank you for taking the time to reply, sorry for my tardy response.

      I would really like this feature to be landed. I've just rebased this branch against the latest master and added what I believe to be a minimal test to validate the presence of CI_FORCE_PUSH.

      It's unclear to me in the CI pipelines which job is the one actually performing the test I changed.

      If you'd be willing to have a look I would appreciate it greatly!

      Edited by Tyler Kellen
    • @iamricecake can you help out this community contribution? We've been stuck on failing tests for a long time now, and for such a small change I'd hate to see this linger in limbo for much longer.

    • <3 an assist here would be hugely helpful! I am well capable of writing tests but I must admit I haven't been able to justify the level of effort required for getting local development functioning in order to do so. Please, anyone who can actually run the test suite, help!

    • @wescaleout apologies, I wasn't able to get to this today, will do tomorrow. Will need to refresh my memory on the pending discussions on this MR.

      cc @shampton

    • Please register or sign in to reply
  • 🤖 GitLab Bot 🤖 added sectionci label and removed sectionops label

    added sectionci label and removed sectionops label

  • added idle label

  • 🤖 GitLab Bot 🤖 added stale label and removed idle label

    added stale label and removed idle label

  • Hi @shampton! :wave:

    To provide a better contribution experience, we're identifying older merge requests with no human activity in the past 120 days. Our goal is to bring these merge requests to completion or close them, enabling us to spend more time on active merge requests.

    Review this merge request and determine if you should:

    • Work on the provided feedback. Add @gitlab-bot ready when you need a review or are looking for more feedback.
    • Don't know how to proceed? Ask for help from a merge request coach by adding @gitlab-bot help in a comment.
    • Close the merge request.

    Please see the handbook for additional details on this

    (Improve this message?)

  • removed stale label

  • added idle label

  • removed idle label

  • added idle label

  • Tyler Kellen added 46947 commits

    added 46947 commits

    • f5af0d2c...433f7838 - 46945 commits from branch gitlab-org:master
    • e2c672ba - Add predefined variable CI_COMMIT_FORCE_PUSH.
    • 210451fa - add test detecting CI_COMMIT_FORCE_PUSH variable

    Compare with previous version

  • removed idle label

  • 48 48 variables.append(key: 'CI_COMMIT_SHA', value: pipeline.sha)
    49 49 variables.append(key: 'CI_COMMIT_SHORT_SHA', value: pipeline.short_sha)
    50 50 variables.append(key: 'CI_COMMIT_BEFORE_SHA', value: pipeline.before_sha)
    51 variables.append(key: 'CI_COMMIT_FORCE_PUSH', value: pipeline.force_push?.to_s)
  • Tyler Kellen added 2794 commits

    added 2794 commits

    • 210451fa...06c1289e - 2791 commits from branch gitlab-org:master
    • a4e7a5f3 - Add predefined variable CI_COMMIT_FORCE_PUSH.
    • 15531be6 - add test detecting CI_COMMIT_FORCE_PUSH variable
    • df8cd1c6 - move force_push? to Ci:Pipeline

    Compare with previous version

  • added idle label

  • Security policy violations have been resolved.

    Edited by GitLab Security Bot
  • 🤖 GitLab Bot 🤖 added stale label and removed idle label

    added stale label and removed idle label

  • Hi @wescaleout, are you still interested in working on this?

  • removed stale label

  • Please register or sign in to reply
    Loading