/*package*/ static CLICommand setCurrent(CLICommand cmd) { CLICommand old = getCurrent(); CURRENT_COMMAND.set(cmd); return old; }
/** * If the command is currently running inside a build, return it. Otherwise null. */ protected Run optCurrentlyBuilding() throws CmdLineException { try { CLICommand c = CLICommand.getCurrent(); if (c==null) throw new IllegalStateException("Not executing a CLI command"); String[] envs = c.checkChannel().call(new GetCharacteristicEnvironmentVariables()); if (envs[0]==null || envs[1]==null) return null; Job j = Jenkins.getActiveInstance().getItemByFullName(envs[0],Job.class); if (j==null) throw new IllegalArgumentException("No such job: "+envs[0]); try { Run r = j.getBuildByNumber(Integer.parseInt(envs[1])); if (r==null) throw new IllegalArgumentException("No such build #"+envs[1]+" in "+envs[0]); if (!r.isBuilding()) { throw new IllegalStateException(r + " is not currently being built"); } return r; } catch (NumberFormatException e) { throw new IllegalArgumentException("Invalid build number: "+envs[1]); } } catch (IOException | InterruptedException e) { throw new IllegalArgumentException("Failed to identify the build being executed",e); } }
/*package*/ static CLICommand setCurrent(CLICommand cmd) { CLICommand old = getCurrent(); CURRENT_COMMAND.set(cmd); return old; }
/*package*/ static CLICommand setCurrent(CLICommand cmd) { CLICommand old = getCurrent(); CURRENT_COMMAND.set(cmd); return old; }
/*package*/ static CLICommand setCurrent(CLICommand cmd) { CLICommand old = getCurrent(); CURRENT_COMMAND.set(cmd); return old; }
/*package*/ static CLICommand setCurrent(CLICommand cmd) { CLICommand old = getCurrent(); CURRENT_COMMAND.set(cmd); return old; }
/*package*/ static CLICommand setCurrent(CLICommand cmd) { CLICommand old = getCurrent(); CURRENT_COMMAND.set(cmd); return old; }
/** * If the command is currently running inside a build, return it. Otherwise null. */ protected Run optCurrentlyBuilding() throws CmdLineException { try { CLICommand c = CLICommand.getCurrent(); if (c==null) throw new IllegalStateException("Not executing a CLI command"); String[] envs = c.checkChannel().call(new GetCharacteristicEnvironmentVariables()); if (envs[0]==null || envs[1]==null) return null; Job j = Jenkins.getActiveInstance().getItemByFullName(envs[0],Job.class); if (j==null) throw new IllegalArgumentException("No such job: "+envs[0]); try { Run r = j.getBuildByNumber(Integer.parseInt(envs[1])); if (r==null) throw new IllegalArgumentException("No such build #"+envs[1]+" in "+envs[0]); if (!r.isBuilding()) { throw new IllegalStateException(r + " is not currently being built"); } return r; } catch (NumberFormatException e) { throw new IllegalArgumentException("Invalid build number: "+envs[1]); } } catch (IOException | InterruptedException e) { throw new IllegalArgumentException("Failed to identify the build being executed",e); } }
/** * This method makes sense only when called from within the build kicked by Hudson. * We use the environment variables that Hudson sets to determine the build that is being run. */ protected Run getCurrentlyBuilding() throws CmdLineException { try { CLICommand c = CLICommand.getCurrent(); if (c==null) throw new IllegalStateException("Not executing a CLI command"); String[] envs = c.channel.call(new GetCharacteristicEnvironmentVariables()); if (envs[0]==null || envs[1]==null) throw new CmdLineException("This CLI command works only when invoked from inside a build"); Job j = Hudson.getInstance().getItemByFullName(envs[0],Job.class); if (j==null) throw new CmdLineException("No such job: "+envs[0]); try { Run r = j.getBuildByNumber(Integer.parseInt(envs[1])); if (r==null) throw new CmdLineException("No such build #"+envs[1]+" in "+envs[0]); return r; } catch (NumberFormatException e) { throw new CmdLineException("Invalid build number: "+envs[1]); } } catch (IOException e) { throw new CmdLineException("Failed to identify the build being executed",e); } catch (InterruptedException e) { throw new CmdLineException("Failed to identify the build being executed",e); } }
/** * This method makes sense only when called from within the build kicked by Hudson. * We use the environment variables that Hudson sets to determine the build that is being run. */ protected Run getCurrentlyBuilding() throws CmdLineException { try { CLICommand c = CLICommand.getCurrent(); if (c==null) throw new IllegalStateException("Not executing a CLI command"); String[] envs = c.channel.call(new GetCharacteristicEnvironmentVariables()); if (envs[0]==null || envs[1]==null) throw new CmdLineException("This CLI command works only when invoked from inside a build"); Job j = Hudson.getInstance().getItemByFullName(envs[0],Job.class); if (j==null) throw new CmdLineException("No such job: "+envs[0]); try { Run r = j.getBuildByNumber(Integer.parseInt(envs[1])); if (r==null) throw new CmdLineException("No such build #"+envs[1]+" in "+envs[0]); return r; } catch (NumberFormatException e) { throw new CmdLineException("Invalid build number: "+envs[1]); } } catch (IOException e) { throw new CmdLineException("Failed to identify the build being executed",e); } catch (InterruptedException e) { throw new CmdLineException("Failed to identify the build being executed",e); } }
/** * This method makes sense only when called from within the build kicked by Hudson. * We use the environment variables that Hudson sets to determine the build that is being run. */ protected Run getCurrentlyBuilding() throws CmdLineException { try { CLICommand c = CLICommand.getCurrent(); if (c==null) throw new IllegalStateException("Not executing a CLI command"); String[] envs = c.channel.call(new GetCharacteristicEnvironmentVariables()); if (envs[0]==null || envs[1]==null) throw new CmdLineException("This CLI command works only when invoked from inside a build"); Job j = Hudson.getInstance().getItemByFullName(envs[0],Job.class); if (j==null) throw new CmdLineException("No such job: "+envs[0]); try { Run r = j.getBuildByNumber(Integer.parseInt(envs[1])); if (r==null) throw new CmdLineException("No such build #"+envs[1]+" in "+envs[0]); return r; } catch (NumberFormatException e) { throw new CmdLineException("Invalid build number: "+envs[1]); } } catch (IOException e) { throw new CmdLineException("Failed to identify the build being executed",e); } catch (InterruptedException e) { throw new CmdLineException("Failed to identify the build being executed",e); } }
CLICommand c = CLICommand.getCurrent(); if (c == null) { throw new IllegalStateException("Not executing a CLI command");