debian-mirror-gitlab/doc/ci/quick_start/README.md

158 lines
5.2 KiB
Markdown
Raw Normal View History

2019-09-04 21:01:54 +05:30
---
2020-06-23 00:09:42 +05:30
stage: Verify
group: Continuous Integration
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#designated-technical-writers
2019-09-04 21:01:54 +05:30
type: reference
---
2021-01-29 00:20:46 +05:30
# Get started with GitLab CI/CD
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
Use this document to get started with
GitLab [continuous integration](https://about.gitlab.com/stages-devops-lifecycle/continuous-integration/).
2016-06-02 11:05:42 +05:30
2021-01-29 00:20:46 +05:30
Before you start, make sure you have:
2016-06-02 11:05:42 +05:30
2021-01-29 00:20:46 +05:30
- A project in GitLab that you would like to use CI/CD for.
- Maintainer or owner access for the project.
2016-06-02 11:05:42 +05:30
2021-01-29 00:20:46 +05:30
If you are migrating from another CI/CD tool, view this documentation:
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
- [Migrate from CircleCI](../migration/circleci.md).
- [Migrate from Jenkins](../migration/jenkins.md).
2016-08-24 12:49:21 +05:30
2021-01-29 00:20:46 +05:30
## CI/CD process overview
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
To use GitLab CI/CD:
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
1. [Ensure you have runners available](#ensure-you-have-runners-available) to run your jobs.
If you don't have a runner, [install GitLab Runner](https://docs.gitlab.com/runner/install/)
and [register a runner](https://docs.gitlab.com/runner/register/) for your instance, project, or group.
1. [Create a `.gitlab-ci.yml` file](#create-a-gitlab-ciyml-file)
at the root of your repository. This file is where you define your CI/CD jobs.
2016-06-02 11:05:42 +05:30
2021-01-29 00:20:46 +05:30
When you commit the file to your repository, the runner runs your jobs.
The job results [are displayed in a pipeline](#view-the-status-of-your-pipeline-and-jobs).
2016-06-02 11:05:42 +05:30
2021-01-29 00:20:46 +05:30
### Ensure you have runners available
2016-06-02 11:05:42 +05:30
2021-01-29 00:20:46 +05:30
In GitLab, runners are agents that run your CI/CD jobs.
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
You might already have runners available for your project, including
[shared runners](../runners/README.md#shared-runners), which are
available to all projects in your GitLab instance.
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
To view available runners:
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
- Go to **Settings > CI/CD** and expand **Runners**.
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
As long as you have at least one runner that's active, with a green circle next to it,
you have a runner available to process your jobs.
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
If no runners are listed on the **Runners** page in the UI, you or an administrator
must [install GitLab Runner](https://docs.gitlab.com/runner/install/) and
[register](https://docs.gitlab.com/runner/register/) at least one runner.
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
If you are testing CI/CD, you can install GitLab Runner and register runners on your local machine.
When your CI/CD jobs run, they will run on your local machine.
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
### Create a `.gitlab-ci.yml` file
2015-12-23 02:04:40 +05:30
2021-01-29 00:20:46 +05:30
The `.gitlab-ci.yml` file is a [YAML](https://en.wikipedia.org/wiki/YAML) file where
you configure specific instructions for GitLab CI/CD.
2020-10-24 23:57:45 +05:30
2021-01-29 00:20:46 +05:30
In this file, you define:
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
- The structure and order of jobs that the runner should execute.
- The decisions the runner should make when specific conditions are encountered.
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
For example, you might want to run a suite of tests when you commit to
any branch except `master`. When you commit to `master`, you want
to run the same suite, but also publish your application.
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
All of this is defined in the `.gitlab-ci.yml` file.
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
To create a `.gitlab-ci.yml` file:
2015-12-23 02:04:40 +05:30
2021-01-29 00:20:46 +05:30
1. Go to **Project overview > Details**.
1. Above the file list, select the branch you want to commit to,
click the plus icon, then select **New file**:
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
![New file](img/new_file_v13_6.png)
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
1. For the **File name** type `.gitlab-ci.yml` and in the larger window,
paste this sample code:
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
```yaml
build-job:
stage: build
script:
- echo "Hello, $GITLAB_USER_LOGIN!"
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
test-job1:
stage: test
script:
- echo "This job tests something"
2021-01-03 14:25:43 +05:30
2021-01-29 00:20:46 +05:30
test-job2:
stage: test
script:
- echo "This job tests something, but takes more time than test-job1."
- echo "After the echo commands complete, it runs the sleep command for 20 seconds"
- echo "which simulates a test that runs 20 seconds longer than test-job1"
- sleep 20
2015-12-23 02:04:40 +05:30
2021-01-29 00:20:46 +05:30
deploy-prod:
stage: deploy
script:
- echo "This job deploys something from the $CI_COMMIT_BRANCH branch."
```
2021-01-03 14:25:43 +05:30
2021-01-29 00:20:46 +05:30
`$GITLAB_USER_LOGIN` and `$CI_COMMIT_BRANCH` are
[predefined variables](../variables/predefined_variables.md)
that populate when the job runs.
2015-12-23 02:04:40 +05:30
2021-01-29 00:20:46 +05:30
1. Click **Commit changes**.
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
The pipeline starts when the commit is committed.
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
#### `.gitlab-ci.yml` tips
2015-12-23 02:04:40 +05:30
2021-01-29 00:20:46 +05:30
- If you want the runner to use a Docker image to run the jobs, edit the `.gitlab-ci.yml` file
to include your image name:
2018-05-09 12:01:36 +05:30
2021-01-29 00:20:46 +05:30
```yaml
default:
image: ruby:2.7.2
```
2015-12-23 02:04:40 +05:30
2021-01-29 00:20:46 +05:30
This command tells the runner to use a Ruby image from Docker Hub.
2015-12-23 02:04:40 +05:30
2021-01-29 00:20:46 +05:30
- To validate your `.gitlab-ci.yml` file, use the
[CI Lint tool](../lint.md), which is available in every project.
- You can also use [CI/CD configuration visualization](../yaml/visualization.md) to
view a graphical representation of your `.gitlab-ci.yml` file.
- For the complete `.gitlab-ci.yml` syntax, see
[the `.gitlab-ci.yml` reference topic](../yaml/README.md).
2015-12-23 02:04:40 +05:30
2021-01-29 00:20:46 +05:30
### View the status of your pipeline and jobs
2015-12-23 02:04:40 +05:30
2021-01-29 00:20:46 +05:30
When you committed your changes, a pipeline started.
2015-12-23 02:04:40 +05:30
2021-01-29 00:20:46 +05:30
To view your pipeline:
2015-12-23 02:04:40 +05:30
2021-01-29 00:20:46 +05:30
- Go **CI/CD > Pipelines**.
2015-12-23 02:04:40 +05:30
2021-01-29 00:20:46 +05:30
A pipeline with three stages should be displayed:
2015-12-23 02:04:40 +05:30
2021-01-29 00:20:46 +05:30
![Three stages](img/three_stages_v13_6.png)
2015-12-23 02:04:40 +05:30
2021-01-29 00:20:46 +05:30
- To view a visual representation of your pipeline, click the pipeline ID.
2015-12-23 02:04:40 +05:30
2021-01-29 00:20:46 +05:30
![Pipeline graph](img/pipeline_graph_v13_6.png)
2015-12-23 02:04:40 +05:30
2021-01-29 00:20:46 +05:30
- To view details of a job, click the job name, for example, `deploy-prod`.
2015-12-23 02:04:40 +05:30
2021-01-29 00:20:46 +05:30
![Job details](img/job_details_v13_6.png)
2015-12-23 02:04:40 +05:30
2021-01-29 00:20:46 +05:30
If the job status is `stuck`, check to ensure a runner is probably configured for the project.