How to get exact corresponding http Jenkins workspace path?

A bit late to the party, however here is my take on this:

what is that 64 number in the path

This number corresponds to a particular step in the job’s flowgraph. If you open the job instance page in Jenkins and click on “Pipeline Steps” in the left bar you’ll get to the list of all steps that ran as part of that particular job. When you hover on a step the step ID will be displayed. Step #64 should be a “node” step where the current agent and associated workspace were created.

how can it be retrieved using Jenkins environment variable or APIs?

My approach (originating from this discussion) is to create a common step in a Jenkins shared library (e.g., as file library/vars/workspaceUrl.groovy) as such:

import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.actions.WorkspaceAction;

def call() {
  FlowNode node = getContext(FlowNode.class)

  for (FlowNode parent: node.iterateEnclosingBlocks()) {
    if (parent.getAction(WorkspaceAction)) {
      return "${env.JENKINS_URL}${parent.url}ws/"
    }
  }
}

Then this custom step can be called in a Jenkins declarative pipeline as this:

@Library('pipeline-lib') _

pipeline {
  agent {
    label 'deploy'
  }
  stages {
    stage('from_step') {
      steps {
        println(workspaceUrl())
      }
    }
    stage('from_environment') {
      environment {
        WORKSPACE_URL = "${workspaceUrl()}"
      }
      steps {
        sh 'printenv WORKSPACE_URL'
      }
    }
  }
}

Gives the following output with Jenkins v2.492.3:

Running on agent-deploy-vv1b8 in /home/jenkins/agent/workspace/demo
[Pipeline] {
[Pipeline] stage
[Pipeline] { (from_step)
[Pipeline] getContext
[Pipeline] echo
https://jenkins.mydomain.org/jenkins/job/demo/4/execution/node/3/ws/
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (from_environment)
[Pipeline] getContext
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ printenv WORKSPACE_URL
https://jenkins.mydomain.org/jenkins/job/demo/4/execution/node/3/ws/
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
1 Like