Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.jenkinsci.plugins.workflow.cps.CpsThread;
- echo "Computer name: ${getCurrentComputerSafe().displayName}" // Prints: Built-In Node
- pipeline {
- agent any
- stages {
- stage('Stage1') {
- agent {
- label 'MyNode'
- }
- steps {
- script {
- echo "Computer name: ${getCurrentComputerSafe().displayName}" // Prints: MyNode
- }
- }
- }
- }
- }
- @NonCPS
- static private Computer getCurrentComputerSafe()
- {
- // Try get executing computer first
- def current = Computer.currentComputer();
- if (current != null)
- return current;
- // Reverse iterate steps to find a delegate computer that
- // will execute.
- def threads = CpsThread.current().group.threads.toArray();
- for (int i = threads.length - 1; i >= 0; i--)
- {
- def thread = threads[i];
- if (thread.step == null)
- continue;
- def filePath = thread.step.context.get(hudson.FilePath);
- if (filePath == null)
- continue;
- return filePath.toComputer();
- }
- // As a last measure, return the master computer
- def computers = Jenkins.instance.computers;
- for (int i = 0; i < threads.length; i++)
- {
- def computer = computers[i];
- if (computer instanceof Jenkins.MasterComputer)
- return computer;
- }
- throw new Exception('Unexpected no master computer found');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement