citasks 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 78164b53af04e75190a4116f08a5b148880fd564
4
+ data.tar.gz: 70ac11f150c4a8a73c6924d7e86e3ec935c07432
5
+ SHA512:
6
+ metadata.gz: 5937ccb9144cb89fb5899db4482ae15aefc3940e9b1a7ccaa5a3fd4f71bfe91f6d519b56497f074fca1e141bc0c640e6f193fbf5866a2d43b82b0ef53526d511
7
+ data.tar.gz: e070c096c11bfc0457caa172d543f67dd033a66eb33329f06029b0559b2a72a218fdd5526ec5d496c2d4a77e87742a18202e94f1cc1046801b850f145e9dc62e
@@ -0,0 +1,181 @@
1
+ require 'gitlab'
2
+
3
+ module JenkinsTools
4
+ WORKFLOW_PLUGIN = ENV["WORKFLOW_PLUGIN"] || "workflow-job@2.14.1"
5
+ GITLAB_PLUGIN = ENV["GITLAB_PLUGIN"] || "gitlab-plugin@1.4.7"
6
+ WORkFLOW_CPS_PLUGIN = ENV["WORkFLOW_CPS_PLUGIN"] || "workflow-cps@2.39"
7
+ GIT_PLUGIN = ENV["GIT_PLUGIN"] || "git@3.4.0"
8
+
9
+ # git_repo_url = http://virtuous-porcupine-gitlab-ce/wenzm/icp-static-web.git, gitlab-wenzm-password
10
+ def self.gen_job_xml job_name, xml_file_name, git_repo_url, repo_credential_id_in_jenkins,token_to_trigger_build_remotely = SecureRandom.uuid
11
+ secret_token = "{AQAAABAAAAAQ76W/e/wjLSZ6yxDaU6oaB3rUABFZ/jw6NVzpJkLGL/8=}" #empty??? TODO
12
+ xmls= <<~EOF
13
+ <?xml version='1.0' encoding='UTF-8'?>
14
+ <flow-definition plugin="#{WORKFLOW_PLUGIN}">
15
+ <actions/>
16
+ <description>Workflow Created with template</description>
17
+ <keepDependencies>false</keepDependencies>
18
+ <properties>
19
+ <com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty plugin="#{GITLAB_PLUGIN}">
20
+ <gitLabConnection>gitlab</gitLabConnection>
21
+ </com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty>
22
+ <org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
23
+ <triggers>
24
+ <com.dabsquared.gitlabjenkins.GitLabPushTrigger plugin="#{GITLAB_PLUGIN}">
25
+ <spec></spec>
26
+ <triggerOnPush>true</triggerOnPush>
27
+ <triggerOnMergeRequest>false</triggerOnMergeRequest>
28
+ <triggerOnAcceptedMergeRequest>false</triggerOnAcceptedMergeRequest>
29
+ <triggerOnClosedMergeRequest>false</triggerOnClosedMergeRequest>
30
+ <triggerOpenMergeRequestOnPush>never</triggerOpenMergeRequestOnPush>
31
+ <triggerOnNoteRequest>true</triggerOnNoteRequest>
32
+ <noteRegex>Jenkins please build one more</noteRegex>
33
+ <ciSkip>true</ciSkip>
34
+ <skipWorkInProgressMergeRequest>true</skipWorkInProgressMergeRequest>
35
+ <setBuildDescription>true</setBuildDescription>
36
+ <branchFilterType>All</branchFilterType>
37
+ <includeBranchesSpec></includeBranchesSpec>
38
+ <excludeBranchesSpec></excludeBranchesSpec>
39
+ <targetBranchRegex></targetBranchRegex>
40
+ <!-- <secretToken>#{secret_token}</secretToken> -->
41
+ </com.dabsquared.gitlabjenkins.GitLabPushTrigger>
42
+ </triggers>
43
+ </org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
44
+ </properties>
45
+ <definition class="org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition" plugin="#{WORkFLOW_CPS_PLUGIN}">
46
+ <scm class="hudson.plugins.git.GitSCM" plugin="#[GIT_PLUGIN}">
47
+ <configVersion>2</configVersion>
48
+ <userRemoteConfigs>
49
+ <hudson.plugins.git.UserRemoteConfig>
50
+ <url>#{git_repo_url}</url>
51
+ <credentialsId>#{repo_credential_id_in_jenkins}</credentialsId>
52
+ </hudson.plugins.git.UserRemoteConfig>
53
+ </userRemoteConfigs>
54
+ <branches>
55
+ <hudson.plugins.git.BranchSpec>
56
+ <name>*/master</name>
57
+ </hudson.plugins.git.BranchSpec>
58
+ </branches>
59
+ <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
60
+ <submoduleCfg class="list"/>
61
+ <extensions/>
62
+ </scm>
63
+ <scriptPath>Jenkinsfile</scriptPath>
64
+ <lightweight>true</lightweight>
65
+ </definition>
66
+ <triggers/>
67
+ <authToken>#{token_to_trigger_build_remotely}</authToken>
68
+ <disabled>false</disabled>
69
+ </flow-definition>
70
+ EOF
71
+
72
+ File.open xml_file_name, "w" do |fh|
73
+ fh.puts xmls
74
+ end
75
+
76
+ end
77
+
78
+ def self.gen_jenkins_file
79
+ content= <<~EOF
80
+ //A Jenkinsfile for start
81
+ podTemplate(label: 'my-pod',
82
+ containers:[
83
+ // containerTemplate(name: 'compiler', image:'compiler/image',ttyEnabled: true, command: 'cat', envVars:[
84
+ // containerEnvVar(key: 'BUILD_NUMBER', value: env.BUILD_NUMBER),
85
+ // containerEnvVar(key: 'BUILD_ID', value: env.BUILD_ID),
86
+ // containerEnvVar(key: 'BUILD_URL', value: env.BUILD_URL),
87
+ // containerEnvVar(key: 'BUILD_TAG', value: env.BUILD_TAG),
88
+ // containerEnvVar(key: 'JOB_NAME', value: env.JOB_NAME)
89
+ // ],
90
+ // ),
91
+ containerTemplate(name: 'citools', image:'zhiminwen/citools',ttyEnabled: true, command: 'cat', envVars:[
92
+ // these env is only available in container template? podEnvVar deosn't work?!
93
+ containerEnvVar(key: 'BUILD_NUMBER', value: env.BUILD_NUMBER),
94
+ containerEnvVar(key: 'BUILD_ID', value: env.BUILD_ID),
95
+ containerEnvVar(key: 'BUILD_URL', value: env.BUILD_URL),
96
+ containerEnvVar(key: 'BUILD_TAG', value: env.BUILD_TAG),
97
+ containerEnvVar(key: 'JOB_NAME', value: env.JOB_NAME)
98
+ ],
99
+ )
100
+ ],
101
+ volumes: [
102
+ //for docker to work
103
+ hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock')
104
+ ]
105
+ ){
106
+ node('my-pod') {
107
+ stage('clone git repo'){
108
+ checkout scm
109
+
110
+ // container('compiler'){
111
+ // stage('Compile and Build'){
112
+ // sh("echo compile")
113
+ // }
114
+ // }
115
+
116
+ container('citools'){
117
+ stage('Docker Build'){
118
+ // sleep 3600
119
+ sh "echo build docker image"
120
+ }
121
+
122
+ stage('Deploy into k8s'){
123
+ sh "echo rollout to k8s"
124
+ }
125
+ }
126
+ }
127
+ }
128
+ }
129
+ EOF
130
+
131
+ File.open "Jenkinsfile", "w" do |fh|
132
+ fh.puts content
133
+ end
134
+
135
+ end
136
+
137
+ def self.post_new_job job_name, xml_file, base_url, user, token
138
+ system %Q(curl -s -XPOST "#{base_url}/createItem?name=#{job_name}" --data-binary "@#{xml_file}" -H "Content-Type:text/xml" --user "#{user}:#{token}")
139
+ end
140
+
141
+ def self.download_job job_name, xml_file, base_url, user, token
142
+ system %Q(curl -s "#{base_url}/job/#{job_name}/config.xml" -o #{xml_file} --user "#{user}:#{token}")
143
+ end
144
+
145
+ def self.delete! job_name, base_url, user, token
146
+ system %Q(curl -XPOST "#{base_url}/job/#{job_name}/doDelete" --user "#{user}:#{token}")
147
+ end
148
+
149
+ def self.trigger_build job_name,build_token, base_url
150
+ system %Q(curl "#{base_url}/job/#{job_name}/build?token=#{build_token}")
151
+ end
152
+
153
+ end
154
+
155
+ module GitlabTools
156
+ def self._setup_gitlab gitlab_url, token
157
+ Gitlab.endpoint = "#{gitlab_url}/api/v4"
158
+ Gitlab.private_token = token
159
+ end
160
+
161
+ def self.new_repo repo_name, gitlab_url, token
162
+ _setup_gitlab gitlab_url, token
163
+ Gitlab.create_project repo_name
164
+ end
165
+
166
+ def self.setup_hook repo_name, gitlab_url, token, hooked_url, secret_token_for_hooked_url=nil
167
+ _setup_gitlab gitlab_url, token
168
+
169
+ project = Gitlab.projects.find do |p|
170
+ p.name== repo_name
171
+ end
172
+
173
+ Gitlab.add_project_hook project.id, hooked_url, :push_events => 1,:enable_ssl_verification=>0, :token=> secret_token_for_hooked_url
174
+
175
+ end
176
+
177
+ def self.delete! repo_name, gitlab_url, token
178
+ _setup_gitlab gitlab_url, token
179
+ Gitlab.delete_project repo_name
180
+ end
181
+ end
@@ -0,0 +1,5 @@
1
+ @task_index=0
2
+ def next_task_index
3
+ @task_index += 1
4
+ sprintf("%02d", @task_index)
5
+ end
data/lib/citasks.rb ADDED
@@ -0,0 +1,54 @@
1
+ require_relative "citasks/ci_lib"
2
+ require_relative "citasks/task_index"
3
+
4
+ namespace "Jenkins" do
5
+ @task_index = 0
6
+ job_name = ENV["JOB_NAME"]
7
+
8
+ def git_repo_url_in_cluster
9
+ sprintf("%s/%s/%s.git",ENV["GITLAB_IN_CLUSTER_BASE_URL"], ENV["GITLAB_USER"], ENV["REPO_NAME"])
10
+ end
11
+
12
+ desc "create a new project"
13
+ task "#{next_task_index}_create_new_project" do
14
+ xml_file = job_name + ".xml"
15
+ JenkinsTools.gen_job_xml job_name, xml_file, git_repo_url_in_cluster, ENV["JENKINS_GIT_USER_CREDENTIAL_ID"]
16
+
17
+ JenkinsTools.post_new_job job_name, xml_file, ENV["JENKINS_URL"], ENV["JENKINS_USER"], ENV["JENKINS_USER_API_TOKEN"]
18
+
19
+ JenkinsTools.gen_jenkins_file
20
+ end
21
+
22
+ desc "delete"
23
+ task "#{next_task_index}_delete" do
24
+ JenkinsTools.delete! job_name, ENV["JENKINS_URL"], ENV["JENKINS_USER"], ENV["JENKINS_USER_API_TOKEN"]
25
+ end
26
+ end
27
+
28
+ namespace "Gitlab" do
29
+ @task_index = 0
30
+
31
+ repo_name=ENV["REPO_NAME"]
32
+
33
+ def git_repo_url
34
+ sprintf("%s/%s/%s.git",ENV["GITLAB_BASE_URL"], ENV["GITLAB_USER"], ENV["REPO_NAME"])
35
+ end
36
+
37
+ desc "create a new gitlab repo"
38
+ task "#{next_task_index}_create_new_repo" do
39
+ GitlabTools.new_repo repo_name, ENV["GITLAB_BASE_URL"], ENV["GITLAB_API_TOKEN"]
40
+ end
41
+
42
+ desc "setup webhook"
43
+ task "#{next_task_index}_webhook" do
44
+ job_name = ENV["JOB_NAME"]
45
+ hooked_jenkins_url = "#{ENV["JENKINS_IN_CLUSTER_URL"]}/project/#{job_name}"
46
+
47
+ GitlabTools.setup_hook repo_name, ENV["GITLAB_BASE_URL"], ENV["GITLAB_API_TOKEN"],hooked_jenkins_url
48
+ end
49
+
50
+ desc "delete"
51
+ task "#{next_task_index}_delete" do
52
+ GitlabTools.delete repo_name, ENV["GITLAB_BASE_URL"], ENV["GITLAB_API_TOKEN"]
53
+ end
54
+ end
data/readme.md ADDED
@@ -0,0 +1,58 @@
1
+ # CI Tools for Gitlab + Jenkins
2
+
3
+ This gem bootstraps the initial setup for gitlab and jenkins integration.
4
+
5
+ ## Install and require
6
+ Install
7
+ ```
8
+ gem install citasks
9
+ ```
10
+ Use
11
+ ```
12
+ require 'citasks'
13
+ ```
14
+
15
+ ## Usage
16
+ There is a rake task library that you can require to start
17
+
18
+ In your rake task file
19
+ ```
20
+ require 'citasks'
21
+ require 'dotenv'
22
+ Dotenv.load
23
+ ```
24
+
25
+ In the .env file define the following
26
+ ```
27
+ REPO_NAME=
28
+ GITLAB_USER=
29
+ GITLAB_PASS=
30
+
31
+ GITLAB_BASE_URL=
32
+ GITLAB_IN_CLUSTER_BASE_URL=
33
+ GITLAB_API_TOKEN=
34
+
35
+
36
+ JOB_NAME=
37
+ JENKINS_URL=
38
+ JENKINS_IN_CLUSTER_URL=
39
+ JENKINS_GIT_USER_CREDENTIAL_ID=
40
+
41
+ JENKINS_USER=
42
+ JENKINS_USER_API_TOKEN=
43
+ ```
44
+
45
+ Then run
46
+ ```
47
+ rake -T
48
+ ```
49
+
50
+ The tasks is shown as below,
51
+
52
+ ```
53
+ rake Gitlab:01_create_new_repo # create a new gitlab repo
54
+ rake Gitlab:02_webhook # setup webhook
55
+ rake Gitlab:03_delete # delete
56
+ rake Jenkins:01_create_new_project # create a new project
57
+ rake Jenkins:02_delete # delete
58
+ ```
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: citasks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Zhimin Wen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gitlab
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ description: ci/cd tools for gitlab + jenkins. Libray and rake tasks
28
+ email: zhimin.wen@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/citasks.rb
34
+ - lib/citasks/ci_lib.rb
35
+ - lib/citasks/task_index.rb
36
+ - readme.md
37
+ homepage: http://github.com/zhiminwen/citasks
38
+ licenses:
39
+ - MIT
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.6.11
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: ci/cd tools for gitlab + jenkins
61
+ test_files: []