Search code examples
gitlab-ci

Make GitLab CI job use matrix when scheduled but just a single value when manual?


I want to use GitLab CI to build multiple variants during scheduled nightly builds, but only a specific variant if triggered manually from the web.

I have this config:

firebaseBuild:
  stage: build
  script:
    - ./gradlew assembleDebug
    - ./gradlew appDistributionUploadDebug
  rules:
    - if: '$CI_PIPELINE_SOURCE == "web"'
      variables:
        VAR: "x"
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
  parallel:
    matrix:
      - VAR: [ 'x', 'y' ]

My logic would be: by default, it's a matrix, but if the first if succeeds, then the value of VAR becomes constant.

Unfortunately, this doesn't work and the matrix is used even when triggered from web UI.

I know that I can define separate jobs, but that would result in code duplication, which I'd like to avoid. Any way to achieve my goal?


Solution

  • you can use the extends keyword to extend hidden jobs. That way you can reduce code duplication and have the hidden job define the code, then your job for web and schedules just apply the additional part like rules and matrix as needed.

    stages:          # List of stages for jobs, and their order of execution
      - build
    
    .firebaseBuild:
      stage: build
      script:
        - echo "${VAR}"
    
    scheduled_firebaseBuild:
        extends: .firebaseBuild
        parallel:
            matrix:
                - VAR: [ 'x', 'y' ]
        rules:
            - if: '$CI_PIPELINE_SOURCE == "schedule"'
    
    web_firebaseBuild:
        extends: .firebaseBuild
        rules:
            - if: '$CI_PIPELINE_SOURCE == "web"'
              variables:
                VAR: "x"
    

    Looking at these in the pipeline the schedule job process the vars as a matrix still. While web pipelines just run a single job for value X enter image description here

    enter image description here