Run pipeline only on successful merge in specific branch

Hello all,
I want to start a pipeline only on specific branch after successful commit.
I wrote this:

workflow:
  rules:
    - if: $CI_MERGE_REQUEST_APPROVED
....
stagename:
   stage: stage
   only:
    - branchname
    - merge_requests
  except:
    - master

When I approve and make merge - nothing is happening.
If i remove the workflow - the pipeline is starting even when i make a push in master.
If i add the rule in the pipeline below except for example, i got - invalid yaml.

please let me know if it is possible and how to run a pipeline after merge in a specific branch.

thank you

Hi @vasilev
You have indent error in your YAML. The except is not on the same level as only

If you want to run a merge request pipeline only for a specific branch you can also use rules

only_on_branchname:
  rules:
    - if: '$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME == "branchname"'

Hi @balonik , thanks. Yes, it is written correct in the yaml :slight_smile:
i have added this rule and i got error:

config key may not be used with rules: except, only

I have tried with workflow → rules, but then got another error: rule if invalid expression syntax

Perhaps something else need to be changed.
I need

$CI_MERGE_REQUEST_APPROVED

but as I understood it is from gitlab 14 version. I am using currently 13…

I have edited my previous post and added missing ‘$’ there.

You have to remove except and only keywords. New rules keyword replaces them and they cannot be used together.

Obviosly, you cannot use a variable from GitLab 14 in GitLab 13.

Hm,
I am merging master to dev and want to start the pipeline only on dev.
When i specified SOURCE … it is skipped in dev branch and not starting in master.
When i specified TARGET… it is skipped in dev and started in master.
now i have specified this on gitlab v.14:

rules:
- if: ‘$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME == “main”’
when: never
- if: ‘$CI_COMMIT_BRANCH == “dev” && $CI_MERGE_REQUEST_APPROVED’
when: always

and … nothing is happening. perhaps is written wrong

$CI_COMMIT_BRANCH is empty in pipelines for merge requests, because they are detached and “does not belong to a branch”, ref docs. That rule won’t work.

If I understand you are merging main into dev. Which means main is source and where the MR pipeline is normally running. And you want to run pipeline for (target) dev branch?
In what state do you want the dev branch to be? If you need to run pipeline on dev as if the MR is merged, before it actually is you need this Premium feature: Pipelines for merged results | GitLab

Ah, ok.
Yes, I am merging main into dev.
I want to start a pipeline once it is merged or approved in dev but not in main.
Is this possible in v14 community edition?

If you merge a MR main into dev (by default) it creates a (merge) commit in the dev branch and that will trigger a pipeline (if defined) as any other commit to the dev branch.

There is no trigger/functionality to launch a pipeline for the Approval.