Debian Codegraph @

jenkins_jobs/modules/project_pipeline.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2016 Mirantis, Inc.
3 #
4 # Based on jenkins_jobs/modules/project_workflow.py by
5 # Copyright (C) 2015 David Caro <[email protected]>
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
17 # under the License.
18
19
20 """
21 The Pipeline Project module handles creating Jenkins Pipeline projects
22 (formerly known as the Workflow projects).
23 You may specify ``pipeline`` in the ``project-type`` attribute of
24 the :ref:`Job` definition.
25
26 Requires the Jenkins :jenkins-plugins:`Pipeline Plugin <workflow-aggregator>`.
27
28 In order to write an inline script within a job-template you have to escape the
29 curly braces by doubling them in the DSL: { -> {{ , otherwise it will be
30 interpreted by the python str.format() command.
31
32 :Job Parameters:
33 * **sandbox** (`bool`): If the script should run in a sandbox (default
34 false)
35 * **dsl** (`str`): The DSL content or,
36 * **pipeline-scm** (`str`): in case "Pipeline as code" feature is used.
37 Then you should specify:
38
39 * **scm**: single ``scm`` component (or a reference) describing the
40 source code repository
41 * **script-path**: path to the Groovy file containing the job's steps
42 (optional, default: ``Jenkinsfile``)
43 * **lightweight-checkout** (`bool`): If selected, try to obtain the
44 Pipeline script contents directly from the SCM without performing a
45 full checkout. (optional, default: ``false``)
46
47 Note that ``dsl`` and ``pipeline-scm`` parameters are mutually exclusive.
48
49 Inline DSL job example:
50
51 .. literalinclude::
52 /../../tests/yamlparser/job_fixtures/project_pipeline_template001.yaml
53
54 Inline DSL job template example:
55
56 .. literalinclude::
57 /../../tests/yamlparser/job_fixtures/project_pipeline_template002.yaml
58
59 "Pipeline as code" example:
60
61 .. literalinclude::
62 /../../tests/yamlparser/job_fixtures/project_pipeline_template004.yaml
63
64 "Pipeline as code" example using templates:
65
66 .. literalinclude::
67 /../../tests/yamlparser/job_fixtures/project_pipeline_template005.yaml
68
69 "Pipeline as nested stage" example :
70
71 .. literalinclude::
72 /../../tests/yamlparser/job_fixtures/project_pipeline_template006.yaml
73
74 .. _Pipeline as code: https://jenkins.io/solutions/pipeline/
75
76 """
77 import xml.etree.ElementTree as XML
78
79 from jenkins_jobs.errors import JenkinsJobsException
80 import jenkins_jobs.modules.base
81
82
83 class Pipeline(jenkins_jobs.modules.base.Base):
84 sequence = 0
85 error_msg = "You cannot declare both 'dsl' and 'pipeline-scm' on a " "pipeline job"
86
87 def root_xml(self, data):
88 xml_parent = XML.Element("flow-definition", {"plugin": "workflow-job"})
89 if "dsl" in data and "pipeline-scm" in data:
90 raise JenkinsJobsException(self.error_msg)
91 if "dsl" in data:
92 xml_definition = XML.SubElement(
93 xml_parent,
94 "definition",
95 {
96 "class": "org.jenkinsci.plugins." "workflow.cps.CpsFlowDefinition",
97 "plugin": "workflow-cps",
98 },
99 )
100 XML.SubElement(xml_definition, "script").text = data["dsl"]
101 elif "pipeline-scm" in data:
102 xml_definition = XML.SubElement(
103 xml_parent,
104 "definition",
105 {
106 "class": "org.jenkinsci.plugins.workflow.cps."
107 "CpsScmFlowDefinition",
108 "plugin": "workflow-cps",
109 },
110 )
111 else:
112 raise JenkinsJobsException(
113 "Either 'dsl' or 'pipeline-scm' " "is required for pipeline job"
114 )
115
116 needs_workspace = data.get("sandbox", False)
117 XML.SubElement(xml_definition, "sandbox").text = str(needs_workspace).lower()
118
119 return xml_parent