Class: JenkinsApi::CLI::Job

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/jenkins_api_client/cli/job.rb

Overview

This class provides various command line operations related to jobs.

Instance Method Summary collapse

Instance Method Details

#build(job) ⇒ Object

CLI command to build a job given the name of the job

Parameters:

  • job (String)

    Name of the job



64
65
66
67
# File 'lib/jenkins_api_client/cli/job.rb', line 64

def build(job)
  @client = Helper.setup(parent_options)
  @client.job.build(job)
end

#console(job) ⇒ Object

CLI command to obtain console output for a job

Parameters:

  • job (String)

    Name of the job



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/jenkins_api_client/cli/job.rb', line 96

def console(job)
  @client = Helper.setup(parent_options)
  # If debug is enabled, disable it. It shouldn't interfere
  # with console output.
  debug_changed = false
  if @client.debug == true
    @client.debug = false
    debug_changed = true
  end

  # Print progressive console output
  response = @client.job.get_console_output(job)
  puts response['output'] unless response['more']
  while response['more']
    size = response['size']
    puts response['output'] unless response['output'].chomp.empty?
    sleep options[:sleep].to_i if options[:sleep]
    response = @client.job.get_console_output(job, 0, size)
  end
  # Print the last few lines
  puts response['output'] unless response['output'].chomp.empty?
  # Change the debug back if we changed it now
  @client.toggle_debug if debug_changed
end

#delete(job) ⇒ Object

CLI command to delete a job

Parameters:

  • job (String)

    Name of the job



84
85
86
87
# File 'lib/jenkins_api_client/cli/job.rb', line 84

def delete(job)
  @client = Helper.setup(parent_options)
  puts @client.job.delete(job)
end

#listObject

CLI command to list all jobs in Jenkins or the ones matched by status or a regular expression



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jenkins_api_client/cli/job.rb', line 38

def list
  @client = Helper.setup(parent_options)
  if options[:filter] && options[:status]
    name_filtered = @client.job.list(options[:filter])
    puts @client.job.list_by_status(options[:status], name_filtered)
  elsif options[:filter]
    puts @client.job.list(options[:filter])
  elsif options[:status]
    puts @client.job.list_by_status(options[:status])
  else
    puts @client.job.list_all
  end
end

#recreate(job) ⇒ Object

CLI command to recreate a job on Jenkins



54
55
56
57
# File 'lib/jenkins_api_client/cli/job.rb', line 54

def recreate(job)
  @client = Helper.setup(parent_options)
  @client.job.recreate(job)
end

#restrict(job) ⇒ Object

CLI command to restrict a job to a node

Parameters:

  • job (String)

    Name of the job



127
128
129
130
131
132
133
134
# File 'lib/jenkins_api_client/cli/job.rb', line 127

def restrict(job)
  @client = Helper.setup(parent_options)
  if options[:node]
    @client.job.restrict_to_node(job, options[:node])
  else
    say "You need to specify the node to be restricted to.", :red
  end
end

#status(job) ⇒ Object

CLI command to get the status of a job

Parameters:

  • job (String)

    Name of the job



74
75
76
77
# File 'lib/jenkins_api_client/cli/job.rb', line 74

def status(job)
  @client = Helper.setup(parent_options)
  puts @client.job.get_current_build_status(job)
end