How can I run a job on a commit in a specific branch

,

I’m trying to configure my .config-ci.yml so that certain jobs run when commits are made on specific branches. For example, I would like “dev-job” to run when I commit to the “development” branch. What would be the best way to accomplish this? Would something like the following work:

stages:
     - dev

dev-job:
     stage: dev
     only:
         - if: $CI_COMMIT_BRANCH == 'development
     script:
         - echo "This runs when a commit is made in the 'development' branch."

Hi @tlheille

I think you probably want to use rules here, like this:

stages:
     - dev

workflow:
    rules:
        - if: $CI_MERGE_REQUEST_ID
          when: never
        - when: always

dev-job:
     stage: dev
     rules:
         - if: '$CI_COMMIT_BRANCH == "development"'
           when: always
         - when: never
     script:
         - echo "This runs when a commit is made in the 'development' branch."

I reviewed some situations like mine and found that you need to reference merge_request in order to use the $CI_MERGE_REQUEST_TARGET_BRANCH_NAME variable:
stages:
- dev

dev-job:
stage: dev
only:
refs:
- merge_request
variables:
- $CI_MERGE_REQUEST_TARGET_BRANCH
script:
- echo “This runs when a commit is made in the ‘development’ branch.”

Right, so this is for projects that run merge request pipelines rather than branch pipelines, i.e. if you have a workflow like this:


workflow:
  rules:
    - if: $CI_MERGE_REQUEST_IID
    - if: $CI_COMMIT_TAG
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
1 Like