Skip to content

Instantly share code, notes, and snippets.

@skayred
Last active June 20, 2017 11:29
Show Gist options
  • Save skayred/9b34e7b68369e4f73dd0871305677497 to your computer and use it in GitHub Desktop.
Save skayred/9b34e7b68369e4f73dd0871305677497 to your computer and use it in GitHub Desktop.
require 'telegram/bot'
require '/Users/sk_/projects/builder_bot/jenkins'
token = 'TOKEN'
loop do
begin
Telegram::Bot::Client.run(token) do |bot|
bot.options[:timeout] = 1
bot.listen do |message|
case message.text
when '/ping'
bot.api.send_message(chat_id: message.chat.id, text: "Pong #{Socket.gethostname}")
when /\/build/
bot.api.send_message(chat_id: message.chat.id, text: "Build #{message}")
params = message.text.split(' ')
params.shift
begin
builder = Builder.new(*params)
builder.run {
bot.api.send_message(chat_id: message.chat.id, text: "Build finished #{message}")
}
rescue Exception => e
puts e
bot.api.send_message(chat_id: message.chat.id, text: "Build failed :(")
end
end
end
end
rescue
puts 'failed, restart'
end
end
source 'https://rubygems.org'
gem 'jenkins_api_client', :git => 'https://github.com/arangamani/jenkins_api_client'
gem 'telegram-bot-ruby'
gem 'daemons'
require 'jenkins_api_client'
CONFIG = {
front: {
dev: [
'/Frontend/job/Microservices/job/Content/job/Build/',
'/Frontend/job/Microservices/job/Content/job/DeployOnDev/',
],
beta: [
'/Frontend/job/Microservices/job/Content/job/Build/',
'/Frontend/job/Microservices/job/Content/job/DeployOnStaging/',
],
},
back: {
dev: [
'/Python/job/Microservices/job/Content/job/Build/',
'/Python/job/Microservices/job/Content/job/DeployOnDev/',
],
beta: [
'/Python/job/Microservices/job/Content/job/Build/',
'/Python/job/Microservices/job/Content/job/DeployOnStagingOldDB/',
],
},
}
class Builder
def initialize(project, stage, stage_number, branch, back_branch)
@client = JenkinsApi::Client.new(
:server_ip => '192.168.16.188',
:username => 'dsavchenko',
:password => 'PASWD')
@project = project.to_sym
@stage = stage.to_sym
@stage_number = stage_number
@branch = branch
@back_branch = back_branch
end
def run(&block)
build_config = CONFIG[@project][@stage]
run_job(build_config[0])
run_job(build_config[1], build_config[0])
run_job(nil, build_config[1]) {
yield
}
end
def run_job(name, chain = nil, &block)
run_state = 'not_running'
if chain
loop do
sleep(20)
run_state = @client.job.get_current_build_status(chain)
break if (run_state != "running")
end
return unless run_state == 'success'
yield if block
end
@client.job.build(name, {
BRANCH_NAME: @branch,
BACKEND_BRANCH: @back_branch,
STAGE_ID: @stage_number,
}) if name
end
end
require 'rubygems'
require 'daemons'
module Daemons
class Application
def logfile; '/Users/sk_/projects/builder_bot/f1'; end
def output_logfile; '/Users/sk_/projects/builder_bot/f2'; end
end
end
Daemons.run '/Users/sk_/projects/builder_bot/build_bot.rb',
:dir => '/Users/sk_/projects/builder_bot/pids',
:dir_mode => :normal,
:ontop => false,
:log_output => true do
loop do
sleep(5)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment