Rycieos
Rycieos

Reputation: 43

How to get the current Jenkins pipeline StepContext

I have a step in a pipeline that pulls objects from the context and uses them. However, I need to access those objects outside of the steps to feed into different steps, and the second step doesn't expose it.

stage() {
  steps {
    script {
        def status = waitForQualityGate()
        // Use the taskId
      }
    }
  }
}

The waitForQualityGate() call only returns a boolean, so I can't access it there.

I could instead manually initialize the step, like so:

 script {
    def qualityGate = new WaitForQualityGateStep()
    def taskId = qualityGate.getTaskId()
 }

but the taskId is null. If I try to run the start methods manually on the step:

script {
    def qualityGate = new WaitForQualityGateStep()
    qualityGate.start().start()
    def taskId = qualityGate.getTaskId()
}

It fails with the message:

java.lang.IllegalStateException: you must either pass in a StepContext to the StepExecution constructor, or have the StepExecution be created automatically

The WaitForQualityGateStep has the info I need, but I can't initialize it without having a StepContext (which is an Abstract class). How can I get one from the pipeline?

Upvotes: 3

Views: 1628

Answers (2)

Rycieos
Rycieos

Reputation: 43

I still have no idea how to manually get a step context to manually execute a step, but in case anyone else finds this by trying to get information out of the Sonar plugin, this is how I got the task ID that I needed.

def output = sh(script: "mvn sonar:sonar", returnStdout: true)
echo output  // The capture prevents printing to console

def taskUri = output.find(~'/api/ce/task\\?id=[\\w-]*')

Upvotes: 1

oblio
oblio

Reputation: 1633

You can define the variable before the pipeline and in the step just set its value. This way the variable is visible across the pipeline.

Upvotes: 1

Related Questions