Home > Mobile >  How to set up yaml file with gitlab to deploy when a specific file changes?
How to set up yaml file with gitlab to deploy when a specific file changes?

Time:10-01

I'm trying to set up a YAML file for GitLab that will deploy to my QA server only when a specific folder has a change in it.

This is what I have but it doesn't want to work. The syntax doesn't register any errors.

deploy to qa:
  script: **aws scripts**
  only:
    refs:
      - master
    changes:
      - directory/*
  stage: deploy
  environment:
    name: qa
    url: **aws bucket url**

The problem seems to be with this section, the rest works without it. The documentation talks about using rules as a replacement for when only and changes are used together but I couldn't get that to work either.

only:
    refs:
      - master
    changes:
      - directory/*

CodePudding user response:

The issue you're running into is the refs section of your "only" rule. Per GitLab's documentation on "changes": "If you use refs other than branches, external_pull_requests, or merge_requests, changes can’t determine if a given file is new or old and always returns true." Since you're using master as your ref, you are running into this issue.

As you've ascertained, the correct answer to this is to use a rules keyword instead. The equivalent rules setup should be as follows:

deploy to qa:
  script: **aws scripts**
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      changes:
        - directory/*
      when: on_success
    - when: never
  stage: deploy
  environment:
    name: qa
    url: **aws bucket url**

Essentially, the rule is saying "If the commit you're building from exists on your default branch (master in your case), and you have changes in directory/*, then run this job when previous jobs have succeeded. ELSE, never run this job"

Note: Technically the when: never is implied if no clauses match, but I prefer including it because it explicitly states your expectation for the next person who has to read your CI/CD file.

  • Related