Middle
Tell about the rules and processes of the workflow in GitLab CI.
sobes.tech AI
Answer from AI
GitLab CI/CD workflow is based on the .gitlab-ci.yml file located at the root of the repository, which describes the pipeline consisting of stages and jobs. The pipeline is triggered automatically by various triggers (pushes, tags, schedules).
Key rules:
- Declarative: The pipeline is described in a YAML file.
- Stages: Run sequentially. Default: build, test, deploy. Custom stages can be defined.
- Jobs: Can run in parallel within a stage.
- Runners: Execute jobs. They can be shared or specific, on various platforms (Docker, Shell, Kubernetes, etc.).
- Artifacts: Files generated by jobs, which can be passed to subsequent jobs or downloaded by users.
- Cache: Allows storing dependencies and other files between pipeline runs to speed up execution.
- Rules: Define when a job should run based on conditions (branches, tags, file changes, etc.).
- Variables: Parameterize the pipeline (predefined, protected, group, project variables).
- Secrets: Store sensitive data via GitLab CI/CD Variables with masking.
Workflow processes:
- Code Commit: Developer makes changes and pushes to the GitLab repository.
- Pipeline Trigger: GitLab automatically starts the pipeline based on
.gitlab-ci.yml. - Stage Execution: The pipeline begins executing stage jobs sequentially.
- Job Execution: Jobs in the current stage run in parallel on available runners.
- Build: The build job compiles code and creates artifacts.
- Test: The test job runs unit and integration tests.
- Deploy: The deploy job deploys the application to environments (Staging, Production).
- Artifacts & Cache Management: Artifacts are collected, cache is used/updated.
- Notifications: GitLab sends notifications about the pipeline status (success, failure).
- Monitoring & Logging: Task statuses and logs are available in the GitLab UI.
Example .gitlab-ci.yml structure:
stages:
- build
- test
- deploy
variables:
IMAGE_NAME: my-app
build:
stage: build
script:
- echo " building..."
- docker build -t $IMAGE_NAME:$CI_COMMIT_SHORT_SHA .
test:
stage: test
script:
- echo " testing..."
- docker run $IMAGE_NAME:$CI_COMMIT_SHORT_SHA npm test
deploy:
stage: deploy
script:
- echo " deploying..."
# Deployment example, depends on environment (Kubernetes, VM, etc.)
- echo "Deploying to staging..."
rules:
- if: $CI_COMMIT_BRANCH == "main" # Deploy only from the main branch