How to make matrix and lockable resources work together

31 views
Skip to first unread message

nikola...@gmail.com

unread,
Nov 29, 2024, 1:35:19 PM11/29/24
to Jenkins Users

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

  • device1:
    • port: /dev/ttyUSB1
  • device2:
    • port: /dev/ttyUSB2

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.

So, my questions:

  1. Can lockable resources plugin work with matrix? Are there alternatives?
  2. Should I report the bug about the variable scope?
  3. Is there a way to evaluate a variable name from the other variable?
  4. Other suggestions on making this code prettier?
Thank you in advance,
Nick

Reply all
Reply to author
Forward
0 new messages