How to use parallel: matrix in gitlab.ci to run multiple maven profiles

Curious if I can do something like what is below, essentially use parallel matrix to tack -P flags to the maven script. Is there a way to use parallel matrix to add different maven profiles and run the scripts in parallel? Or is there another way to do this without having the jobs separated? I want them coupled together in the pipeline.

test-e2e:
  stage: test
  script:
    - |
      mvn $MAVEN_CLI_OPTS test
  parallel:
    matrix:
      - VERSION: [-Ptest1,-Ptest2]

Yes, you can use GitLab CI’s parallel matrix to pass different Maven profiles (-P) and execute them in parallel, while still keeping them logically coupled within the same pipeline job. Here’s how you can structure it:

Example Using Parallel Matrix

test-e2e:
  stage: test
  parallel:
    matrix:
      - PROFILE: ["test1", "test2"] # Define Maven profiles here
  script:
    - echo "Running with Maven profile: $PROFILE"
    - mvn $MAVEN_CLI_OPTS test -P$PROFILE # Pass the profile dynamically

Explanation

  1. parallel.matrix:
  • The matrix keyword generates parallel jobs within the same pipeline stage.
  • Each matrix combination runs as an isolated job but is part of the same logical CI stage.
  1. Dynamic Profile Assignment:
  • The variable PROFILE is populated from the matrix and used in the mvn command via -P$PROFILE.
  1. Job Coupling:
  • These jobs are tied to the same pipeline stage (test) and run in parallel, but their results are aggregated within the same pipeline execution.

Benefits

  • Parallel Execution: Each Maven profile (-Ptest1, -Ptest2) runs in a separate thread, speeding up the pipeline.
  • Logical Grouping: All these jobs remain part of the same CI stage (test) and appear grouped in the GitLab UI.

Alternative: Running Multiple Profiles in the Same Script

If you don’t want separate parallel jobs and prefer to handle all profiles in a single script (e.g., for sequential execution), you could iterate over the profiles:

test-e2e:
  stage: test
  script:
    - for PROFILE in test1 test2; do
        echo "Running with Maven profile: $PROFILE";
        mvn $MAVEN_CLI_OPTS test -P$PROFILE || exit 1;
      done

This approach ensures the profiles run in sequence within a single job, but it doesn’t leverage parallelism.


Which Approach is Best?

  • Use Parallel Matrix: When you want to speed up execution by running multiple profiles concurrently.
  • Iterate Over Profiles: When the profiles must run in sequence, or you want them in the same job log for simplicity.

Let me know if you’d like help refining either configuration!