Get started with GitLab CI/CD
Use this document to get started with GitLab CI/CD.
Before you start, make sure you have:
- A project in GitLab that you would like to use CI/CD for.
- The Maintainer or Owner role for the project.
If you are migrating from another CI/CD tool, view this documentation:
- Watch First time GitLab & CI/CD. This includes a quick introduction to GitLab, the first steps with CI/CD, building a Go project, running tests, using the CI/CD pipeline editor, detecting secrets and security vulnerabilities and offers more exercises for async practice.
- Watch Intro to GitLab CI. This workshop uses the Web IDE to quickly get going with building source code using CI/CD, and run unit tests.
CI/CD process overview
To use GitLab CI/CD:
- Ensure you have runners available to run your jobs. If you don’t have a runner, install GitLab Runner and register a runner for your instance, project, or group.
-
Create a
.gitlab-ci.yml
file at the root of your repository. This file is where you define your CI/CD jobs.
When you commit the file to your repository, the runner runs your jobs. The job results are displayed in a pipeline.
Ensure you have runners available
In GitLab, runners are agents that run your CI/CD jobs.
You might already have runners available for your project, including shared runners, which are available to all projects in your GitLab instance.
To view available runners:
- Go to Settings > CI/CD and expand Runners.
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.
If no runners are listed on the Runners page in the UI, you or an administrator must install GitLab Runner and register at least one runner.
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 run on your local machine.
Create a .gitlab-ci.yml
file
The For example, you might want to run a suite of tests when you commit to
any branch except the default branch. When you commit to the default branch, you want
to run the same suite, but also publish your application.
All of this is defined in the To create a Above the file list, select the branch you want to commit to,
click the plus icon, then select New file:
For the Filename, type The pipeline starts when the commit is committed.
If you want the runner to use a Docker container to run the jobs,
edit the This command tells the runner to use a Ruby image from Docker Hub
and to run the jobs in a container that’s generated from the image.
This process is different than
building an application as a Docker container.
Your application does not need to be built as a Docker container to
run CI/CD jobs in Docker containers.
When you committed your changes, a pipeline started.
To view your pipeline:
Go to CI/CD > Pipelines.
A pipeline with three stages should be displayed:
To view a visual representation of your pipeline, click the pipeline ID.
To view details of a job, click the job name, for example, If the job status is .gitlab-ci.yml
file is a In this file, you define:
.gitlab-ci.yml
file.
.gitlab-ci.yml
file:
.gitlab-ci.yml
and in the larger window,
paste this sample code:
build-job:
stage: build
script:
- echo "Hello, $GITLAB_USER_LOGIN!"
test-job1:
stage: test
script:
- echo "This job tests something"
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
deploy-prod:
stage: deploy
script:
- echo "This job deploys something from the $CI_COMMIT_BRANCH branch."
$GITLAB_USER_LOGIN
and $CI_COMMIT_BRANCH
are
predefined variables
that populate when the job runs.
.gitlab-ci.yml
tips
.gitlab-ci.yml
file, use the pipeline editor
for all future edits to the file. With the pipeline editor, you can:
.gitlab-ci.yml
file.
.gitlab-ci.yml
file
to include an image name:
default:
image: ruby:2.7.4
default
keyword is for
custom defaults, for example with before_script
and after_script
.
stage
describes the sequential execution of jobs.
Jobs in a single stage run in parallel as long as there are available runners.
rules
keyword to specify when to run or skip jobs.
The only
and except
legacy keywords are still supported, but can’t be used
with rules
in the same job.
cache
and artifacts
. These keywords are ways to store
dependencies and job output, even when using ephemeral runners for each job.
.gitlab-ci.yml
syntax, see the full .gitlab-ci.yml
reference topic.
View the status of your pipeline and jobs
deploy-prod
.
stuck
, check to ensure a runner is properly configured for the project.