Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use rule in gitlab-ci

I'm trying to build a job that can be conditionally executed depends on whether the files or subdirectories in WebClient is modified in develop branch, using rules. If there are changes found in the develop branch only, then a pipeline will be built.

Currently what I got in my .gitlab-ci.yml is

deploy_dev_client:
  stage: client
  tags:
    - my tags
  script:
    - '& cd WebClient'
    - 'npm rebuild node-sass'
    - 'npm install @angular/[email protected]'
    - '& npm run build-release --max_old_space_size=$NODE_MEMORY_SIZE'

  rules:
    - changes:
      - WebClient/**/*
      when: always
    - when: never

However, after testing, I realized that the pipeline is executed whenever I push something from my local repo to gitlab, even at the other side branches.

I have tried using only:-develop', however it results in yaml invalid error, maybe due to not being able to use only if rules has already been used. Is there anyway I can still using rules to target only develop branch?

like image 282
Việt Tùng Nguyễn Avatar asked Jan 15 '20 09:01

Việt Tùng Nguyễn


1 Answers

In this link:

https://docs.gitlab.com/ee/ci/yaml/#ruleschanges

They write that rules: changes should work exactly like only/except. If you read about only/except, there are a few strange things with it:

https://docs.gitlab.com/ee/ci/yaml/#using-onlychanges-without-pipelines-for-merge-requests

When pushing a new branch or a new tag to GitLab, the policy always evaluates to true.

To get around this and only run your job on the development branch, you should be able to combine an if with changes:

deploy_dev_client:
  stage: client
  tags:
    - my tags
  script:
    - '& cd WebClient'
    - 'npm rebuild node-sass'
    - 'npm install @angular/[email protected]'
    - '& npm run build-release --max_old_space_size=$NODE_MEMORY_SIZE'

  rules:
    - if: '$CI_COMMIT_REF_NAME== "development"'
      changes:
      - WebClient/**/*
      when: always

(I haven't tested this code, so let me know if it is all wrong!)

like image 159
MrBerta Avatar answered Sep 18 '22 10:09

MrBerta