jenkinsjenkins-pipelinejenkins-plugins

How to get Jenkins lockable resources parameters with matrix plugin


I'm trying to run automated tests in parallel on several devices using Jenkins. There is the matrix plugin for the parallel execution. I have several devices connected to the same agent and they have different parameters. Let's say

Then in the pipeline I need to know this parameter "port". So I do next:

pipeline {
    ...
    stages {
        stage('Checkout') {...}
        stage('Build') {...}
        stage('Parallel') {
            matrix {
                axes {
                    name 'device_name'
                    values 'device1', 'device2'
                }
            }
            stages {
                stage('Flashing and testing') {
                    steps {
                        lock(resource: "${device_name}", variable: "${device}", quantity: 1)
                        echo "Device is ${device}"
                        ...
                    }
                }
            }
        }
    }
}

With that device is defined correctly, but how can I access it's port parameter? The documentation does not describe the case when there are several locks at the time. Though I can access the port with device0_port seems like it is the global variable and it is the same for all the parallel threads. I tried to call something like "${${device}0_port}" but could not realize what should it be.

As a workaround I tried to move the parameters into the pipeline like

if ("${device}" == 'device1') {
    port = "/dev/ttyUSB1"
}
if ("${device}" == 'device2') {
    port = "/dev/ttyUSB2"
}

It does exactly what I need, but I suspect it is not the way it is supposed to be. Any suggestions how to do it nicely?


Solution

  • Seems like no one knows. Just in case someone will face this issue and find this question, I could not find anything better than shorten the workaround to

    port = evaluate("${device}0_port")
    

    This replace device with the actual device name when it is parsed by groovy interpreter and then at runtime it evaluates the actual variable name.