linux

GitLab如何在Linux中集成CI/CD

小樊
39
2025-10-10 02:43:56
栏目: 智能运维

Integrating GitLab CI/CD in Linux: A Step-by-Step Implementation Guide

GitLab CI/CD is a powerful built-in tool for automating code integration, testing, and deployment. Integrating it with a Linux environment involves setting up GitLab Runner (the execution agent), defining a .gitlab-ci.yml configuration file (the pipeline blueprint), and configuring runners to execute tasks. Below is a structured guide to achieve this integration.

1. Prerequisites

Before starting, ensure your Linux environment meets the following requirements:

2. Install GitLab Runner

GitLab Runner is responsible for executing CI/CD jobs defined in .gitlab-ci.yml. You can install it using one of the following methods (Docker is recommended for simplicity):

Option A: Install via Docker (Recommended)

Run the following command to start a GitLab Runner container:

docker run -d --name gitlab-runner \
  --restart always \
  -v /var/run/docker.sock:/var/run/docker.sock \  # Allows Runner to use Docker inside containers
  -v /srv/gitlab-runner/config:/etc/gitlab-runner \  # Persists Runner configuration
  gitlab/gitlab-runner:latest

This command:

Option B: Install via Package Manager (Ubuntu/CentOS)

For a system-wide installation (not containerized), use:

Register the Runner with GitLab

After installation, register the Runner to your GitLab project:

sudo gitlab-runner register

Follow the interactive prompts:

  1. Enter the GitLab instance URL (e.g., http://gitlab.example.com).
  2. Provide the registration token (found in Project Settings → CI/CD → Runners).
  3. Choose an executor (e.g., docker for containerized jobs or shell for direct shell execution).
  4. Add default tags (optional, but useful for filtering jobs).

Verify registration by visiting Project Settings → CI/CD → Runners—your Runner should appear as “Active.”

3. Create .gitlab-ci.yml Configuration File

The .gitlab-ci.yml file defines your CI/CD pipeline’s structure (stages, jobs, and scripts). Place it in the root directory of your project repository.

Basic Structure Example

stages:  # Define pipeline stages (executed in order)
  - build
  - test
  - deploy

variables:  # Optional: Define reusable variables
  DOCKER_IMAGE: "my-app:latest"

build_job:  # First job in the "build" stage
  stage: build
  image: node:18  # Use a Node.js Docker image
  script:
    - echo "Installing dependencies..."
    - npm install
    - echo "Building project..."
    - npm run build
  artifacts:  # Pass built files to subsequent jobs
    paths:
      - dist/
    expire_in: 1 hour

test_job:  # Second job in the "test" stage
  stage: test
  image: node:18
  script:
    - echo "Running unit tests..."
    - npm test
  needs: ["build_job"]  # Only run after build_job succeeds

deploy_job:  # Third job in the "deploy" stage
  stage: deploy
  image: alpine:latest  # Lightweight image for deployment
  script:
    - echo "Deploying to production..."
    - apk add --no-cache openssh
    - ssh user@production-server "rm -rf /var/www/my-app/*"
    - scp -r dist/* user@production-server:/var/www/my-app/
  only: ["main"]  # Only trigger on pushes to the "main" branch
  when: on_success  # Only run if previous jobs succeed

Key Components Explained

For advanced configurations (e.g., caching, Docker-in-Docker), refer to the official GitLab CI/CD documentation.

4. Trigger the CI/CD Pipeline

Once the .gitlab-ci.yml file is committed to your repository’s default branch (e.g., main), GitLab automatically detects it and triggers a pipeline. You can also manually trigger pipelines from the CI/CD → Pipelines page in your project.

To verify the pipeline is running:

  1. Go to your project’s CI/CD → Pipelines page.
  2. Click on the latest pipeline to see job statuses (e.g., running, success, failed).
  3. View job logs to debug issues (e.g., script errors, missing dependencies).

5. Advanced Configurations

Caching Dependencies

Speed up builds by caching dependencies between jobs. For example, cache node_modules in a Node.js project:

build_job:
  stage: build
  image: node:18
  script:
    - npm install
    - npm run build
  cache:
    paths:
      - node_modules/  # Cache this directory
    policy: pull-push  # Save and reuse the cache

Using Docker-in-Docker (DinD)

For jobs that require building Docker images, use the docker:dind (Docker-in-Docker) service:

build_image_job:
  stage: build
  image: docker:24
  services:
    - docker:dind  # Start a Docker daemon inside the job
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker build -t $CI_REGISTRY_IMAGE:latest .
    - docker push $CI_REGISTRY_IMAGE:latest
  variables:
    DOCKER_TLS_CERTDIR: ""  # Disable TLS for faster execution

Secure Sensitive Data

Store sensitive information (e.g., SSH keys, API tokens) in GitLab’s CI/CD Variables (Settings → CI/CD → Variables). Use them in your pipeline like this:

deploy_job:
  stage: deploy
  script:
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
    - ssh -o StrictHostKeyChecking=no user@server "deploy-command"

Mark variables as Protected to restrict access to protected branches/environments.

6. Monitor and Optimize

Use GitLab’s built-in tools to track pipeline performance and resolve issues:

Optimize pipelines by:

By following these steps, you can successfully integrate GitLab CI/CD into your Linux environment, enabling automated, reliable, and scalable software delivery.

0
看了该问题的人还看了