GitLab CI - running a task automatically only on scheduled pipelines

Viewed 2029

I need a task that I can either manually execute or is run automatically on nightly schedule. I found this solution:

  rules:
    - changes:
      - scheduled
      when: always
    - when: manual

The problem with the solution is that when a new pipeline is created and the task isn't run the pipeline is stuck in blocked state until I run it manually.

To avoid this I found a suggested workaround to add:

 allow_failure: true

But this again brings a problem that if the task fails on nigtly run then the pipeline doesent fail and I don't get e-mail notifications.

Is there a way to solve this?

2 Answers

You can set allow_failure conditionally using rules:. So, instead of setting the allow_faulure: key on the job, set it in any rule that causes the job to be 'manual'.

  rules:
    - changes:
      - scheduled
      when: always
    - when: manual
      allow_failure: true

Also, based on your description it would probably be best to use a rule like so:

myjob:
  rules:
    - if: $CI_PIPELINE_SOURCE == "schedule"
      when: always
      allow_failure: false
    - when: manual
      allow_failure: true

Another alternative to prevent your pipeline being blocked by this job would be to have it run in the .post stage and use needs: [] to have it run immediately. That way, it'll never cause other jobs to wait on it.

myjob:
  needs: []
  stage: .post
  # ...

There might be a better solution, but another workaround could be to have two separate jobs with the rules in and template out everything else. For example:

.build_template: &build_template
  image: ubuntu:18.04
  script:
    - echo "hello world"

build_manual:
  <<: *build_template
  when: manual
  except:
    - schedules

build_nightly:
  <<: *build_template
  only:
      - schedules
Related