How to get exact corresponding http Jenkins workspace path?

I need to email following path after Jenkins build is completed -

http://<ip>:8080/job/Nightly_Build/51/execution/node/64/ws/

Using BUILD_URL environment variable, I can get path till http://<ip>:8080/job/Nightly_Build/51. Remaining path we can hardcode except value 64 in above path which keep changes when I make changes in pipeline code.

I am unable to get that value on the fly.

Can someone explain what is that 64 number in the path and how can it be retrieved using Jenkins environment variable or APIs?

See example pipeline below:

import org.jenkinsci.plugins.workflow.actions.WorkspaceAction
import org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode
import org.jenkinsci.plugins.workflow.graph.FlowGraphWalker
import org.jenkinsci.plugins.workflow.graph.FlowNode

pipeline {
    agent any

    stages {
        stage('Create') {
            steps {
                deleteDir()
                sh 'date +%F%T >> ts.txt'
                //requires badge-plugin >= 2.x
                addBadge id: 'myBadgeId', icon: 'symbol-hourglass-outline plugin-ionicons-api', text: 'Timestamp',
                        link: link('ts.txt')
            }
        }
        stage('Work') {
            steps {
                echo "Hourglas symbol in job history linking to ts.txt now"
                sleep 30
            }
        }
        stage('Clean') {
            steps {
                removeBadges(id: 'myBadgeId')
            }
        }
    }
}


/*
Create a link to a file in a current workspace.
Note: presume this won't work for parallel stages

Signatures for Script Security:
method hudson.model.Actionable getAction java.lang.Class
method org.jenkinsci.plugins.workflow.graph.FlowNode getId
method org.jenkinsci.plugins.workflow.job.WorkflowRun getExecution
method org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper getRawBuild
new org.jenkinsci.plugins.workflow.graph.FlowGraphWalker org.jenkinsci.plugins.workflow.flow.FlowExecution
staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods findAll java.lang.Object groovy.lang.Closure
*/

String link(String file) {
    List<String> ids = new FlowGraphWalker(currentBuild.rawBuild.execution).findAll { FlowNode n ->
        (n instanceof StepStartNode) && n.getAction(WorkspaceAction)
    }*.id
    return ids.isEmpty() ? null : "${env.BUILD_URL}execution/node/${ids.first()}/ws/${file}"
}

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