Skip to content

Instantly share code, notes, and snippets.

@skayred
Last active September 26, 2017 08:37
Show Gist options
  • Save skayred/600d439f7172eddb368915bfded9d93e to your computer and use it in GitHub Desktop.
Save skayred/600d439f7172eddb368915bfded9d93e to your computer and use it in GitHub Desktop.
require 'telegram/bot'
require '/Users/sk_/projects/builder_bot/jenkins'
token = 'TELEGRAM_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
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 => 'APIKEY')
@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment