jenkins-build 0.1.2

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2ae30a4dd90c885908bcb22da2a2ab46ac401733
4
+ data.tar.gz: 49d83dc73f72f4226069340db1433e9dd51d28e7
5
+ SHA512:
6
+ metadata.gz: 9e1a46947d32df1bf5d33243f0b2f08f89ba5d29b9f2a7dfedf1485b6edef03ec8e1cffec21027596b4ddf165e9dfbef9e20eb530cabbcc76032fadebb9023dc
7
+ data.tar.gz: 2b826e4d3477957573b7cb39ca39fbfb0f350e7019d779aa0df309f75da95b62e4bbbe3514725f8a5a35a1a44c8389d776764a06cbccf9f2c20edb658842489f
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .jenkins-build
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jenkins-build.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # jenkins-build
2
+
3
+
4
+ ## Installation
5
+
6
+ Install it from rubygems.org:
7
+
8
+ ```shell
9
+ gem install jenkins-build
10
+ ```
11
+
12
+
13
+ ## Usage
14
+
15
+ You have to configure `jenkins-build` to use your jenkins server and project.
16
+ Do that by running:
17
+
18
+ ```shell
19
+ jenkins-build configure
20
+ ```
21
+
22
+ And then you can trigger build by running:
23
+
24
+ ```shell
25
+ jenkins-build trigger
26
+ ```
27
+
28
+ To get your API Key go to your Jenkins, log in, in the right corner click your name, then click
29
+ Configuration in the left sidebar and finally press 'Show API Token'.
30
+
31
+ ## Development
32
+
33
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
34
+
35
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it ( https://github.com/3scale/jenkins-build/fork )
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/jenkins-build ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'jenkins/build'
4
+
5
+ Jenkins::Build::CLI.start
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jenkins/build/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jenkins-build"
8
+ spec.version = Jenkins::Build::VERSION
9
+ spec.authors = ["Michal Cichra"]
10
+ spec.email = ["michal@3scale.net"]
11
+
12
+
13
+ spec.summary = %q{CLI utility to trigger jobs on jenkins.}
14
+ spec.description = %q{Using Github Pull Request Plugin and Jenkins with GitHub authentication}
15
+ spec.homepage = "https://github.com/3scale/jenkins-build"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.8"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+
24
+ spec.add_development_dependency 'rspec', '~> 3.1'
25
+
26
+ spec.add_dependency 'thor'
27
+ end
@@ -0,0 +1,9 @@
1
+ module Jenkins
2
+ module Build
3
+ autoload :Git, 'jenkins/build/git'
4
+ autoload :CLI, 'jenkins/build/cli'
5
+ autoload :Configuration, 'jenkins/build/configuration'
6
+ autoload :Client, 'jenkins/build/client'
7
+ autoload :VERSION, 'jenkins/build/version'
8
+ end
9
+ end
@@ -0,0 +1,49 @@
1
+ require 'thor'
2
+
3
+ module Jenkins
4
+ module Build
5
+ class CLI < Thor
6
+
7
+ extend Jenkins::Build::Git
8
+
9
+ desc "configure", "configures this project"
10
+ option :project, desc: 'Jenkins project name', required: true
11
+ option :server, desc: 'Jenkins server', required: true
12
+ option :user, desc: 'Your User', required: true
13
+ option :api_key, desc: 'API Key (http://<server>/user/<user>/configure)', required: true
14
+
15
+ def configure
16
+ configuration.merge!(options)
17
+
18
+ configuration.write
19
+ end
20
+
21
+ option :branch, desc: 'Git branch to build', default: current_branch
22
+
23
+ desc 'trigger', 'triggers build of a branch'
24
+ def trigger
25
+ unless configuration.exists?
26
+ warn "must run: 'jenkins-build configure' first"
27
+ exit(1)
28
+ end
29
+
30
+ client.trigger(branch)
31
+ puts "Triggered build of #{configuration.project} with branch #{branch}."
32
+ end
33
+
34
+ private
35
+
36
+ def branch
37
+ options[:branch]
38
+ end
39
+
40
+ def client
41
+ @client ||= Jenkins::Build::Client.new(configuration)
42
+ end
43
+
44
+ def configuration
45
+ @configuration ||= Jenkins::Build::Configuration.new(Dir.pwd)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,61 @@
1
+ require 'uri'
2
+ require 'json'
3
+ require 'net/http'
4
+
5
+ module Jenkins
6
+ module Build
7
+ class Client
8
+ def initialize(configuration)
9
+ @configuration = configuration
10
+
11
+ @base_uri = URI(@configuration.server).freeze
12
+
13
+ @connection = Net::HTTP.new(@base_uri.host, @base_uri.port)
14
+ @connection.use_ssl = (@base_uri.scheme == 'https')
15
+
16
+ @connection
17
+ end
18
+
19
+ def trigger(branch)
20
+ response = post("/job/#{@configuration.project}/build", parameter: { name: :sha1, value: branch })
21
+ case response
22
+ when Net::HTTPCreated then true
23
+ else
24
+ raise InvalidResponse, response
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ class InvalidResponse < StandardError
31
+ attr_reader :code, :message
32
+
33
+ def initialize(response)
34
+ @code = response.code
35
+ @message = response.message
36
+ @response = response
37
+ super("Unexpected HTTP Response: #{@message}")
38
+ end
39
+ end
40
+
41
+ def post(path, params)
42
+ uri = URI.join(@base_uri, path)
43
+ body = JSON.generate(params)
44
+
45
+ post = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
46
+ post.set_form_data json: body
47
+
48
+ request(post)
49
+ end
50
+
51
+ def request(request)
52
+ request.basic_auth(@configuration.user, @configuration.api_key)
53
+
54
+ case response = @connection.request(request)
55
+ when Net::HTTPSuccess then return response
56
+ else raise InvalidResponse, response
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,54 @@
1
+ require 'pathname'
2
+ require 'yaml/store'
3
+
4
+ module Jenkins
5
+ module Build
6
+ class Configuration
7
+
8
+ CONFIG = Pathname('.jenkins-build').freeze
9
+
10
+ CONFIG_KEYS = [:server, :project, :api_key, :user]
11
+
12
+ def initialize(folder)
13
+ @config = Pathname(CONFIG).expand_path(folder).freeze
14
+ @store = YAML::Store.new(@config)
15
+ end
16
+
17
+ CONFIG_KEYS.each do |key|
18
+ define_method(key) do
19
+ options.fetch(key)
20
+ end
21
+ end
22
+
23
+ def exists?
24
+ @config.exist?
25
+ end
26
+
27
+ def merge!(opts)
28
+ CONFIG_KEYS.each do |key|
29
+ options[key] = opts[key]
30
+ end
31
+ end
32
+
33
+ def options
34
+ @options ||= read
35
+ end
36
+
37
+ def read
38
+ @store.transaction(true) do
39
+ @options = CONFIG_KEYS.map do |key|
40
+ [ key, @store[key] ]
41
+ end.to_h
42
+ end
43
+ end
44
+
45
+ def write(options = @options)
46
+ @store.transaction do
47
+ CONFIG_KEYS.each do |key|
48
+ @store[key] = options.fetch(key)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,9 @@
1
+ module Jenkins
2
+ module Build
3
+ module Git
4
+ def current_branch
5
+ `git symbolic-ref --short HEAD`.strip
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Jenkins
2
+ module Build
3
+ VERSION = "0.1.2"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jenkins-build
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Michal Cichra
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: thor
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Using Github Pull Request Plugin and Jenkins with GitHub authentication
70
+ email:
71
+ - michal@3scale.net
72
+ executables:
73
+ - jenkins-build
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - README.md
82
+ - Rakefile
83
+ - bin/jenkins-build
84
+ - jenkins-build.gemspec
85
+ - lib/jenkins/build.rb
86
+ - lib/jenkins/build/cli.rb
87
+ - lib/jenkins/build/client.rb
88
+ - lib/jenkins/build/configuration.rb
89
+ - lib/jenkins/build/git.rb
90
+ - lib/jenkins/build/version.rb
91
+ homepage: https://github.com/3scale/jenkins-build
92
+ licenses: []
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.5
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: CLI utility to trigger jobs on jenkins.
114
+ test_files: []