Created
July 17, 2012 12:34
-
-
Save hitode909/3129177 to your computer and use it in GitHub Desktop.
Jenkinsにブランチを登録するスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
# -*- coding: utf-8 -*- | |
# Jenkinsにブランチを登録するスクリプト | |
# | |
# 現在のブランチを追加 | |
# jenkins_register_branch http://jenkins.example.com/job/Project1 | |
# 現在のブランチにstagingブランチを追加 | |
# jenkins_register_branch http://jenkins.example.com/job/Project1 staging | |
require 'rubygems' | |
require 'uri' | |
require 'open-uri' | |
require 'nokogiri' | |
if ARGV.length < 1 | |
warn "usage: $0 [PROJECT URL] ([BRANCH])" | |
exit 1 | |
end | |
project_url = ARGV[0] | |
ENDPOINT = URI.parse "#{project_url}/config.xml" | |
# いまみてるブランチ | |
def git_current_branch | |
branch = `git rev-parse --symbolic-full-name HEAD`.chomp.gsub(/^refs\/heads\//, '') | |
raise 'HEAD' if branch == 'HEAD' | |
branch | |
end | |
# origin/つける | |
def originalize_branch(branch) | |
return branch if branch =~ /^origin\// | |
"origin/#{branch}" | |
end | |
# すでに登録されているか | |
def already_registered_branch(xml, branch) | |
branches = xml.xpath('//hudson.plugins.git.BranchSpec').to_a | |
branches.any?{ |branch_tag| | |
branch_tag.at('name').text == branch | |
} | |
end | |
# XMLに追記 | |
def add_branch_to_xml(xml, branch) | |
last_branch = xml.xpath('//hudson.plugins.git.BranchSpec').last | |
last_branch.after("<hudson.plugins.git.BranchSpec><name>#{branch}</name></hudson.plugins.git.BranchSpec>") | |
end | |
# JenkinsにPOST | |
def post_config(body) | |
url = ENDPOINT | |
req = Net::HTTP::Post.new(url.path) | |
http = Net::HTTP.new(url.host, url.port) | |
http.request(req, body) | |
end | |
branch = originalize_branch(ARGV[1] || git_current_branch) | |
xml = Nokogiri open(ENDPOINT).read | |
if already_registered_branch(xml, branch) | |
warn "branch #{branch} already registered" | |
exit 1 | |
end | |
add_branch_to_xml(xml, branch) | |
post_config(xml.to_xml) | |
warn "branch #{branch} registered" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment