GitLab
100%
Course progress
0 of 1 topics done
Keep going, you are close!
1 topic in a row
0 of 1 topics done
CoursesdevopsGitLabGitLab CI/CD Pipeline
Lesson·1 of 1

GitLab CI/CD Pipeline

50 min1 lessons
1

GitLab CI/CD Pipeline

GitLab CI/CD Pipeline

GitLab CI/CD is a built-in continuous integration and continuous deployment platform that automates the build, test, and deployment of your code.

What is GitLab CI/CD?

GitLab CI/CD uses a YAML file called .gitlab-ci.yml to define pipeline configurations. Every commit triggers a pipeline that can build, test, and deploy your code.

Basic Pipeline Structure

yaml
# .gitlab-ci.yml
stages:
  - build
  - test
  - deploy

build-job:
  stage: build
  script:
    - echo "Building the application..."
    - npm install
    - npm run build

test-job:
  stage: test
  script:
    - echo "Running tests..."
    - npm test

deploy-job:
  stage: deploy
  script:
    - echo "Deploying to production..."
  only:
    - main

Pipeline Stages

Stages define the order of execution:

yaml
stages:
  - lint
  - build
  - test
  - security
  - deploy

Jobs and Scripts

Each job runs specific commands:

yaml
lint-job:
  stage: lint
  script:
    - npm run lint
    - npm run format:check

build-job:
  stage: build
  script:
    - docker build -t myapp:$CI_COMMIT_SHA .
    - docker push myapp:$CI_COMMIT_SHA

Using Variables

yaml
variables:
  DOCKER_IMAGE: "myapp:latest"
  DEPLOY_ENV: "production"

deploy-job:
  stage: deploy
  script:
    - echo "Deploying $DOCKER_IMAGE to $DEPLOY_ENV"
  environment:
    name: production
    url: https://myapp.example.com

Caching Dependencies

yaml
build-job:
  stage: build
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/
      - .cache/
  script:
    - npm install
    - npm run build

Artifacts

yaml
build-job:
  stage: build
  script:
    - npm run build
  artifacts:
    paths:
      - dist/
    expire_in: 1 week

Manual Deployments

yaml
deploy-production:
  stage: deploy
  script:
    - ./deploy.sh production
  when: manual
  only:
    - main

Best Practices

  1. Keep pipelines fast with caching
  2. Use merge request pipelines
  3. Protect sensitive data with CI/CD variables
  4. Use tags for runner selection
  5. Implement rollback strategies
  6. Monitor pipeline performance