gitlab

Run triggered Gitlab jobs only on specific days


I have the following issue:

I have a scheduled git pipeline which runs every work day in the morning. This pipeline is triggering other pipelines of other projects. The jobs are defined in multiple gitlab.yml files in their corresponding projects. For better understanding, here is a minimal example with only one triggered job:

main job:
  trigger: child job


child job:
  // Do something here

Now the thing is that this triggered child job is only allowed to run on specific days. In our case, it is that the child job is not allowed to run on Mondays.

I already had the idea to distinguish in the main job on which day the child job should be executed, give the child job a variable and check the given variable with the only or except tags. But it seems like it is not that easy to get the current work day inside the gitlab.yml. Or am I wrong there?

Is there a way to achieve this?

UPDATE @KamilCuk made me realize that I was missing an important aspect in the question. When the child job is executed on its own, it should run without any hinderance (also on monday) if possible. When triggered by the main job, the check should apply.


Solution

  • The simplest is to just check it in the job.

    child job:
      script:
        - weekday=$(LC_ALL=C date +%a)
        - case "$weekday" in
          Mon) echo "Not running on monday"; exit 0; ;;
          esac
        - rest of the job
    

    You can trigger the job via API. https://docs.gitlab.com/ee/api/pipeline_triggers.html https://docs.gitlab.com/ee/ci/triggers/index.html

    main job trigger child job:
      script:
        - weekday=$(LC_ALL=C date +%a)
        - case "$weekday" in
          Mon) echo "Not running on monday"; exit 0; ;;
          esac
        - curl
          -H "Authorization: Bearer somethingosmething" 
          $CI_GITALB_URL/.../api/4/....
          trigger child job