Help us learn about your current experience with the documentation. Take the survey.

教程:设置流水线执行策略

  • Tier: Ultimate
  • Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated

本教程将向您展示如何使用 inject_policy 策略创建和配置一个 流水线执行策略。您可以使用这些策略来确保在策略所链接的项目中,必需的流水线始终会运行。

在本教程中,您将创建一个流水线执行策略,将其链接到一个测试项目,并验证流水线是否成功执行。

要设置流水线执行策略,您需要:

  1. 创建一个测试项目
  2. 创建一个 CI/CD 配置文件
  3. 添加一个流水线执行策略
  4. 测试流水线执行策略

开始之前

要完成本教程,您需要:

  • 在现有群组中创建项目的权限。
  • 创建和链接安全策略的权限。

创建测试项目

首先,创建一个测试项目来应用您的流水线执行策略:

  1. 在左侧边栏,选择 Search or go to 并找到您的群组。
  2. 选择 New project
  3. 选择 Create blank project
  4. 填写以下字段。
    • Project name: my-pipeline-execution-policy
    • 勾选 Enable Static Application Security Testing (SAST) 复选框。
  5. 选择 Create project

创建 CI/CD 配置文件

接下来,创建您希望流水线执行策略强制执行的 CI/CD 配置文件:

  1. 选择 Code > Repository

  2. Add (+) 下拉列表中,选择 New file

  3. Filename 字段中,输入 pipeline-config.yml

  4. 在文件内容中,复制以下内容:

    # 此文件定义了将由流水线执行策略强制执行的 CI/CD 作业
    enforced-security-scan:
      stage: .pipeline-policy-pre
      script:
        - echo "Running enforced security scan from pipeline execution policy"
        - echo "This job cannot be skipped by developers"
        - echo "Checking for security vulnerabilities..."
        - echo "Security scan completed successfully"
      rules:
        - when: always
    
    enforced-test-job:
      stage: test
      script:
       - echo "Running enforced test job in test stage"
       - echo "Creating test stage if it doesn't exist"
       - echo "Performing mandatory testing requirements..."
       - echo "Enforced tests completed successfully"
     rules:
       - when: always
    
    enforced-compliance-check:
      stage: .pipeline-policy-post
      script:
        - echo "Running enforced compliance check"
        - echo "Verifying pipeline compliance requirements"
        - echo "Compliance check passed"
      rules:
        - when: always
  5. Commit message 字段中,输入 Add pipeline execution policy configuration

  6. 选择 Commit changes

添加流水线执行策略

接下来,向您的测试项目添加一个流水线执行策略:

  1. 选择 Secure > Policies

  2. 选择 New policy

  3. Pipeline execution policy 中,选择 Select policy

  4. 填写以下字段。

    • Name: Enforce Security and Compliance Jobs
    • Description: Enforces required security and compliance jobs across all pipelines
    • Policy status: Enabled
  5. Actions 设置为以下内容:

    将 My Pipeline Execution Policy 中的流水线执行文件注入到 .gitlab-ci.yml 中
    文件路径: [group]/my-pipeline-execution/policy/pipeline-config.yml
  6. 选择 Configure with a merge request

  7. 在合并请求的 Changes 标签页中查看生成的策略 YAML。策略内容应类似于:

    ---
    pipeline_execution_policy:
    - name: Enforce Security and Compliance Jobs
      description: Enforces required security and compliance jobs across all pipelines
      enabled: true
      pipeline_config_strategy: inject_policy
      content:
        include:
        - project: [group]/my-pipeline-execution-policy
          file: pipeline-config.yml
      skip_ci:
        - allowed: false
  8. 转到 Overview 标签页并选择 Merge。此步骤会创建一个名为 My Pipeline Execution Policy - Security Policy Project 的新项目。安全策略项目用于存储安全策略,以便在多个项目中强制执行相同的策略。

  9. 在左侧边栏,选择 Search or go to 并找到 my-pipeline-execution-policy 项目。

  10. 选择 Secure > Policies

    您可以在此看到之前添加的策略列表。

测试流水线执行策略

现在,通过创建一个合并请求来测试您的流水线执行策略:

  1. 选择 Code > Repository

  2. Add (+) 下拉列表中,选择 New file

  3. Filename 字段中,输入 test-file.txt

  4. 在文件内容中,添加:

    这是一个用于触发流水线执行策略的测试文件。
  5. Commit message 字段中,输入 Add test file to trigger pipeline

  6. Target Branch 字段中,输入 test-policy-branch

  7. 选择 Commit changes

  8. 当合并请求页面打开时,选择 Create merge request

    等待流水线完成。这可能需要几分钟时间。

  9. 在合并请求中,选择 Pipelines 标签页并选择已创建的流水线。

    您应该看到正在运行的强制作业:

    • enforced-security-scan.pipeline-policy-pre 阶段运行(最先执行)
    • enforced-test-jobtest 阶段运行(由策略注入)
    • enforced-compliance-check.pipeline-policy-post 阶段运行(最后执行)
  10. 选择 enforced-security-scan 作业以查看其日志,并确认它按照策略定义执行了安全扫描。

流水线执行策略成功强制执行了必需的作业,确保无论开发者在项目的 .gitlab-ci.yml 文件中包含什么内容,这些作业都会运行。

您现在已了解如何设置和使用流水线执行策略,以在您组织内的各个项目中强制使用必需的 CI/CD 作业!

后续步骤