Class: Autoproj::Jenkins::Server
- Inherits:
-
Object
- Object
- Autoproj::Jenkins::Server
- Defined in:
- lib/autoproj/jenkins/server.rb
Overview
The interface to a Jenkins server
Instance Attribute Summary collapse
-
#client ⇒ JenkinsApi::Client
readonly
The underlying client.
-
#jobs ⇒ JenkinsApi::Client::Job
readonly
The job-related API.
Instance Method Summary collapse
-
#add_downstream_project(upstream_job_name, downstream_job_name) ⇒ Object
Add a job as a downstream job of another.
-
#create_job(job_name, template, pipeline: '', **parameters) ⇒ Object
Create a job using a rendered template.
-
#create_or_reset_job(job_name, template, pipeline: '', **parameters) ⇒ Object
Either create or reset a job, depending on whether it already exists or not.
-
#delete_job(job_name) ⇒ Object
Delete a job.
-
#has_job?(job_name) ⇒ Boolean
Test whether the server already has a job.
-
#initialize(**options) ⇒ Server
constructor
A new instance of Server.
-
#read_job_config(job_name) ⇒ REXML::Document
Read a job configuration.
-
#render_pipeline(job_name, template, **parameters) ⇒ Object
Render a pipeline script.
-
#reset_job(job_name, template, pipeline: '', **parameters) ⇒ Object
Reset the configuration of an existing job.
-
#trigger_job(job_name) ⇒ Object
Trigger a job.
-
#update_job_config(job_name, xml) ⇒ Object
Update a job configuration.
-
#update_job_pipeline(job_name, template, **parameters) ⇒ Object
Update the pipeline of the given job.
-
#update_pipeline_in_config(config, pipeline) ⇒ Object
Update the pipeline script in a job config.
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(**) @client = JenkinsApi::Client.new(log_location: STDERR, **) @jobs = JenkinsApi::Client::Job.new(client) end |
Instance Attribute Details
#client ⇒ JenkinsApi::Client (readonly)
The underlying client
7 8 9 |
# File 'lib/autoproj/jenkins/server.rb', line 7 def client @client end |
#jobs ⇒ JenkinsApi::Client::Job (readonly)
The job-related API
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
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
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
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
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
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
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
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 |