Add branch filter to scheduled pipeline execution policies

What does this MR do and why?

This allows to run scheduled pipeline execution policies for certain branch names. If branches is not provided, it will use the default branch.

If branches are provided but the project doesn't have those branches, it won't start a pipeline.

References

Related issue: #504143

How to set up and validate locally

  1. Create a new group.

  2. Create a new project on the group.

  3. Create two new branches on the project schedule and schedule-2.

  4. Add a simple CI config file policy-ci.yml:

    pipeline execution policy job:
      stage: .pipeline-policy-pre
      script:
        - echo "Enforce your policy here"
  5. Add a .gitlab/security-policies/policy.yml file to the project with content (Replace path/to/project with the path to your project):

    ---
    experiments:
      pipeline_execution_schedule_policy:
        enabled: true
    pipeline_execution_policy: []
    approval_policy: []
    pipeline_execution_schedule_policy:
      - name: test
        description: ''
        enabled: true
        pipeline_config_strategy: schedule
        content:
          include:
            - project: path/to/project
              file: policy-ci.yml
        schedules:
          - type: daily
            start_time: '10:00'
            branches:
              - schedule
              - schedule-2
              - schedule-3
            time_window:
              value: 600
              distribution: random
  6. Go back to the group page and on the left sidebar, select Security & Compliance and Policies.

  7. Select Edit policy project and select your project. Then select Save.

  8. Start the schedule worker manually to trigger the schedule (replace PROJECT_ID with the ID of your project):

    Security::PipelineExecutionPolicies::RunScheduleWorker.new.perform(PROJECT_ID)
  9. Go to Build -> Pipelines in your project and wait for the scheduled pipeline to appear. There should be 2 new pipelines. One for the schedule branch and one for schedule-2.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #504143

Edited by Andy Schoenen

Merge request reports

Loading