0

I created a job that eventually seem to work

So I decided to erase all the unsuccessful builds history with these lines that I passed to Script Console:

// change this variable to match the name of the job whose builds you want to delete
def jobName = "JOB_NAME_custom_binary"
def job = Jenkins.instance.getItem(jobName)

job.getBuilds().each { it.delete() }
// uncomment these lines to reset the build number to 1
job.nextBuildNumber = 1
job.save()

It removed all build from history, but now if I try to run build (select job option "Build with parameters" --> tap "Build" button) nothing seem to happen (no build started) - just a screen like below

enter image description here

Did I break something with that script? Can I fix it somehow or only re-create the job?

JaSON
  • 4,843
  • 2
  • 8
  • 15

1 Answers1

1

almost everything you see on UI exist as files on file system

under /var/jenkins_home/jobs/PATH_TO_YOUR_JOB

there you will find config.xml which represents job config

and builds folder with folders for each build. usually with log files from console and some additional files

if builds(can have different name because of pipeline type) folder is empty then you can not restore items which your removed

i created "test" job in root jenkins with 2 parameters and run it 2 times

# ls -la /var/jenkins_home/jobs/test/builds
drwxr-xr-x 4 jenkins jenkins 4096 Aug  1 21:36 .
drwxr-xr-x 3 jenkins jenkins 4096 Aug  1 21:36 ..
drwxr-xr-x 3 jenkins jenkins 4096 Aug  1 21:36 1
drwxr-xr-x 3 jenkins jenkins 4096 Aug  1 21:36 2
-rw-r--r-- 1 jenkins jenkins    0 Aug  1 21:33 legacyIds
-rw-r--r-- 1 jenkins jenkins  126 Aug  1 21:36 permalinks

# cat permalinks 
lastCompletedBuild 2
lastFailedBuild -1
lastStableBuild 2
lastSuccessfulBuild 2
lastUnstableBuild -1
lastUnsuccessfulBuild -1

test/builds/2$ cat build.xml

...
<parameters>
  <hudson.model.BooleanParameterValue>
    <name>user</name>
    <value>false</value>
  </hudson.model.BooleanParameterValue>
  <hudson.model.StringParameterValue>
    <name>ENV</name>
    <value>222222</value>
  </hudson.model.StringParameterValue>
</parameters>

...

with saved parameters

after your script execution

cat permalinks 
lastCompletedBuild -1
lastFailedBuild -1
lastStableBuild -1
lastSuccessfulBuild -1
lastUnstableBuild -1
lastUnsuccessfulBuild -1
jenkins@fc67fb3fb752:~/jobs/test/builds$ ls -la 
total 12
drwxr-xr-x 2 jenkins jenkins 4096 Aug  1 21:45 .
drwxr-xr-x 3 jenkins jenkins 4096 Aug  1 21:45 ..
-rw-r--r-- 1 jenkins jenkins    0 Aug  1 21:33 legacyIds
-rw-r--r-- 1 jenkins jenkins  129 Aug  1 21:45 permalinks

there are no build directories


your script removed ALL builds for job (you dont have condition if success in it)

should be something like

import jenkins.model.Jenkins
import hudson.model.Result

// change this variable to match the name of the job whose builds you want to delete
def jobName = "JOB_NAME_custom_binary"
def job = Jenkins.instance.getItem(jobName)

job.getBuilds().each { build -> 
    if (build.getResult() != Result.SUCCESS) {
        build.delete() 
    }
}
// depends on what you need you need to detect it by your needs.
// job.nextBuildNumber = 1

your post title differs to description... it's unclear what exactly you need

how to trigger build now button via groovy see this post https://stackoverflow.com/a/24262800/6727228

Ruslan
  • 444
  • 3
  • 8