1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 107 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 182 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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
##################
Pipelines and Jobs
##################
Project pipelines
=================
A pipeline is a group of jobs executed by GitLab CI.
Reference
---------
* v4 API:
+ :class:`gitlab.v4.objects.ProjectPipeline`
+ :class:`gitlab.v4.objects.ProjectPipelineManager`
+ :attr:`gitlab.v4.objects.Project.pipelines`
* GitLab API: https://docs.gitlab.com/ce/api/pipelines.html
Examples
--------
List pipelines for a project::
pipelines = project.pipelines.list()
Get a pipeline for a project::
pipeline = project.pipelines.get(pipeline_id)
Get variables of a pipeline::
variables = pipeline.variables.list()
Create a pipeline for a particular reference with custom variables::
pipeline = project.pipelines.create({'ref': 'main', 'variables': [{'key': 'MY_VARIABLE', 'value': 'hello'}]})
Retry the failed builds for a pipeline::
pipeline.retry()
Cancel builds in a pipeline::
pipeline.cancel()
Delete a pipeline::
pipeline.delete()
Get latest pipeline::
projet.pipelines.latest(ref="main")
Triggers
========
Triggers provide a way to interact with the GitLab CI. Using a trigger a user
or an application can run a new build/job for a specific commit.
Reference
---------
* v4 API:
+ :class:`gitlab.v4.objects.ProjectTrigger`
+ :class:`gitlab.v4.objects.ProjectTriggerManager`
+ :attr:`gitlab.v4.objects.Project.triggers`
* GitLab API: https://docs.gitlab.com/ce/api/pipeline_triggers.html
Examples
--------
List triggers::
triggers = project.triggers.list()
Get a trigger::
trigger = project.triggers.get(trigger_token)
Create a trigger::
trigger = project.triggers.create({'description': 'mytrigger'})
Remove a trigger::
project.triggers.delete(trigger_token)
# or
trigger.delete()
Full example with wait for finish::
def get_or_create_trigger(project):
trigger_decription = 'my_trigger_id'
for t in project.triggers.list():
if t.description == trigger_decription:
return t
return project.triggers.create({'description': trigger_decription})
trigger = get_or_create_trigger(project)
pipeline = project.trigger_pipeline('main', trigger.token, variables={"DEPLOY_ZONE": "us-west1"})
while pipeline.finished_at is None:
pipeline.refresh()
time.sleep(1)
You can trigger a pipeline using token authentication instead of user
authentication. To do so create an anonymous Gitlab instance and use lazy
objects to get the associated project::
gl = gitlab.Gitlab(URL) # no authentication
project = gl.projects.get(project_id, lazy=True) # no API call
project.trigger_pipeline('main', trigger_token)
Reference: https://docs.gitlab.com/ee/ci/triggers/#trigger-token
Pipeline schedules
==================
You can schedule pipeline runs using a cron-like syntax. Variables can be
associated with the scheduled pipelines.
Reference
---------
* v4 API
+ :class:`gitlab.v4.objects.ProjectPipelineSchedule`
+ :class:`gitlab.v4.objects.ProjectPipelineScheduleManager`
+ :attr:`gitlab.v4.objects.Project.pipelineschedules`
+ :class:`gitlab.v4.objects.ProjectPipelineScheduleVariable`
+ :class:`gitlab.v4.objects.ProjectPipelineScheduleVariableManager`
+ :attr:`gitlab.v4.objects.ProjectPipelineSchedule.variables`
+ :class:`gitlab.v4.objects.ProjectPipelineSchedulePipeline`
+ :class:`gitlab.v4.objects.ProjectPipelineSchedulePipelineManager`
+ :attr:`gitlab.v4.objects.ProjectPipelineSchedule.pipelines`
* GitLab API: https://docs.gitlab.com/ce/api/pipeline_schedules.html
Examples
--------
List pipeline schedules::
scheds = project.pipelineschedules.list()
Get a single schedule::
sched = project.pipelineschedules.get(schedule_id)
Create a new schedule::
sched = project.pipelineschedules.create({
'ref': 'main',
'description': 'Daily test',
'cron': '0 1 * * *'})
Update a schedule::
sched.cron = '1 2 * * *'
sched.save()
Take ownership of a schedule:
sched.take_ownership()
Trigger a pipeline schedule immediately::
sched = projects.pipelineschedules.get(schedule_id)
sched.play()
Delete a schedule::
sched.delete()
List schedule variables::
# note: you need to use get() to retrieve the schedule variables. The
# attribute is not present in the response of a list() call
sched = projects.pipelineschedules.get(schedule_id)
vars = sched.attributes['variables']
Create a schedule variable::
var = sched.variables.create({'key': 'foo', 'value': 'bar'})
Edit a schedule variable::
var.value = 'new_value'
var.save()
Delete a schedule variable::
var.delete()
List all pipelines triggered by a pipeline schedule::
pipelines = sched.pipelines.list()
Jobs
====
Jobs are associated to projects, pipelines and commits. They provide
information on the jobs that have been run, and methods to manipulate
them.
Reference
---------
* v4 API
+ :class:`gitlab.v4.objects.ProjectJob`
+ :class:`gitlab.v4.objects.ProjectJobManager`
+ :attr:`gitlab.v4.objects.Project.jobs`
* GitLab API: https://docs.gitlab.com/ce/api/jobs.html
Examples
--------
Jobs are usually automatically triggered, but you can explicitly trigger a new
job::
project.trigger_build('main', trigger_token,
{'extra_var1': 'foo', 'extra_var2': 'bar'})
List jobs for the project::
jobs = project.jobs.list()
Get a single job::
project.jobs.get(job_id)
List the jobs of a pipeline::
project = gl.projects.get(project_id)
pipeline = project.pipelines.get(pipeline_id)
jobs = pipeline.jobs.list()
.. note::
Job methods (play, cancel, and so on) are not available on
``ProjectPipelineJob`` objects. To use these methods create a ``ProjectJob``
object::
pipeline_job = pipeline.jobs.list()[0]
job = project.jobs.get(pipeline_job.id, lazy=True)
job.retry()
Get the artifacts of a job::
build_or_job.artifacts()
Get the artifacts of a job by its name from the latest successful pipeline of
a branch or tag::
project.artifacts.download(ref_name='main', job='build')
.. warning::
Artifacts are entirely stored in memory in this example.
.. _streaming_example:
You can download artifacts as a stream. Provide a callable to handle the
stream::
with open("archive.zip", "wb") as f:
build_or_job.artifacts(streamed=True, action=f.write)
You can also directly stream the output into a file, and unzip it afterwards::
zipfn = "___artifacts.zip"
with open(zipfn, "wb") as f:
build_or_job.artifacts(streamed=True, action=f.write)
subprocess.run(["unzip", "-bo", zipfn])
os.unlink(zipfn)
Or, you can also use the underlying response iterator directly::
artifact_bytes_iterator = build_or_job.artifacts(iterator=True)
This can be used with frameworks that expect an iterator (such as FastAPI/Starlette's
``StreamingResponse``) to forward a download from GitLab without having to download
the entire content server-side first::
@app.get("/download_artifact")
def download_artifact():
artifact_bytes_iterator = build_or_job.artifacts(iterator=True)
return StreamingResponse(artifact_bytes_iterator, media_type="application/zip")
Delete all artifacts of a project that can be deleted::
project.artifacts.delete()
Get a single artifact file::
build_or_job.artifact('path/to/file')
Get a single artifact file by branch and job::
project.artifacts.raw('branch', 'path/to/file', 'job')
Mark a job artifact as kept when expiration is set::
build_or_job.keep_artifacts()
Delete the artifacts of a job::
build_or_job.delete_artifacts()
Get a job log file / trace::
build_or_job.trace()
.. warning::
Traces are entirely stored in memory unless you use the streaming feature.
See :ref:`the artifacts example <streaming_example>`.
Cancel/retry a job::
build_or_job.cancel()
build_or_job.retry()
Play (trigger) a job::
build_or_job.play()
Erase a job (artifacts and trace)::
build_or_job.erase()
Pipeline bridges
=====================
Get a list of bridge jobs (including child pipelines) for a pipeline.
Reference
---------
* v4 API
+ :class:`gitlab.v4.objects.ProjectPipelineBridge`
+ :class:`gitlab.v4.objects.ProjectPipelineBridgeManager`
+ :attr:`gitlab.v4.objects.ProjectPipeline.bridges`
* GitLab API: https://docs.gitlab.com/ee/api/jobs.html#list-pipeline-bridges
Examples
--------
List bridges for the pipeline::
bridges = pipeline.bridges.list()
Pipeline test report
====================
Get a pipeline's complete test report.
Reference
---------
* v4 API
+ :class:`gitlab.v4.objects.ProjectPipelineTestReport`
+ :class:`gitlab.v4.objects.ProjectPipelineTestReportManager`
+ :attr:`gitlab.v4.objects.ProjectPipeline.test_report`
* GitLab API: https://docs.gitlab.com/ee/api/pipelines.html#get-a-pipelines-test-report
Examples
--------
Get the test report for a pipeline::
test_report = pipeline.test_report.get()
Pipeline test report summary
============================
Get a pipeline’s test report summary.
Reference
---------
* v4 API
+ :class:`gitlab.v4.objects.ProjectPipelineTestReportSummary`
+ :class:`gitlab.v4.objects.ProjectPipelineTestReportSummaryManager`
+ :attr:`gitlab.v4.objects.ProjectPipeline.test_report_summary`
* GitLab API: https://docs.gitlab.com/ee/api/pipelines.html#get-a-pipelines-test-report-summary
Examples
--------
Get the test report summary for a pipeline::
test_report_summary = pipeline.test_report_summary.get()
|