GitLab CI run Job only refs AND changes

Viewed 3927

I want to run Job only for merge_requests refs AND when changes for specified files, according to official doc i create this Job in .gitlab-ci.yml:

merge-request-test:
  <<: *some_anchor
  only:
    refs:
      - merge_requests
    changes:
      - "*.py"
      - "**/*.py"
      - postman/some-file.json

But this job starts even if i change any other file.

I understand that GitLab CI apply OR rule for refs and changes sections:

In the example below, the test job will not be created when any of the following are true:

The pipeline runs for the master. There are changes to the README.md file in the root directory of the repo.

test:   
    script: npm run test
    except:
      refs:
        - master
      changes:
        - "README.md"  

I want to start this Job only for MR and when changes specified files. How i can achieve this behaviour?

If remove refs section, it works only when files changed, but official doc does not recommend do this, because i want to use it in pipelines:

If using only:changes with only allow merge requests to be merged if the pipeline succeeds, undesired behavior could result if you do not also use only:merge_requests.

1 Answers

For now i have this working solution:

merge-request-deploy:
  <<: *deploy-app
  rules:
    - if: '$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ /^feature/ && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop"'
      changes:
      - "*.py"
      - "**/*.py"
      - postman/some-file.json
      when: always
  • if: '$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ /^feature/ && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop"' - gives me MR branc
  • changes - gives me changed files

Rule's sections works with AND rule.

Related