@Override public void onLoad(ItemGroup<? extends Item> parent, String name) throws IOException { super.onLoad(parent, name); TextFile f = getNextBuildNumberFile(); if (f.exists()) { // starting 1.28, we store nextBuildNumber in a separate file. // but old Hudson didn't do it, so if the file doesn't exist, // assume that nextBuildNumber was read from config.xml try { synchronized (this) { this.nextBuildNumber = Integer.parseInt(f.readTrim()); } } catch (NumberFormatException e) { // try to infer the value of the next build number from the existing build records. See // JENKINS-11563 File[] folders = this.getBuildDir() .listFiles( new FileFilter() { public boolean accept(File file) { return file.isDirectory() && file.getName().matches("[0-9]+"); } }); if (folders == null || folders.length == 0) { this.nextBuildNumber = 1; } else { Collection<Integer> foldersInt = Collections2.transform( Arrays.asList(folders), new Function<File, Integer>() { public Integer apply(File file) { return Integer.parseInt(file.getName()); } }); this.nextBuildNumber = Collections.max(foldersInt) + 1; } saveNextBuildNumber(); } } else { // From the old Hudson, or doCreateItem. Create this file now. saveNextBuildNumber(); save(); // and delete it from the config.xml } if (properties == null) // didn't exist < 1.72 properties = new CopyOnWriteList<JobProperty<? super JobT>>(); for (JobProperty p : properties) p.setOwner(this); }
/** * Programatically updates the next build number. * * <p>Much of Hudson assumes that the build number is unique and monotonic, so this method can * only accept a new value that's bigger than {@link #getLastBuild()} returns. Otherwise it'll be * no-op. * * @since 1.199 (before that, this method was package private.) */ public synchronized void updateNextBuildNumber(int next) throws IOException { RunT lb = getLastBuild(); if (lb != null ? next > lb.getNumber() : next > 0) { this.nextBuildNumber = next; saveNextBuildNumber(); } }
/** Allocates a new buildCommand number. */ public synchronized int assignBuildNumber() throws IOException { int r = nextBuildNumber++; saveNextBuildNumber(); return r; }