Tonelock
Tonelock

Reputation: 53

`rules` condition in gitlab ci does not work

I am currently trying to build a pipeline in gitlab ci. It contains a job (here called speed) that should only be run on a specific day (e.g. the 6th of every month). The configuration yaml looks like this:

stages:
  - speed
  - watcher

variables:
  # setup cache in root folder so gitlab cache can pick it up
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
  TODAYS_DATE: "$(date +%Y-%m-%d)"
  TODAYS_DAY: "$(date +%d)"
  TODAYS_MONTH: "$(date +%m)"

include:
  - local: "ci/ci_job_speed.yml"
  - local: "ci/ci_job_watcher.yml"

speed:
  extends: .speed
  stage: speed
  rules:
    - if: '$TODAYS_DAY == "06"'
      when: always

watcher:
  when: always
  extends: .watcher
  stage: watcher
  rules:
    - if: '($CI_COMMIT_AUTHOR !~ /.*Gitlab Runner.*/ && $CI_COMMIT_BRANCH == "test") || $CI_PIPELINE_SOURCE != "push"'

cache:
  paths:
    - .cache/pip
    - .cache/poetry
    - .venv
  key: "globalcache"

My problem is, that the speed job seems to be ignored, and only the watcher job is started, even though I expect speed to be run (since today is the 6th). The ci linter in GitLab validates my yaml as correct. Does anyboy have an idea what I am doing wrong?

I have tried different combinations as '$TODAYS_DAY == '06'', $TODAYS_DAY == '06' etc, none of them worked yet (the job is simply ignored).

Thanks in advance for any advice :)

Upvotes: 0

Views: 967

Answers (2)

Tonelock
Tonelock

Reputation: 53

@joreign you are correct, the variable TODAYS_DATE was not assigned the day of the current date, but a string value "$(date +%d)". After trying a bit, I could not find a way to enforce the desired behaviour. Instead, I used a workaround as described in https://gitlab.com/gitlab-org/gitlab/-/issues/20769#note_215109166 and set a random variable in a Pipeline scheduler and gave the ci job a corresponding run condition.

speed:
  extends: .speed
  stage: speed
  only:
    refs:
      - schedules
    variables:
      - $REFSPEED_SCHEDULE

Upvotes: 1

joreign
joreign

Reputation: 168

Looking at your yaml i would suggest, that TODAYS_DAY: "$(date +%d)" is not executed as intended.
If you assigned this Variable in a bash script, the command date +%d would be executed and the result would be stored in the variable, but i think in your case $(date +%d) just gets stored as literal string.

I could be wrong about this variable, but this is my best guess.

You could consider using GitLabs scheduled Pipelines Feature: https://docs.gitlab.com/ee/ci/pipelines/schedules.html#add-a-pipeline-schedule

Upvotes: 2

Related Questions