GitLab CI/CD pipelines are defined using a YAML file called .gitlab-ci.yml
which is stored in the root directory of your GitLab project. This YAML file specifies the stages, jobs, and commands needed to build, test, and deploy your project. Let me break down the basic structure and provide examples:
1. Define Stages:
Stages represent the different steps in your pipeline. Each stage can contain one or more jobs, and stages run sequentially.
stages: - build - test - deploy
2. Define Jobs:
Jobs are the individual tasks that need to be performed within each stage. Each job runs in a separate environment, and you can specify dependencies between jobs.
job_name: stage: stage_name script: - command1 - command2
Example
3. Define Scripts:
In each job, you can specify the commands that need to be executed. This can include build commands, testing commands, deployment scripts, etc.
Example:
4. Define Environment:
You can define environments for your jobs, such as staging or production. This helps in deploying to specific environments and managing releases.
Example:
5. Define Triggers:
You can define triggers for your pipeline, such as when to run the pipeline (e.g., on every push to a specific branch, on a schedule, manually triggered, etc.).
Example:
6. Define Variables:
You can define variables to be used in your pipeline, such as API keys, environment-specific configurations, etc.
Example:
Let's delve deeper into some advanced features and configurations available in GitLab CI/CD YAML pipelines:
1. Artifacts:
Artifacts are files generated by your jobs that you want to keep or use in subsequent stages. You can define artifacts to be passed between stages or to be downloaded after the job finishes.
build: stage: build script: - npm install - npm run build artifacts: paths: - dist/
2. Dependencies:
You can define dependencies between jobs, ensuring that one job runs only after the successful completion of another job.
test: stage: test script: - npm test dependencies: - build
3. Caching:
Caching allows you to cache dependencies between job runs, speeding up subsequent pipeline executions.
build: stage: build script: - npm install - npm run build cache: paths: - node_modules/
4. Rules:
Rules allow you to define conditions under which jobs will be executed. This provides more flexibility than simple only
or except
directives.
deploy: stage: deploy script: - deploy_script.sh rules: - if: '$CI_COMMIT_BRANCH == "main"' when: always
5. Parallel Jobs:
You can define jobs to run in parallel, improving the overall pipeline execution time.
test: stage: test script: - npm test parallel: 3
6. Manual Jobs:
Manual jobs require manual intervention before they can be executed, useful for tasks like deployment to production.
deploy_production: stage: deploy script: - deploy_script.sh environment: name: production url: https://example.com when: manual
7. Triggering External Pipelines:
You can trigger pipelines in other projects, enabling cross-project dependencies and workflows.
trigger: include: - project: 'group/project' ref: main
8. Custom Docker Images:
You can specify custom Docker images to use in your jobs, allowing for a specific environment setup.
image: node:14
9. Secrets:
You can securely store and use secrets such as API keys, SSH keys, etc., in your pipeline.
deploy: stage: deploy script: - deploy_script.sh environment: name: production url: https://example.com variables: SECRET_KEY: $SECRET_KEY
Here's a list of key keywords used in GitLab CI/CD YAML files along with examples and descriptions:
stages:
- Example:
- Description: Defines the different stages of the pipeline. Jobs within the same stage run in parallel, while stages run sequentially.
job:
- Example:
- Description: Defines a job to be executed in a particular stage. It includes the script or commands to be run.
script:
- Example:
- Description: Contains the commands or script to be executed for the job.
artifacts:
- Example:
- Description: Specifies files or directories generated by the job that should be passed to subsequent stages or available for download.
dependencies:
- Example:
- test: stage: test script: - npm test dependencies: - build
- Description: Defines dependencies between jobs, ensuring that a job runs only after the successful completion of specified jobs.
cache:
- Example:
- Description: Configures caching of files or directories between job runs, improving pipeline execution speed.
rules:
- Example:
- Description: Specifies conditions under which jobs will be executed, providing more flexibility than simple
only
orexcept
directives.
- Description: Specifies conditions under which jobs will be executed, providing more flexibility than simple
parallel:
- Example:
- Description: Defines the number of parallel jobs to be executed for a job.
when:
- Example:
- Description: Specifies when a job should be executed. It can be
on_success
,on_failure
,always
, ormanual
.
- Description: Specifies when a job should be executed. It can be
trigger:
- Example:
- Description: Triggers pipelines in other projects, enabling cross-project dependencies and workflows.
image:
- Example:
- Description: Specifies a custom Docker image to use for the job, allowing for a specific environment setup.
variables:
- Example:
- Description: Defines variables to be used in the job, such as secrets, environment-specific configurations, etc.
These are the key keywords used in GitLab CI/CD YAML files along with examples and descriptions. They provide the necessary structure and configuration options to define and customize your pipeline.