fire-jenkins-builder 0.1.0

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: 65a70568ce2055b134ab92273530b2a9d89dba37
4
+ data.tar.gz: 858977371e0632eef1084d67d5f694a00c685f44
5
+ SHA512:
6
+ metadata.gz: 17c5d97a762b62dc841eb07dedd1d7a78c0f25c51a83726328db48ccb0afa328ad00b84b7050c685f3b8dd273460fbdfe5a336aa6d37dcdd18083ba1c15611f9
7
+ data.tar.gz: 5d9d382d6c13915ed4d8c9938bca2c2505d47eff0110b6b8cb54351c3789a936282bbc8b954150db7c77fa009eb3cff1b4fb36334daceb0e331f2a5fa4c0b7d7
data/.fire-jenkins.yml ADDED
@@ -0,0 +1,37 @@
1
+ # jenkins job 配置文件
2
+ # 可以通过在 commit 中添加 prefix ,使 CI 出发 jenkins job 的创建/构建行为
3
+ # 提交 commit 信息
4
+ # [jenkins create] XXXXXX 创建/更新并执行 jenkins job
5
+ # [jenkins build] XXXXXX 执行 jenkins job
6
+ #
7
+ ########################
8
+ # 选填
9
+ ########################
10
+ # job 参数
11
+ # 添加自己的钉钉消息等健值对
12
+ # 需要注意的是,这里面的配置,模版必须已经存在了,这里只是修改对应的值
13
+ parameters:
14
+ REPORTER_ACCESS_TOKEN: XXXXXX
15
+ DEBUG: true
16
+
17
+ ########################
18
+ # 必填
19
+ ########################
20
+ # 打包 job 名称 / 前缀名
21
+ # 如果 job_name 为空,则采用 job_name_prefix + 分支名称
22
+ # job_name: XXXX
23
+ job_name_prefix: ZGiOS_
24
+ # jenkins 上的分组,对应 view
25
+ job_view: 掌柜iOS
26
+
27
+ # jenkins 用户名密码
28
+ username: qingmu
29
+ password: 1
30
+
31
+ # 各业务线采用不同的模版,配置这里
32
+ # 模版工程名称(需要唯一)
33
+ template_job_name: ZGiOS_develop
34
+
35
+ # 这里不动
36
+ server_url: 'http://jenkins-shopkeeper-client.2dfire.net'
37
+ server_port: 80
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at triplec.linux@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in fire-jenkins-builder.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,39 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fire-jenkins-builder (0.1.0)
5
+ jenkins_api_client
6
+ nokogiri
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ jenkins_api_client (1.5.3)
12
+ json (>= 1.0)
13
+ mixlib-shellout (>= 1.1.0)
14
+ nokogiri (~> 1.6)
15
+ socksify (>= 1.7.0)
16
+ terminal-table (>= 1.4.0)
17
+ thor (>= 0.16.0)
18
+ json (2.1.0)
19
+ mini_portile2 (2.3.0)
20
+ mixlib-shellout (2.4.0)
21
+ nokogiri (1.8.4)
22
+ mini_portile2 (~> 2.3.0)
23
+ rake (10.5.0)
24
+ socksify (1.7.1)
25
+ terminal-table (1.8.0)
26
+ unicode-display_width (~> 1.1, >= 1.1.1)
27
+ thor (0.20.0)
28
+ unicode-display_width (1.4.0)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ bundler (~> 1.16)
35
+ fire-jenkins-builder!
36
+ rake (~> 10.0)
37
+
38
+ BUNDLED WITH
39
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 tripleCC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,113 @@
1
+ # Fire::Jenkins::Builder
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/fire/jenkins/builder`. To experiment with that code, run `jb` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'fire-jenkins-builder'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install fire-jenkins-builder
22
+
23
+ ## Usage
24
+
25
+ cli :
26
+
27
+ ```sh
28
+ jb -p PATH -b BRANCH -l LOG_LEVEL
29
+ ```
30
+
31
+ 添加如下配置文件至目标根目录下:
32
+
33
+ ```yml
34
+ # jenkins job 配置文件
35
+ # 可以通过在 commit 中添加 prefix ,使 CI 出发 jenkins job 的创建/构建行为
36
+ # 提交 commit 信息
37
+ # [jb] XXXXXX 执行 jenkins job ,如果不存在 job ,则会在对应 job_view 下创建并执行
38
+ #
39
+ ########################
40
+ # 选填
41
+ ########################
42
+ # job 参数
43
+ # 添加自己的钉钉消息等健值对
44
+ # 需要注意的是,这里面的配置,模版必须已经存在了,这里只是修改对应的值
45
+ parameters:
46
+ REPORTER_ACCESS_TOKEN: XXXXXX
47
+ DEBUG: true
48
+
49
+ ########################
50
+ # 必填
51
+ ########################
52
+ # 打包 job 名称 / 前缀名
53
+ # 如果 job_name 为空,则采用 job_name_prefix + 分支名称
54
+ # job_name: XXXX
55
+ job_name_prefix: ZGiOS_
56
+ # jenkins 上的分组,对应 view
57
+ job_view: 掌柜iOS
58
+
59
+ # jenkins 用户名密码
60
+ username: qingmu
61
+ password: xxx
62
+
63
+ # 各业务线采用不同的模版,配置这里
64
+ # 模版工程名称(需要唯一)
65
+ template_job_name: ZGiOS_develop
66
+
67
+ # 这里不动
68
+ server_url: 'http://jenkins-shopkeeper-client.2dfire.net'
69
+ server_port: 80
70
+ ```
71
+
72
+ 配置 `.gitlab-ci.yml` :
73
+
74
+ ```sh
75
+ stages:
76
+ ...
77
+ - build
78
+ ...
79
+
80
+ ...
81
+ jenkins_build:
82
+ before_script:
83
+ - gem install jenkins_api_client
84
+ - gem install nokogiri
85
+ stage: build
86
+ only:
87
+ variables:
88
+ - $CI_COMMIT_MESSAGE =~ /^\[jb\]/
89
+ script:
90
+ - jb -p .fire-jenkins.yml -b $CI_COMMIT_REF_NAME
91
+ tags:
92
+ - iOSCI
93
+ allow_failure: true
94
+ ...
95
+ ```
96
+
97
+ 提交如下 commit 就会触发 jenkins 打包:
98
+
99
+ ```
100
+ git commit -m "[jb] XXXXXX"
101
+ ```
102
+
103
+ ## Contributing
104
+
105
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fire-jenkins-builder. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
106
+
107
+ ## License
108
+
109
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
110
+
111
+ ## Code of Conduct
112
+
113
+ Everyone interacting in the Fire::Jenkins::Builder project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/fire-jenkins-builder/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/jb ADDED
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "optparse"
5
+ require "fire/jenkins/builder"
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ options = {}
14
+ OptionParser.new do |opts|
15
+ opts.banner = "jb is a tool to build jenkins job on gitlab CI."
16
+
17
+ opts.on('-p PATH', '--path PATH', 'config file path.') do |value|
18
+ options[:path] = value
19
+ end
20
+
21
+ opts.on('-b BRANCH', '--branch BRANCH', 'jenkins branch sprcifier.') do |value|
22
+ options[:branch] = value
23
+ end
24
+
25
+ opts.on('-l LOG_LEVEL', '--log_level LOGGER_LEVEL', 'log level of jb.') do |value|
26
+ options[:log_level] = value
27
+ end
28
+ end.parse!
29
+
30
+ config_path = options[:path]
31
+
32
+ raise "必须指定配置文件路径,模版见 git homepage" if config_path.nil? || !File.exist?(config_path)
33
+
34
+ config = YAML.load_file(config_path)
35
+
36
+ config.merge!({
37
+ 'branch' => options[:branch],
38
+ 'log_level' => options[:log_level]
39
+ })
40
+
41
+ builder = Fire::Jenkins::Builder::Base.new(config)
42
+ builder.build
43
+ # require "irb"
44
+ # IRB.start(__FILE__)
@@ -0,0 +1,28 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "fire/jenkins/builder/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fire-jenkins-builder"
8
+ spec.version = Fire::Jenkins::Builder::VERSION
9
+ spec.authors = ["tripleCC"]
10
+ spec.email = ["triplec.linux@gmail.com"]
11
+
12
+ spec.summary = %q{2dfire jenkins builder tool.}
13
+ spec.description = %q{2dfire jenkins builder tool.}
14
+ spec.homepage = "https://github.com/tripleCC/fire-jenkins-builder"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "nokogiri"
25
+ spec.add_dependency "jenkins_api_client"
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ end
@@ -0,0 +1,7 @@
1
+ module Fire
2
+ module Jenkins
3
+ module Builder
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,89 @@
1
+ require 'jenkins_api_client'
2
+ require 'nokogiri'
3
+ require 'fileutils'
4
+ require 'pathname'
5
+ require "fire/jenkins/builder/version"
6
+
7
+ module Fire
8
+ module Jenkins
9
+ module Builder
10
+ class Base
11
+ attr_reader :config
12
+ attr_reader :client
13
+
14
+ REQUIRED_KEYS = %w[server_url server_port username password template_job_name job_view branch].freeze
15
+
16
+ def initialize(config)
17
+ @client = JenkinsApi::Client.new(
18
+ server_url: config['server_url'],
19
+ server_port: config['server_port'],
20
+ username: config['username'],
21
+ password: config['password'],
22
+ log_level: config['log_level'] || 'info'
23
+ )
24
+ @config = config || {}
25
+ validate!
26
+ end
27
+
28
+ def create
29
+ client.job.create_or_update(job_name, target_xml)
30
+ client.view.add_job(config['job_view'], job_name)
31
+ build
32
+ end
33
+
34
+ def build
35
+ jobs = client.job.list(job_name)
36
+ if jobs.empty?
37
+ create
38
+ else
39
+ job_name = client.job.chain(jobs, 'success', ['all'])[0]
40
+ client.job.build(job_name, config['parameters'])
41
+ end
42
+ end
43
+
44
+ def job_name
45
+ @job_name = begin
46
+ (config['job_name'] || (config['job_name_prefix'] + config.branch)).sub('/', '_')
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def target_xml
53
+ job_config = client.job.get_config(config['template_job_name'])
54
+ parameters = config['parameters'] || []
55
+
56
+ doc = Nokogiri::XML(job_config)
57
+ properties = doc.search('//parameterDefinitions')
58
+ properties.children.each do |child|
59
+ next if child.children.empty?
60
+
61
+ children = child.children.reject { |c| c.name.nil? }
62
+ matched = false
63
+ defaultValue = nil
64
+ children.each do |c|
65
+ if c.name == 'name' && parameters.keys.include?(c.content)
66
+ matched = true
67
+ defaultValue = parameters[c.content]
68
+ end
69
+
70
+ c.content = defaultValue if matched && c.name == 'defaultValue'
71
+ end
72
+ end
73
+
74
+ branch_spec_node = doc.search("//hudson.plugins.git.BranchSpec")
75
+ branch_node = branch_spec_node.children.find { |c| c.name == 'name' }
76
+ branch_node.content = config.branch
77
+ doc.to_xml
78
+ end
79
+
80
+ def validate!
81
+ raise "job_name 和 job_name_prefix 必须有一个不为空" if config['job_name'].nil? && config['job_name_prefix'].nil?
82
+
83
+ empty_value_keys = REQUIRED_KEYS.select { |key| config[key].nil? }
84
+ raise "#{empty_value_keys.join(', ')} 不能为空" if empty_value_keys.any?
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fire-jenkins-builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - tripleCC
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jenkins_api_client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: 2dfire jenkins builder tool.
70
+ email:
71
+ - triplec.linux@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".fire-jenkins.yml"
77
+ - ".gitignore"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/jb
85
+ - fire-jenkins-builder.gemspec
86
+ - lib/fire/jenkins/builder.rb
87
+ - lib/fire/jenkins/builder/version.rb
88
+ homepage: https://github.com/tripleCC/fire-jenkins-builder
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.6.14
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: 2dfire jenkins builder tool.
112
+ test_files: []