debian-mirror-gitlab/doc/development/cicd/index.md

203 lines
12 KiB
Markdown
Raw Normal View History

2020-10-24 23:57:45 +05:30
---
stage: Verify
2021-09-04 01:27:46 +05:30
group: Pipeline Execution
2022-11-25 23:54:43 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/product/ux/technical-writing/#assignments
2020-10-24 23:57:45 +05:30
type: index, concepts, howto
---
2023-03-17 16:20:25 +05:30
# CI/CD development documentation
2020-03-13 15:44:24 +05:30
2022-05-07 20:08:51 +05:30
Development guides that are specific to CI/CD are listed here:
2020-03-13 15:44:24 +05:30
2022-05-07 20:08:51 +05:30
- If you are creating new CI/CD templates, please read [the development guide for GitLab CI/CD templates](templates.md).
- If you are adding a new keyword or changing the CI schema, check the [CI schema guide](schema.md)
2020-07-28 23:09:34 +05:30
2021-06-08 01:23:25 +05:30
See the [CI/CD YAML reference documentation guide](cicd_reference_documentation_guide.md)
2021-09-30 23:02:18 +05:30
to learn how to update the [reference page](../../ci/yaml/index.md).
2021-06-08 01:23:25 +05:30
2022-07-23 23:45:48 +05:30
## Examples of CI/CD usage
We maintain a [`ci-sample-projects`](https://gitlab.com/gitlab-org/ci-sample-projects) group, with projects that showcase
examples of `.gitlab-ci.yml` for different use cases of GitLab CI/CD. They also cover specific syntax that could
be used for different scenarios.
2020-05-24 23:13:21 +05:30
## CI Architecture overview
2021-11-11 11:23:49 +05:30
The following is a simplified diagram of the CI architecture. Some details are left out to focus on
2020-05-24 23:13:21 +05:30
the main components.
![CI software architecture](img/ci_architecture.png)
<!-- Editable diagram available at https://app.diagrams.net/#G1LFl-KW4fgpBPzz8VIH9rsOlAH4t0xwKj -->
2020-06-23 00:09:42 +05:30
On the left side we have the events that can trigger a pipeline based on various events (triggered by a user or automation):
2020-05-24 23:13:21 +05:30
- A `git push` is the most common event that triggers a pipeline.
- The [Web API](../../api/pipelines.md#create-a-new-pipeline).
2023-01-13 00:05:48 +05:30
- A user selecting the "Run pipeline" button in the UI.
2022-04-04 11:22:00 +05:30
- When a [merge request is created or updated](../../ci/pipelines/merge_request_pipelines.md).
2021-09-30 23:02:18 +05:30
- When an MR is added to a [Merge Train](../../ci/pipelines/merge_trains.md#merge-trains).
2022-05-07 20:08:51 +05:30
- A [scheduled pipeline](../../ci/pipelines/schedules.md).
2022-10-11 01:57:18 +05:30
- When project is [subscribed to an upstream project](../../ci/pipelines/index.md#trigger-a-pipeline-when-an-upstream-project-is-rebuilt).
2020-05-24 23:13:21 +05:30
- When [Auto DevOps](../../topics/autodevops/index.md) is enabled.
- When GitHub integration is used with [external pull requests](../../ci/ci_cd_for_external_repos/index.md#pipelines-for-external-pull-requests).
2021-09-30 23:02:18 +05:30
- When an upstream pipeline contains a [bridge job](../../ci/yaml/index.md#trigger) which triggers a downstream pipeline.
2020-05-24 23:13:21 +05:30
2021-02-22 17:27:13 +05:30
Triggering any of these events invokes the [`CreatePipelineService`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/app/services/ci/create_pipeline_service.rb)
which takes as input event data and the user triggering it, then attempts to create a pipeline.
2020-05-24 23:13:21 +05:30
The `CreatePipelineService` relies heavily on the [`YAML Processor`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/yaml_processor.rb)
component, which is responsible for taking in a YAML blob as input and returns the abstract data structure of a
pipeline (including stages and all jobs). This component also validates the structure of the YAML while
processing it, and returns any syntax or semantic errors. The `YAML Processor` component is where we define
2021-09-30 23:02:18 +05:30
[all the keywords](../../ci/yaml/index.md) available to structure a pipeline.
2020-05-24 23:13:21 +05:30
The `CreatePipelineService` receives the abstract data structure returned by the `YAML Processor`,
2021-10-27 15:23:28 +05:30
which then converts it to persisted models (like pipeline, stages, and jobs). After that, the pipeline is ready
2020-05-24 23:13:21 +05:30
to be processed. Processing a pipeline means running the jobs in order of execution (stage or DAG)
until either one of the following:
- All expected jobs have been executed.
- Failures interrupt the pipeline execution.
The component that processes a pipeline is [`ProcessPipelineService`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/app/services/ci/process_pipeline_service.rb),
which is responsible for moving all the pipeline's jobs to a completed state. When a pipeline is created, all its
jobs are initially in `created` state. This services looks at what jobs in `created` stage are eligible
to be processed based on the pipeline structure. Then it moves them into the `pending` state, which means
2020-11-24 15:15:51 +05:30
they can now [be picked up by a runner](#job-scheduling). After a job has been executed it can complete
2020-05-24 23:13:21 +05:30
successfully or fail. Each status transition for job within a pipeline triggers this service again, which
looks for the next jobs to be transitioned towards completion. While doing that, `ProcessPipelineService`
updates the status of jobs, stages and the overall pipeline.
2021-09-30 23:02:18 +05:30
On the right side of the diagram we have a list of [runners](../../ci/runners/index.md)
2023-04-23 21:23:45 +05:30
connected to the GitLab instance. These can be shared runners, group runners, or project runners.
2020-11-24 15:15:51 +05:30
The communication between runners and the Rails server occurs through a set of API endpoints, grouped as
2020-05-24 23:13:21 +05:30
the `Runner API Gateway`.
2020-11-24 15:15:51 +05:30
We can register, delete, and verify runners, which also causes read/write queries to the database. After a runner is connected,
2021-09-04 01:27:46 +05:30
it keeps asking for the next job to execute. This invokes the [`RegisterJobService`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/app/services/ci/register_job_service.rb)
2021-02-22 17:27:13 +05:30
which picks the next job and assigns it to the runner. At this point the job transitions to a
2020-05-24 23:13:21 +05:30
`running` state, which again triggers `ProcessPipelineService` due to the status change.
For more details read [Job scheduling](#job-scheduling)).
2020-11-24 15:15:51 +05:30
While a job is being executed, the runner sends logs back to the server as well any possible artifacts
2021-11-11 11:23:49 +05:30
that must be stored. Also, a job may depend on artifacts from previous jobs to run. In this
2021-02-22 17:27:13 +05:30
case the runner downloads them using a dedicated API endpoint.
2020-05-24 23:13:21 +05:30
Artifacts are stored in object storage, while metadata is kept in the database. An important example of artifacts
2021-10-27 15:23:28 +05:30
are reports (like JUnit, SAST, and DAST) which are parsed and rendered in the merge request.
2020-05-24 23:13:21 +05:30
2021-10-27 15:23:28 +05:30
Job status transitions are not all automated. A user may run [manual jobs](../../ci/jobs/job_control.md#create-a-job-that-must-be-run-manually), cancel a pipeline, retry
2020-05-24 23:13:21 +05:30
specific failed jobs or the entire pipeline. Anything that
2021-02-22 17:27:13 +05:30
causes a job to change status triggers `ProcessPipelineService`, as it's responsible for
2020-05-24 23:13:21 +05:30
tracking the status of the entire pipeline.
2021-09-30 23:02:18 +05:30
A special type of job is the [bridge job](../../ci/yaml/index.md#trigger) which is executed server-side
2020-05-24 23:13:21 +05:30
when transitioning to the `pending` state. This job is responsible for creating a downstream pipeline, such as
a multi-project or child pipeline. The workflow loop starts again
from the `CreatePipelineService` every time a downstream pipeline is triggered.
2022-11-25 23:54:43 +05:30
<i class="fa fa-youtube-play youtube" aria-hidden="true"></i>
You can watch a walkthrough of the architecture in [CI Backend Architectural Walkthrough](https://www.youtube.com/watch?v=ew4BwohS5OY).
2020-03-13 15:44:24 +05:30
## Job scheduling
When a Pipeline is created all its jobs are created at once for all stages, with an initial state of `created`. This makes it possible to visualize the full content of a pipeline.
2021-02-22 17:27:13 +05:30
A job with the `created` state isn't seen by the runner yet. To make it possible to assign a job to a runner, the job must transition first into the `pending` state, which can happen if:
2020-03-13 15:44:24 +05:30
1. The job is created in the very first stage of the pipeline.
1. The job required a manual start and it has been triggered.
1. All jobs from the previous stage have completed successfully. In this case we transition all jobs from the next stage to `pending`.
1. The job specifies DAG dependencies using `needs:` and all the dependent jobs are completed.
2020-11-24 15:15:51 +05:30
When the runner is connected, it requests the next `pending` job to run by polling the server continuously.
2020-03-13 15:44:24 +05:30
2021-02-22 17:27:13 +05:30
NOTE:
2021-09-04 01:27:46 +05:30
API endpoints used by the runner to interact with GitLab are defined in [`lib/api/ci/runner.rb`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/api/ci/runner.rb)
2020-03-13 15:44:24 +05:30
2020-11-24 15:15:51 +05:30
After the server receives the request it selects a `pending` job based on the [`Ci::RegisterJobService` algorithm](#ciregisterjobservice), then assigns and sends the job to the runner.
2020-03-13 15:44:24 +05:30
2020-11-24 15:15:51 +05:30
Once all jobs are completed for the current stage, the server "unlocks" all the jobs from the next stage by changing their state to `pending`. These can now be picked by the scheduling algorithm when the runner requests new jobs, and continues like this until all stages are completed.
2020-03-13 15:44:24 +05:30
2022-03-02 08:16:31 +05:30
If a job is not picked up by a runner in 24 hours it is automatically removed from
the processing queue after that time. If a pending job is stuck, when there is no
runner available that can process it, it is removed from the queue after 1 hour.
In both cases the job's status is changed to `failed` with an appropriate failure reason.
2020-11-24 15:15:51 +05:30
### Communication between runner and GitLab server
2020-03-13 15:44:24 +05:30
2021-11-11 11:23:49 +05:30
After the runner is [registered](https://docs.gitlab.com/runner/register/) using the registration token, the server knows what type of jobs it can execute. This depends on:
2020-03-13 15:44:24 +05:30
- The type of runner it is registered as:
- a shared runner
- a group runner
2023-04-23 21:23:45 +05:30
- a project runner
2020-03-13 15:44:24 +05:30
- Any associated tags.
2021-11-11 11:23:49 +05:30
The runner initiates the communication by requesting jobs to execute with `POST /api/v4/jobs/request`. Although polling happens every few seconds, we leverage caching through HTTP headers to reduce the server-side work load if the job queue doesn't change.
2020-03-13 15:44:24 +05:30
2021-09-04 01:27:46 +05:30
This API endpoint runs [`Ci::RegisterJobService`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/app/services/ci/register_job_service.rb), which:
2020-03-13 15:44:24 +05:30
1. Picks the next job to run from the pool of `pending` jobs
2020-11-24 15:15:51 +05:30
1. Assigns it to the runner
1. Presents it to the runner via the API response
2020-03-13 15:44:24 +05:30
### `Ci::RegisterJobService`
2020-11-24 15:15:51 +05:30
There are 3 top level queries that this service uses to gather the majority of the jobs and they are selected based on the level where the runner is registered to:
2020-03-13 15:44:24 +05:30
2020-11-24 15:15:51 +05:30
- Select jobs for shared runner (instance level)
- Select jobs for group runner
- Select jobs for project runner
2020-03-13 15:44:24 +05:30
2020-11-24 15:15:51 +05:30
This list of jobs is then filtered further by matching tags between job and runner tags.
2020-03-13 15:44:24 +05:30
2021-02-22 17:27:13 +05:30
NOTE:
If a job contains tags, the runner doesn't pick the job if it does not match **all** the tags.
2020-11-24 15:15:51 +05:30
The runner may have more tags than defined for the job, but not vice-versa.
2020-03-13 15:44:24 +05:30
2020-11-24 15:15:51 +05:30
Finally if the runner can only pick jobs that are tagged, all untagged jobs are filtered out.
2020-03-13 15:44:24 +05:30
2020-11-24 15:15:51 +05:30
At this point we loop through remaining `pending` jobs and we try to assign the first job that the runner "can pick" based on additional policies. For example, runners marked as `protected` can only pick jobs that run against protected branches (such as production deployments).
2020-03-13 15:44:24 +05:30
2020-11-24 15:15:51 +05:30
As we increase the number of runners in the pool we also increase the chances of conflicts which would arise if assigning the same job to different runners. To prevent that we gracefully rescue conflict errors and assign the next job in the list.
2021-03-11 19:13:27 +05:30
## The definition of "Job" in GitLab CI/CD
"Job" in GitLab CI context refers a task to drive Continuous Integration, Delivery and Deployment.
Typically, a pipeline contains multiple stages, and a stage contains multiple jobs.
In Active Record modeling, Job is defined as `CommitStatus` class.
On top of that, we have the following types of jobs:
- `Ci::Build` ... The job to be executed by runners.
- `Ci::Bridge` ... The job to trigger a downstream pipeline.
2021-11-18 22:05:49 +05:30
- `GenericCommitStatus` ... The job to be executed in an external CI/CD system, for example Jenkins.
2021-03-11 19:13:27 +05:30
2021-10-27 15:23:28 +05:30
When you use the "Job" terminology in codebase, readers would
2021-03-11 19:13:27 +05:30
assume that the class/object is any type of above.
If you specifically refer `Ci::Build` class, you should not name the object/class
as "job" as this could cause some confusions. In documentation,
we should use "Job" in general, instead of "Build".
We have a few inconsistencies in our codebase that should be refactored.
For example, `CommitStatus` should be `Ci::Job` and `Ci::JobArtifact` should be `Ci::BuildArtifact`.
See [this issue](https://gitlab.com/gitlab-org/gitlab/-/issues/16111) for the full refactoring plan.
2022-03-02 08:16:31 +05:30
## CI/CD Minutes
2021-03-11 19:13:27 +05:30
2022-03-02 08:16:31 +05:30
This diagram shows how the [CI/CD minutes](../../ci/pipelines/cicd_minutes.md)
2021-03-11 19:13:27 +05:30
feature and its components work.
2022-03-02 08:16:31 +05:30
![CI/CD minutes architecture](img/ci_minutes.png)
2021-03-11 19:13:27 +05:30
<!-- Editable diagram available at https://app.diagrams.net/?libs=general;flowchart#G1XjLPvJXbzMofrC3eKRyDEk95clV6ypOb -->
Watch a walkthrough of this feature in details in the video below.
<div class="video-fallback">
2022-03-02 08:16:31 +05:30
See the video: <a href="https://www.youtube.com/watch?v=NmdWRGT8kZg">CI/CD minutes - architectural overview</a>.
2021-03-11 19:13:27 +05:30
</div>
<figure class="video-container">
2023-03-17 16:20:25 +05:30
<iframe src="https://www.youtube-nocookie.com/embed/NmdWRGT8kZg" frameborder="0" allowfullscreen> </iframe>
2021-03-11 19:13:27 +05:30
</figure>