0
votes

I am trying to include an external GitLab CI YAML file into my project local .gitlab-ci.yml file. The external YAML, which is in my other GitLab project ci/cd > templates contains some workflow rules:

# ci-cd.yml

workflow:
  rules:
    - if: '$TRACK != null' # TRACK is the environment type (staging/production)
      when: always
    - if: '$CI_PIPELINE_SOURCE =~ /^trigger|pipeline|web|api$/'
      when: always

Below is my project local .gitlab-ci.yml:

include:
- '/.gitlab-ci-test.yml'
- project: 'ci-cd/templates'
  file: 'ci-cd.yml'

...

The problem is - none of the jobs I have defined inside locally included .gitlab-ci-test.yml get triggered when I push the changes to GitLab, even when the job has when: always set. Seems like the workflow rules in external ci-cd.yml are not letting the jobs run.

I've also tried to locally add a workflow rule to .gitlab-ci.yml that evaluates to true, because the GitLab workflow keyword docs say

When no rules evaluate to true, the pipeline does not run.

That means if any one of the rules evaluates to true, pipeline should have run, which did not happen when I added a local workflow rule.

EDIT - the external file which has workflow rules is used by many projects so this can't be modified to have "push" in $CI_PIPELINE_SOURCE. My intention is to let it be as a global rule and try to 'override' locally in my project.

I hope I was clear in issue description. Would appreciate any help!

1

1 Answers

1
votes

You are missing push event for $CI_PIPELINE_SOURCE in you workflow rule.

workflow:
  rules:
    - if: '$TRACK != null' # TRACK is the environment type (staging/production)
      when: always
    - if: '$CI_PIPELINE_SOURCE =~ /^trigger|pipeline|push|web|api$/'
      when: always

EDIT: if you are not able to change the workflow rule in the included file, the only option I see is to duplicate the workflow in your gitlab-ci.yml and add the missed push there. workflow rules take precedence before all other rules and it is not possible to merge two workflow blocks. If a worfklow block is used in an included yml file and the gitlab-ci.yml itself, the workflow from the gitlab-ci.yml is used. You can check this in CI/CD -> Editor -> View merged YAML in gitlab.

include:
  - '/.gitlab-ci-test.yml'
  - project: 'ci-cd/templates'
    file: 'ci-cd.yml'

workflow:
  rules:
    - if: '$TRACK != null' # TRACK is the environment type (staging/production)
      when: always
    - if: '$CI_PIPELINE_SOURCE =~ /^trigger|pipeline|push|web|api$/'
      when: always

...