Converting only/except to rules

I would like to convert a couple of pipelines that I have which are using the only/except keywords to using the newer rules keyword. I am struggling however to get the same functionality and cannot find an example in the document or whilst searching.

I currently have: -

  only:
    - branches
    - merge_requests
    - main
    - tags
  except:
    - pipelines

The above allows the stage to run in a branch, a merge request, the main branch, a tagged branch but not when a pipeline is triggered from another repo.

How can I write this with the rules keyword?

I’d keep using the above but I would like to use rules:exists which is not part of the only/except it seems.

Thanks so much.

I think this should do it?

  rules:
    # pipeline triggers
    - if: $CI_PIPELINE_SOURCE == "pipeline"
      when: never
    # tags
    - if: $CI_BUILD_TAG
      when: never
    # branches
    - if: $CI_COMMIT_BRANCH != "main" && $CI_PIPELINE_SOURCE == "push"
      when: on_success
    # main
    - if: $CI_COMMIT_BRANCH == "main" && $CI_PIPELINE_SOURCE == "push"
      when: on_success
    # merge requests
    - if: $CI_MERGE_REQUEST
      when: on_success

Ok that didn’t work… but managed to get it mostly working with this:

  rules:
    # pipeline triggers
    - if: $CI_PIPELINE_SOURCE == "pipeline"
      when: never
    # main
    - if: $CI_COMMIT_BRANCH == "main" && $CI_PIPELINE_SOURCE == "push"
      when: on_success
    # merge requests
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: on_success
    # tags
    - if: $CI_BUILD_TAG
      when: on_success

What I can’t seem to figure out is how to get it to fire for just branches. Whenever I try adding a branch only rule, and I push another commit to an existing merge request, the pipeline will then fire for the branch but not the merge request.

Previously using only/except both a branch and a merge pipeline would launch… not so when using rules. With rules it seems you need to duplicate the job template?