I am running a groovy script which needs access to two modules jeninks.model and hudson.model
i tried importing these two via import statement in my jenkinsfile but the issue is still there.
Error: groovy.lang.MissingPropertyException: No such property: build for class: groovy.lang.Binding at groovy.lang.Binding.getVariable(Binding.java:63)
Any solution by which i can do this?
The script is working fine when i am using a freestyle job with Execute System Groovy.
import jenkins.model.*
import hudson.model.*
pipeline{
agent any
stages{
stage('py version'){
steps{
bat 'python --version'
}
}
stage('get jobs'){
get_job()
}
}
}
def get_job(){
def cutOfDate = System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 2
def filename = build.workspace.toString() "/jobs_lists.txt"
targetFile = new File(filename).write("")
targetFile = new File(filename).append("<table><tr><th>Job Name</th><th>Last Build on</th><th>Keep</th><th>username</th></tr>")
println "Cut of Date: " cutOfDate
}
CodePudding user response:
If you just want to access the Workspace DIR you can use the $WORKSPACE environment variable.
Example :
def get_job(){
def cutOfDate = System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 2
println("$WORKSPACE")
def filename = "$WORKSPACE" "/jobs_lists.txt"
targetFile = new File(filename).write("")
targetFile = new File(filename).append("<table><tr><th>Job Name</th><th>Last Build on</th><th>Keep</th><th>username</th></tr>")
println "Cut of Date: " cutOfDate
}
If you want to access the current build context use currentBuild
which will return a RunWrapper. Example below.
def changeset = currentBuild.changeSets
Update : Accessing Jobs from the Pipeline
pipeline {
agent any
stages {
stage('Test') {
steps {
script{
getJobs()
}
}
}
}
}
def getJobs() {
Jenkins.instance.getAllItems(Job.class).each { jobitem ->
def jobName = jobitem.name
def jobInfo = Jenkins.instance.getItem(jobName)
println(jobName)
}
}