
1. Introduction:
.gitlab-ci.yml file.2. Key Concepts:
3. Basic .gitlab-ci.yml Example:
YAML Syntax:
stages:
- build
- test
- deploy
build-job:
stage: build
script:
- echo "Building the project..."
- make
test-job:
stage: test
script:
- echo "Running tests..."
- make test
deploy-job:
stage: deploy
script:
- echo "Deploying the project..."
- make deploy
4. Runners:
5. Artifacts and Caching:
Artifacts: Save job outputs and make them available to subsequent jobs.
artifacts:
paths:
- build/
expire_in: 1 week
Caching: Speed up jobs by reusing previously downloaded dependencies.
cache:
paths:
- node_modules/
6. Environments and Deployments:
Environments: Define environments to organize and manage deployments.
deploy-job:
stage: deploy
environment:
name: production
url: https://myapp.com
script:
- echo "Deploying to production..."
- ./deploy.sh
Manual Deployments: Require manual approval before a job runs.
deploy-job:
stage: deploy
script:
- ./deploy.sh
when: manual
7. Advanced .gitlab-ci.yml Features:
YAML Anchors: Reuse parts of your YAML configuration.
.default-job: &default-job
script:
- echo "Default job script"
job1:
<<: *default-job
job2:
<<: *default-job
Includes: Include other YAML files to organize your configuration.
include:
- local: '/templates/.gitlab-ci-template.yml'
8. Security and Compliance:
Secret Variables: Store sensitive data securely in GitLab CI/CD.
deploy-job:
script:
- deploy --token $CI_DEPLOY_TOKEN
Protected Branches: Restrict certain jobs to run only on protected branches.
9. Troubleshooting:
10. Best Practices: