Module: JenkinsTools

Defined in:
lib/citasks/ci_lib.rb

Constant Summary collapse

WORKFLOW_PLUGIN =
ENV["WORKFLOW_PLUGIN"] || "workflow-job"
GITLAB_PLUGIN =
ENV["GITLAB_PLUGIN"] || "gitlab-plugin"
WORkFLOW_CPS_PLUGIN =
ENV["WORkFLOW_CPS_PLUGIN"] || "workflow-cps"
GIT_PLUGIN =
ENV["GIT_PLUGIN"] || "git"

Class Method Summary collapse

Class Method Details

.delete!(job_name, base_url, user, token) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/citasks/ci_lib.rb', line 227

def self.delete! job_name, base_url, user, token
  # system %Q(curl -XPOST "#{base_url}/job/#{job_name}/doDelete" --user "#{user}:#{token}")
  req = RestClient::Request.new(
    method: :post,
    user: user,
    password: token,
    url: "#{base_url}/job/#{job_name}/doDelete",
    timeout: 30
  )

  begin
    res = req.execute
  rescue RestClient::ExceptionWithResponse => err
    case err.http_code
    when 301, 302, 307
      err.response.follow_redirection
    else
      raise
    end
  end
end

.download_job(job_name, xml_file, base_url, user, token) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/citasks/ci_lib.rb', line 210

def self.download_job job_name, xml_file, base_url, user, token
  # system %Q(curl -s "#{base_url}/job/#{job_name}/config.xml" -o #{xml_file} --user "#{user}:#{token}")
  req = RestClient::Request.new(
    method: :get,
    user: user,
    password: token,
    url: "#{base_url}/job/#{job_name}/config.xml",
    timeout: 30
  )

  res = req.execute

  File.open xml_file, "w" do |fh|
    fh.puts res.body
  end
end

.gen_jenkins_fileObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/citasks/ci_lib.rb', line 108

def self.gen_jenkins_file
  _write "Jenkinsfile", "    //A Jenkinsfile for start\n    podTemplate(label: 'my-pod',\n      containers:[\n        containerTemplate(name: 'compiler', image:'\#{ENV[\"COMPILER_DOCKER_IMAGE\"]}',ttyEnabled: true, command: 'cat', envVars:[\n            containerEnvVar(key: 'BUILD_NUMBER', value: env.BUILD_NUMBER),\n            containerEnvVar(key: 'BUILD_ID', value: env.BUILD_ID),\n            containerEnvVar(key: 'BUILD_URL', value: env.BUILD_URL),\n            containerEnvVar(key: 'BUILD_TAG', value: env.BUILD_TAG),\n            containerEnvVar(key: 'JOB_NAME', value: env.JOB_NAME)\n          ],\n        ),\n        containerTemplate(name: 'citools', image:'zhiminwen/citools',ttyEnabled: true, command: 'cat', envVars:[\n            // these env is only available in container template? podEnvVar deosn't work?!\n            containerEnvVar(key: 'BUILD_NUMBER', value: env.BUILD_NUMBER),\n            containerEnvVar(key: 'BUILD_ID', value: env.BUILD_ID),\n            containerEnvVar(key: 'BUILD_URL', value: env.BUILD_URL),\n            containerEnvVar(key: 'BUILD_TAG', value: env.BUILD_TAG),\n            containerEnvVar(key: 'JOB_NAME', value: env.JOB_NAME)\n          ],\n        )\n      ],\n      volumes: [\n        //for docker to work\n        hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock')\n      ]\n    ){\n      node('my-pod') {\n        stage('check out') {\n          checkout scm\n        }\n\n        stage('compile'){\n          container('compiler'){\n            sh \"echo compile\"\n          }\n        }\n\n        stage('Docker Build'){\n          container('citools'){\n            // sleep 3600\n            sh \"echo build docker image\"\n            // sh \"rake -f build.rb docker:01_build_image docker:02_push_to_ICp_registry\"\n          }\n        }\n\n        stage('Deploy to ICP'){\n          container('citools'){\n            // sleep 3600\n            echo \"deploy to icp...\"\n            // sh \"rake -f build.rb k8s:01_deploy_to_k8s\"\n          }\n        }\n\n        //stage('Deployment'){\n        //  parallel 'deploy to icp': {\n        //    container('citools'){\n        //      echo \"deploy to icp...\"\n        //      // sh \"rake -f build.rb k8s:01_deploy_to_k8s\"\n\n        //    }\n        //  },\n\n        //  'deploy to others': {\n        //    container('citools'){\n        //      echo \"deploy to others...\"\n        //    }\n        //  }\n        //}\n      }\n    }\n  EOF\nend\n"

.gen_job_xml(job_name, xml_file_name, git_repo_url, repo_credential_id_in_jenkins, secret_token = nil) ⇒ Object

git_repo_url = virtuous-porcupine-gitlab-ce/wenzm/icp-static-web.git, gitlab-wenzm-password



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/citasks/ci_lib.rb', line 35

def self.gen_job_xml job_name, xml_file_name, git_repo_url, repo_credential_id_in_jenkins, secret_token=nil
  enable_secret_token = true

  if enable_secret_token
    secret_token = token_shared_persistently if secret_token.nil?
  end

  token_to_trigger_build_remotely = SecureRandom.uuid
  erb = ERB.new "    <?xml version='1.0' encoding='UTF-8'?>\n    <flow-definition plugin=\"\#{WORKFLOW_PLUGIN}\">\n      <actions/>\n      <description>Workflow Created with template</description>\n      <keepDependencies>false</keepDependencies>\n      <properties>\n        <com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty plugin=\"\#{GITLAB_PLUGIN}\">\n          <gitLabConnection>gitlab</gitLabConnection>\n        </com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty>\n        <org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>\n          <triggers>\n            <com.dabsquared.gitlabjenkins.GitLabPushTrigger plugin=\"\#{GITLAB_PLUGIN}\">\n              <spec></spec>\n              <triggerOnPush>true</triggerOnPush>\n              <triggerOnMergeRequest>false</triggerOnMergeRequest>\n              <triggerOnAcceptedMergeRequest>false</triggerOnAcceptedMergeRequest>\n              <triggerOnClosedMergeRequest>false</triggerOnClosedMergeRequest>\n              <triggerOpenMergeRequestOnPush>never</triggerOpenMergeRequestOnPush>\n              <triggerOnNoteRequest>true</triggerOnNoteRequest>\n              <noteRegex>Jenkins please build one more</noteRegex>\n              <ciSkip>true</ciSkip>\n              <skipWorkInProgressMergeRequest>true</skipWorkInProgressMergeRequest>\n              <setBuildDescription>true</setBuildDescription>\n              <branchFilterType>All</branchFilterType>\n              <includeBranchesSpec></includeBranchesSpec>\n              <excludeBranchesSpec></excludeBranchesSpec>\n              <targetBranchRegex></targetBranchRegex>\n              <% if enable_secret_token %>\n              <secretToken><%= secret_token %></secretToken>\n              <% end %>\n            </com.dabsquared.gitlabjenkins.GitLabPushTrigger>\n          </triggers>\n        </org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>\n      </properties>\n      <definition class=\"org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition\" plugin=\"\#{WORkFLOW_CPS_PLUGIN}\">\n        <scm class=\"hudson.plugins.git.GitSCM\" plugin=\"\#{GIT_PLUGIN}\">\n          <configVersion>2</configVersion>\n          <userRemoteConfigs>\n            <hudson.plugins.git.UserRemoteConfig>\n              <url>\#{git_repo_url}</url>\n              <credentialsId>\#{repo_credential_id_in_jenkins}</credentialsId>\n            </hudson.plugins.git.UserRemoteConfig>\n          </userRemoteConfigs>\n          <branches>\n            <hudson.plugins.git.BranchSpec>\n              <name>*/master</name>\n            </hudson.plugins.git.BranchSpec>\n          </branches>\n          <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>\n          <submoduleCfg class=\"list\"/>\n          <extensions/>\n        </scm>\n        <scriptPath>Jenkinsfile</scriptPath>\n        <lightweight>true</lightweight>\n      </definition>\n      <triggers/>\n      <authToken>\#{token_to_trigger_build_remotely}</authToken>\n      <disabled>false</disabled>\n    </flow-definition>\n  EOF\n\n  _write xml_file_name, erb.result(binding)\nend\n"

.post_new_job(job_name, xml_file, base_url, user, token) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/citasks/ci_lib.rb', line 183

def self.post_new_job job_name, xml_file, base_url, user, token
  # system %Q(curl -s -XPOST "#{base_url}/createItem?name=#{job_name}" --data-binary "@#{xml_file}" -H "Content-Type:text/xml" --user "#{user}:#{token}")
  req = RestClient::Request.new(
    method: :post,
    user: user,
    password: token,
    url: "#{base_url}/createItem?name=#{job_name}",
    timeout: 30,
    headers: {
      "Content-Type" => "text/xml",
    },
    payload: File.read(xml_file)
  )

  begin
    res = req.execute
  rescue RestClient::ExceptionWithResponse => err
    case err.http_code
    when 301, 302, 307
      err.response.follow_redirection
    else
      raise
    end
  end

end

.trigger_build(job_name, build_token, base_url) ⇒ Object



249
250
251
252
253
254
255
256
257
# File 'lib/citasks/ci_lib.rb', line 249

def self.trigger_build job_name,build_token, base_url
  # system %Q(curl "#{base_url}/job/#{job_name}/build?token=#{build_token}")
  req = RestClient::Request.new(
    method: :get,
    url: "#{base_url}/job/#{job_name}/build?token=#{build_token}",
    timeout: 30
  )
  res = req.execute
end