How to only build jobs when files change on a specific branch

As described in the documentation one can define only certain (e.g. branches) that trigger a ci pipeline job:

job:
  # use regexp
  only:
    - /^issue-.*$/
  # use special keyword
  except:
    - branches

Further there is a section [https://docs.gitlab.com/ce/ci/yaml/#only-changes] on triggering job on changes only:

docker build:
  script: docker build -t my-image:$CI_COMMIT_REF_SLUG .
  only:
    changes:
      - Dockerfile
      - docker/scripts/*

What is not clear to me is how to combine the branch name with the changes:, since this does not work:

docker build:
  script: docker build -t my-image:$CI_COMMIT_REF_SLUG .
  only:
    - master
    changes:
      - Dockerfile
      - docker/scripts/*
1 Like

Ok, so looking at this section in more detail: [https://docs.gitlab.com/ce/ci/yaml/#only-variables] it’s now clear: you have to put the branch name under a refs: keyword.

deploy:
  script: cap staging deploy
  only:
    refs:
      - branches
    changes:
      - Dockerfile
    variables:
      - $RELEASE == "staging"
      - $STAGING
3 Likes