class Autoproj::Jenkins::Server

The interface to a Jenkins server

Attributes

client[R]

The underlying client

@return [JenkinsApi::Client]

jobs[R]

The job-related API

@return [JenkinsApi::Client::Job]

Public Class Methods

new(**options) click to toggle source
# File lib/autoproj/jenkins/server.rb, line 14
def initialize(**options)
    @client = JenkinsApi::Client.new(log_location: STDERR, **options)
    @jobs = JenkinsApi::Client::Job.new(client)
end

Public Instance Methods

add_downstream_project(upstream_job_name, downstream_job_name) click to toggle source

Add a job as a downstream job of another

# File lib/autoproj/jenkins/server.rb, line 116
def add_downstream_project(upstream_job_name, downstream_job_name)
    jobs.add_downstream_projects(upstream_job_name, downstream_job_name, 'success', false)
end
create_job(job_name, template, pipeline: '', **parameters) click to toggle source

Create a job using a rendered template

@param [String] job_name the name of the new job @param [String] template the name of the template @param [Hash] parameters parameters for the template rendering @raise (see Autoproj::Jenkins.render_template)

# File lib/autoproj/jenkins/server.rb, line 25
def create_job(job_name, template, pipeline: '', **parameters)
    xml = Autoproj::Jenkins.render_template(template, **parameters)
    xml = update_pipeline_in_config(xml, pipeline)
    jobs.create(job_name, xml)
end
create_or_reset_job(job_name, template, pipeline: '', **parameters) click to toggle source

Either create or reset a job, depending on whether it already exists or not

@param [String,nil] pipeline the job’s pipeline script

# File lib/autoproj/jenkins/server.rb, line 56
def create_or_reset_job(job_name, template, pipeline: '', **parameters)
    xml = Autoproj::Jenkins.render_template(template, **parameters)
    xml = update_pipeline_in_config(xml, pipeline)

    if has_job?(job_name)
        jobs.update(job_name, xml.to_s)
    else
        jobs.create(job_name, xml.to_s)
    end
end
delete_job(job_name) click to toggle source

Delete a job

@param [String] job_name

# File lib/autoproj/jenkins/server.rb, line 98
def delete_job(job_name)
    jobs.delete(job_name)
end
has_job?(job_name) click to toggle source

Test whether the server already has a job

@param [String] job_name

# File lib/autoproj/jenkins/server.rb, line 75
def has_job?(job_name)
    jobs.exists?(job_name)
end
read_job_config(job_name) click to toggle source

Read a job configuration

@return [REXML::Document]

# File lib/autoproj/jenkins/server.rb, line 82
def read_job_config(job_name)
    xml_config = jobs.get_config(job_name)
    REXML::Document.new(xml_config)
end
render_pipeline(job_name, template, **parameters) click to toggle source

Render a pipeline script

# File lib/autoproj/jenkins/server.rb, line 68
def render_pipeline(job_name, template, **parameters)
    Autoproj::Jenkins.render_template(template, **parameters)
end
reset_job(job_name, template, pipeline: '', **parameters) click to toggle source

Reset the configuration of an existing job

@param [String] job_name the name of the new job @param [String] template the name of the template @param [Hash] parameters parameters for the template rendering @raise (see Autoproj::Jenkins.render_template)

# File lib/autoproj/jenkins/server.rb, line 37
def reset_job(job_name, template, pipeline: '', **parameters)
    xml = Autoproj::Jenkins.render_template(template, **parameters)
    xml = update_pipeline_in_config(xml, pipeline)
    jobs.update(job_name, xml)
end
trigger_job(job_name) click to toggle source

Trigger a job

# File lib/autoproj/jenkins/server.rb, line 103
def trigger_job(job_name)
    jobs.build(job_name)
end
update_job_config(job_name, xml) click to toggle source

Update a job configuration

@param [String] job_name @param [REXML::Document] xml the configuration XML document

# File lib/autoproj/jenkins/server.rb, line 91
def update_job_config(job_name, xml)
    jobs.update(job_name, xml.to_s)
end
update_job_pipeline(job_name, template, **parameters) click to toggle source

Update the pipeline of the given job

# File lib/autoproj/jenkins/server.rb, line 108
def update_job_pipeline(job_name, template, **parameters)
    config = read_job_config(job_name)
    pipeline = Autoproj::Jenkins.render_template(template, **parameters)
    update_pipeline_in_config(config, pipeline)
    update_job_config(job_name, config)
end
update_pipeline_in_config(config, pipeline) click to toggle source

Update the pipeline script in a job config

# File lib/autoproj/jenkins/server.rb, line 44
def update_pipeline_in_config(config, pipeline)
    if config.respond_to?(:to_str)
        config = REXML::Document.new(config)
    end
    config.elements['//definition/script'].text = pipeline
    config.to_s
end