13

I am using the Jenkins Pipeline plugin to execute an iOS Checkout-Build-Test-Deploy pipeline.

One step involves checking whether an iDevice is actually connected (otherwise the rest of the steps will not be executed).

I want to execute a shell command idevice_id -l (from libimobiledevice) which will print out the device ID of the connected device, or fail if nothing connected.

So the steps as I envision them, would be:

  • Execute command sh "/usr/local/bin/idevice_id -l"
  • parse the output somehow
  • fail if this command does not return anything.

I have read most of the official docs, but drew a blank. How do I parse the output of the sh step? If I am approaching this in the wrong way, any other suggestions are welcome.

Nick Sonneveld
  • 3,356
  • 6
  • 39
  • 48
Vish
  • 2,144
  • 5
  • 25
  • 48

2 Answers2

38

As mentionned in this answer, as of version 2.4 of Pipeline: Nodes and Processes you can use:

def out = sh script: '/usr/local/bin/idevice_id -l', returnStdout: true
Community
  • 1
  • 1
Pom12
  • 7,622
  • 5
  • 50
  • 69
  • `org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 43: Expected a step @ line 43, column 17. def out = sh script: './script.sh', returnStdout: true ^ 1 error` – Alex Jul 20 '18 at 18:48
4

The official method is to redirect the output into a file and read the file

If the output file is empty, you can fail the job with the pipeline script command error "no device connected"

Example on github

Flo
  • 491
  • 4
  • 10
  • fantastic! This works for me fine. `sh('/usr/local/bin/idevice_id -l > DEVICE_ID'); deviceId=readFile('GIT_COMMIT')` – Vish Mar 30 '16 at 11:11
  • 3
    This is not the "official" method/workaround anymore, see [this answer](http://stackoverflow.com/a/39102404/702954) for updated method. – Pom12 Aug 23 '16 at 13:20