Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically set parallel:matrix in gitlab yaml?

I have a .NET solution with multiple projects and for each project I have a separate test project. Currently, whenever I add a new project, I will add a separate test project for it and I need to manually add a new test to the pipeline test step.

I want to write a test step that will run all test projects parallel, but without me having to manually add a new test. Recently, I discovered gitlab has a parallel:matrix keyword, that seems a step in the right direction. I am already working on using it, instead of having separate implementations of a re-usable script, but if possible I want to also dynamically find tests in my test folder.

Current re-usable test script:

.test: &test
  allow_failure: false
  dependencies:
    - build
  image: mcr.microsoft.com/dotnet/sdk:6.0
  script: 
    - echo ${TEST_NAME}
    - echo ${RESULT_FILE_NAME}
    - dotnet test --no-restore ./Tests/${TEST_NAME} -l "JUnit;LogFilePath=../../TestResults/${RESULT_FILE_NAME}.xml"

Example implementation:

Test1:
  <<: *test
  stage: test
  variables:
    TEST_NAME: "test1"
    RESULT_FILE_NAME: "test1_results"
  artifacts:
    paths:
      - ./TestResults/   

What I'm trying to achieve:

test:
  stage: test
  dependencies:
    - build
  image: mcr.microsoft.com/dotnet/sdk:6.0
  before_script:
    - TEST_NAMES = ["test1", "test2"] //Want to find these dynamically
  script: 
    - ls
    - echo ${TEST_NAME}
    - echo ${RESULT_FILE_NAME}
    - dotnet test --no-restore ./Tests/${TEST_NAME} -l "JUnit;LogFilePath=../../TestResults/${TEST_NAME}.xml"
  parallel:
    matrix:
      - TEST_NAME: TEST_NAMES

My current test step (added as exp_test until fully being able to replace test), where I'm expecting 2 parallel tests running, but instead it's only running 1 with the name of the variable, instead of using the variable as an array:

image of test step, showing that currently it's only trying to run 1 test

I found 1 answer on here that suggests to dynamically create a child pipeline yaml, but I want to see if it's possible to use parallel:matrix for this.

like image 791
Friso Avatar asked Oct 24 '25 14:10

Friso


1 Answers

So I was researching the same issue, where I wanted to create dynamic values within parallel:matrix, and the only way I was able to do it was by creating a trigger that would call a script in which would generate the yml file that had the dynamic values, and the trigger job would include this yml for execution of whatever tasks you have. This article goes over it perfectly on how to do create a trigger job:

https://devghoststories.com/gitlab-child-pipeline-with-dynamic-configuration-in-5-minutes-6950c4d3fdd6

The problem with creating dynamic values otherwise in parallel:matrix is that GITLAB CI pipelines interprets the file and creates the parallel jobs BEFORE they execute, so making the dynamic values WITHIN the GITLAB CI doesn't work. In fact, what ends up happening is that the values that are read from this are all passed at once for ONLY ONE job to use. So rather than X amount of parallel tasks, it is ONE task with X entries to go through. Hopefully this helped.

like image 166
ANSOLO Avatar answered Oct 27 '25 01:10

ANSOLO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!