capistrano-jenkins 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in capistrano-jenkins.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,14 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ capistrano-jenkins (0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+
10
+ PLATFORMS
11
+ ruby
12
+
13
+ DEPENDENCIES
14
+ capistrano-jenkins!
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 - 2011 Martin Jonsson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # capistrano-jenkins
2
+
3
+ Capistrano recipe to verify build status on Jenkins
4
+
5
+ ## Installation
6
+
7
+ gem install capistrano-jenkins
8
+
9
+ ## Options
10
+
11
+ **:jenkins_url**
12
+
13
+ URL to jenkins, for password protected use format "http://username:pass@my.jenkins.com".
14
+
15
+ By default it will check for the URL in the environment variable JENKINS_URL.
16
+
17
+ **:jenkins_job_name**
18
+
19
+ The job name on jenkins
20
+
21
+ **:jenkins_retry_sleep**
22
+
23
+ How long between retries if build currently in progress.
24
+
25
+ ## Usage
26
+
27
+ Include the recipe
28
+
29
+ require 'capistrano-jenkins'
30
+
31
+ Set required parameters
32
+
33
+ set :branch, "master"
34
+ set :jenkins_job_name, "MyProject"
35
+ set :jenkins_url, "http://username:pass@my.jenkins.com"
36
+
37
+
38
+ Add the task, e.g on before
39
+
40
+ before "jenkins:verify_build"
@@ -0,0 +1,12 @@
1
+ # -*- encoding: utf-8 -*-
2
+ Gem::Specification.new do |s|
3
+ s.name = "capistrano-jenkins"
4
+ s.version = "0.1"
5
+ s.authors = ["Martin Jonsson"]
6
+ s.email = ["martin.jonsson@gmail.com"]
7
+ s.homepage = "http://github.com/martinj/capistrano-jenkins"
8
+ s.summary = "Capistrano Jenkins recipe"
9
+ s.description = "Capistrano recipe to validate the build on jenkins status before deploying"
10
+ s.files = `git ls-files`.split("\n")
11
+ s.require_paths = ["lib"]
12
+ end
@@ -0,0 +1,84 @@
1
+ # encoding: utf-8
2
+ require 'capistrano'
3
+ require 'colored'
4
+ require 'json'
5
+
6
+ def _cset(name, *args, &block)
7
+ unless exists?(name)
8
+ set(name, *args, &block)
9
+ end
10
+ end
11
+
12
+ Capistrano::Configuration.instance.load do
13
+ _cset(:jenkins_url) {
14
+ abort "Please specify the jenkins URL eiter in :jenkins_url or in environment JENKINS_URL, http://username:password@jenkins.com".red if ENV['JENKINS_URL'].nil?
15
+ ENV['JENKINS_URL']
16
+ }
17
+
18
+ _cset(:jenkins_job_name) { abort "Please specify the jenkins job name, set :jenkins_job_name".red }
19
+ _cset(:branch) { abort "Please specify :branch, needed to find correct build".red }
20
+
21
+ _cset(:jenkins_retry_sleep, 10)
22
+
23
+ def fetch_json(url)
24
+ response = `curl -s #{url}`
25
+ json = JSON.load(response)
26
+ end
27
+
28
+ def get_build(sha, data)
29
+ data['builds'].each do |build|
30
+ build['actions'].each do |action|
31
+ next unless action.has_key?('lastBuiltRevision')
32
+ return build if action['lastBuiltRevision']['SHA1'].chomp == sha.chomp
33
+ end
34
+ end
35
+ false
36
+ end
37
+
38
+ def agree(message)
39
+ Capistrano::CLI.ui.agree message
40
+ end
41
+
42
+ def trigger_build
43
+ `curl -s #{jenkins_url}/job/#{jenkins_job_name}/build`
44
+ end
45
+
46
+ namespace :jenkins do
47
+ desc "Check jenkins build status"
48
+ task :verify_build, :except => { :no_release => true } do
49
+ retrying = false
50
+ [0].each do |i|
51
+ sha = `git ls-remote #{repository} #{branch}`.sub!(/\s+.*$/, '').chomp
52
+ build = get_build(sha, fetch_json("#{jenkins_url}/job/#{jenkins_job_name}/api/json?depth=1"))
53
+
54
+ unless build then
55
+ trigger_new_build = agree "Couldn't find a build with sha #{sha} on jenkins. Do you want to trigger build? yes/no: "
56
+ if trigger_new_build then
57
+ trigger_build
58
+ sleep jenkins_retry_sleep
59
+ redo
60
+ else
61
+ abort
62
+ end
63
+ end
64
+
65
+ if build['building'] and not retrying then
66
+ retrying = agree "The requested revision is currently building, Retry? yes/no: "
67
+ if retrying then
68
+ put "Retrying in #{jenkins_retry_sleep} seconds."
69
+ sleep jenkins_retry_sleep
70
+ redo
71
+ end
72
+ elsif build['building'] and retrying
73
+ puts "Still in build progress.. will retry in #{jenkins_retry_sleep} seconds."
74
+ sleep jenkins_retry_sleep
75
+ redo
76
+ end
77
+
78
+ abort "Build for requested release was not successful, cant deploy. Build result was #{build['result']}".red unless build['result'] == "SUCCESS"
79
+ puts "✔ jenkins build verified for revision #{sha}".green
80
+ end
81
+ end
82
+ end
83
+
84
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-jenkins
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Martin Jonsson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-18 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Capistrano recipe to validate the build on jenkins status before deploying
15
+ email:
16
+ - martin.jonsson@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - Gemfile
22
+ - Gemfile.lock
23
+ - LICENSE
24
+ - README.md
25
+ - capistrano-jenkins.gemspec
26
+ - lib/capistrano-jenkins.rb
27
+ homepage: http://github.com/martinj/capistrano-jenkins
28
+ licenses: []
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.8.15
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: Capistrano Jenkins recipe
51
+ test_files: []