Class: Autoproj::Jenkins::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/autoproj/jenkins/server.rb

Overview

The interface to a Jenkins server

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Server

Returns a new instance of Server.



14
15
16
17
# 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

Instance Attribute Details

#clientJenkinsApi::Client (readonly)

The underlying client

Returns:

  • (JenkinsApi::Client)


7
8
9
# File 'lib/autoproj/jenkins/server.rb', line 7

def client
  @client
end

#jobsJenkinsApi::Client::Job (readonly)

The job-related API

Returns:

  • (JenkinsApi::Client::Job)


12
13
14
# File 'lib/autoproj/jenkins/server.rb', line 12

def jobs
  @jobs
end

Instance Method Details

#add_downstream_project(upstream_job_name, downstream_job_name) ⇒ Object

Add a job as a downstream job of another



116
117
118
# 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) ⇒ Object

Create a job using a rendered template

Parameters:

  • job_name (String)

    the name of the new job

  • template (String)

    the name of the template

  • parameters (Hash)

    parameters for the template rendering



25
26
27
28
29
# 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) ⇒ Object

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

Parameters:

  • pipeline (String, nil) (defaults to: '')

    the job's pipeline script



56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Object

Delete a job

Parameters:

  • job_name (String)


98
99
100
# File 'lib/autoproj/jenkins/server.rb', line 98

def delete_job(job_name)
    jobs.delete(job_name)
end

#has_job?(job_name) ⇒ Boolean

Test whether the server already has a job

Parameters:

  • job_name (String)

Returns:

  • (Boolean)


75
76
77
# File 'lib/autoproj/jenkins/server.rb', line 75

def has_job?(job_name)
    jobs.exists?(job_name)
end

#read_job_config(job_name) ⇒ REXML::Document

Read a job configuration

Returns:

  • (REXML::Document)


82
83
84
85
# 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) ⇒ Object

Render a pipeline script



68
69
70
# 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) ⇒ Object

Reset the configuration of an existing job

Parameters:

  • job_name (String)

    the name of the new job

  • template (String)

    the name of the template

  • parameters (Hash)

    parameters for the template rendering



37
38
39
40
41
# 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) ⇒ Object

Trigger a job



103
104
105
# File 'lib/autoproj/jenkins/server.rb', line 103

def trigger_job(job_name)
    jobs.build(job_name)
end

#update_job_config(job_name, xml) ⇒ Object

Update a job configuration

Parameters:

  • job_name (String)
  • xml (REXML::Document)

    the configuration XML document



91
92
93
# 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) ⇒ Object

Update the pipeline of the given job



108
109
110
111
112
113
# 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) ⇒ Object

Update the pipeline script in a job config



44
45
46
47
48
49
50
# 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