GitLab CI流水线权限隔离

06-01 1333阅读

方案概述

本方案实现在GitLab CI/CD中根据不同人员的权限级别执行不同的流水线步骤,主要基于GitLab的以下特性:

  • rules 条件判断
  • variables 变量传递
  • only/except 条件限制
  • 用户权限API查询

    基础权限模型设计

    1. 用户角色定义

    角色描述对应GitLab权限
    Developer普通开发人员Developer
    Maintainer项目维护者Maintainer
    Owner项目所有者Owner
    Auditor审计人员Reporter

    2. 权限与流水线阶段对应关系

    流水线阶段DeveloperMaintainerOwnerAuditor
    build
    test
    staging
    production
    audit

    实现方案

    1. 基于分支保护的方案

    stages:
      - build
      - test
      - staging
      - production
      - audit
    build:
      stage: build
      script: echo "Building..."
      rules:
        - if: '$CI_PIPELINE_SOURCE == "push"'
    test:
      stage: test
      script: echo "Testing..."
      rules:
        - if: '$CI_PIPELINE_SOURCE == "push"'
    staging:
      stage: staging
      script: echo "Deploying to staging..."
      rules:
        - if: '$CI_COMMITTER_ACCESS_LEVEL >= 40' # Maintainer及以上
        - if: '$CI_DEPLOY_USER_ACCESS_LEVEL >= 40'
    production:
      stage: production
      script: echo "Deploying to production..."
      rules:
        - if: '$CI_COMMITTER_ACCESS_LEVEL == 50' # Only Owner
        - if: '$CI_DEPLOY_USER_ACCESS_LEVEL == 50'
    audit:
      stage: audit
      script: echo "Running audit..."
      rules:
        - if: '$CI_COMMITTER_ACCESS_LEVEL == 20' # Reporter
        - if: '$CI_DEPLOY_USER_ACCESS_LEVEL == 20'
    

    2. 基于自定义变量的方案(更灵活)

    variables:
      # 通过API获取用户权限级别
      USER_ACCESS_LEVEL: $(
        curl --header "PRIVATE-TOKEN: $CI_JOB_TOKEN" \
        "$CI_API_V4_URL/projects/$CI_PROJECT_ID/members/$GITLAB_USER_ID" | \
        jq '.access_level'
      )
    stages:
      - build
      - test
      - staging
      - production
      - audit
    .job_template: &job_settings
      interruptible: true
      tags:
        - docker
    build:
      
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。

目录[+]

取消
微信二维码
微信二维码
支付宝二维码