代码之家  ›  专栏  ›  技术社区  ›  Ijaz Ahmad

如何删除gitlab CI作业管道日志/构建和历史记录

  •  0
  • Ijaz Ahmad  · 技术社区  · 7 年前

    我们如何将gitlab配置为仅保留最后10个CI作业/构建,并继续删除其余的CI作业/构建?

    1 回复  |  直到 7 年前
        1
  •  3
  •   Eflyax    7 年前

    我认为Gitlab不支持此功能。但是您可以使用Gitlab API和webhooks自己创建此功能。

    当您推送到repo(并启动管道)时,它将触发webhook,它可以通过API=>读取您的CI历史记录;你可以删除任何你想要的。

    这是你的文件 pipeline events

    这是你的文件 job API

        2
  •  43
  •   Community Mohan Dere    6 年前

    更新

    关于 Gitlab Release 12.6 ,删除管道现在是GUI中所有者的一个选项:

    • 点击管道详细信息页面右上角的红色删除按钮。

    关于 Gitlab Release 11.6 ,现在可以通过API删除管道,仅限维护人员使用。

    你需要:

    • 这个 id 该项目的
    • pipeline_id 要删除的管道的名称。

    id: 1 和 pipeline_id: 4 :

    curl --header "PRIVATE-TOKEN: <your_access_token>" --request "DELETE" "https://gitlab.example.com/api/v4/projects/1/pipelines/46"
    

    文件是 here

        3
  •  8
  •   Mog    6 年前

    注意:需要jq。

    #!/bin/bash
    set -e
    
    TOKEN=""
    PROJECT=""
    # How many to delete from the oldest.
    PER_PAGE=100
    
    for PIPELINE in $(curl --header "PRIVATE-TOKEN: $TOKEN" "https://gitlab.com/api/v4/projects/$PROJECT/pipelines?per_page=$PER_PAGE&sort=asc" | jq '.[].id') ; do
        echo "Deleting pipeline $PIPELINE"
        curl --header "PRIVATE-TOKEN: $TOKEN" --request "DELETE" "https://gitlab.com/api/v4/projects/$PROJECT/pipelines/$PIPELINE"
    done
    
    
        4
  •  2
  •   Benji over_9000 'benchonaut'    6 年前
    _gitlab_pipeline_cleanup() {
    echo GITLAB PIPELINE CLEANUP TOOL
    echo DELETING ALL EXCEPT RUNNING AND THE MOST RECENT PIPELINE
    if [ -z $GITLABURL ]; then echo GITLAB_URL:; read GITLABURL;fi
    if [ -z $GITLAB_TOKEN ]; then echo TOKEN:; read GITLAB_TOKEN;fi
    if [ -z $PROJECT ]; then echo PROJECT_NUM:; read PROJECT;fi
    
    list=$(curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "$GITLABURL/api/v4/projects/$PROJECT/pipelines?per_page=100" |jq -c '.[] | select( .status != "running" )| .id ' |tail -n+2 |grep -v ^$)
    
    echo removing from $GITLABURL Project number $PROJECT
    while echo $(echo -n "$list" |wc -c  ) |grep -v ^0$; do 
    
    list=$(curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "$GITLABURL/api/v4/projects/$PROJECT/pipelines?per_page=100" |jq -c '.[] | select( .status != "running" )| .id ' |tail -n+2 |grep -v ^$)
    
    for item in $list ;do 
    echo -n "| -p $item |"
    curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" --request "DELETE" "$GITLABURL/api/v4/projects/$PROJECT/pipelines/$item"
    done 
    done 
    echo ; } ;
    

    那你就可以做了

    for PROJECT in 12345 23476234 2138734876238746 ;do _gitlab_pipeline_cleanup ;done
    
        5
  •  2
  •   pringi Syeda Uzma    5 年前

    根据前面的回答,修改脚本以检索多个项目,并针对每个项目删除早于配置日期的管道。

    #!/bin/bash
    set -e
    
    TOKEN=""
    # How many to delete from the oldest.
    PER_PAGE=100
    UPDATED_BEFORE=2021-02-01T00:00:00Z
    GITLAB_URL=
    
    
    while : ; do
      COUNTER=0
    
      for PROJECT in $(curl -s --header "PRIVATE-TOKEN: $TOKEN" "$GITLAB_URL/api/v4/projects?per_page=$PER_PAGE" | jq '.[].id') ; do
        echo "Deleting in project $PROJECT"
        for PIPELINE in $(curl -s --header "PRIVATE-TOKEN: $TOKEN" "$GITLAB_URL/api/v4/projects/$PROJECT/pipelines?per_page=$PER_PAGE&sort=asc&updated_before=$UPDATED_BEFORE" | jq '.[].id') ; do
            echo "Deleting pipeline $PIPELINE"
            curl --header "PRIVATE-TOKEN: $TOKEN" --request "DELETE" "$GITLAB_URL/api/v4/projects/$PROJECT/pipelines/$PIPELINE"
            (( COUNTER++ )) || true
        done
      done
    
      echo $COUNTER
    
      if [[ "$COUNTER" -le 0 ]]; then
        break;
      fi
    done
    
        6
  •  2
  •   Khetho Mtembo    5 年前

    要使用python删除项目中的所有管道,可以使用以下代码。你可以去 Jupyter 在网上试一试

    import requests
    
    def confirmDeletion():
        confirmDelete = input('Do you want to delete all pipelines in the project Y/N ?')
        if confirmDelete == 'Y' or confirmDelete == 'y'  :
            return True
        else:
            return False
    
    projectId = input('Provide the Gitlab Project ID')
    token = input('Provide the Gitlab Access Token')
    proceed = bool()
    pipelinesQueryUrl = f'https://gitlab.com/api/v4/projects/{projectId}/pipelines'
    if confirmDeletion():
        print('Deleting pipelines')
        # The Gitlab API does
        while True:
            response = requests.get(pipelinesQueryUrl, headers = {"PRIVATE-TOKEN": token})
            if response.ok:
                pipelines = response.json()
                if len(pipelines) == 0:
                    print('No more pipelines found, exiting')
                    break
                else:    
                    for pipeline in pipelines:
                        pipelineId = pipeline.get('id')
                        url = f'https://gitlab.com/api/v4/projects/{projectId}/pipelines/{pipelineId}'
                        response = requests.delete(url, headers = {"PRIVATE-TOKEN": token})
                        if response.ok:
                            print(f'Pipeline {pipelineId} succesfully deleted')
                        else:
                            print (f'Deleting pipeline {pipelineId} on path failed: {response.url} : ({response.status_code}) {response.reason}')
                            if response.status_code == 429:
                                # Watch out for rate specific limits https://docs.gitlab.com/ee/user/gitlab_com/index.html#gitlabcom-specific-rate-limits
                                print ('Rate Limits have been reached, wait and try again later')
                                break
            else:
                print (f'Querying for pipelines failed: {response.url}: ({response.status_code}) {response.reason}')
                if response.status_code == 429:
                    # Watch out for rate specific limits https://docs.gitlab.com/ee/user/gitlab_com/index.html#gitlabcom-specific-rate-limits
                    print ('Rate Limits have been reached, wait and try again later')
                break
    else:
        print('No pipelines deleted')
    
    
    
    
        7
  •  1
  •   fommil Adamski    6 年前

    对于懒惰的人来说,要扩展 https://stackoverflow.com/a/55815040/1041691

    拿你的 PROJECT 和 TOKEN 然后运行此操作,直到删除所有管道

    for PIPELINE in $(curl --header "PRIVATE-TOKEN: $TOKEN" "https://gitlab.com/api/v4/projects/$PROJECT/jobs?per_page=100" | jq '.[].pipeline.id') ; do
        echo "deleting $PIPELINE"
        curl --header "PRIVATE-TOKEN: $TOKEN" --request "DELETE" "https://gitlab.com/api/v4/projects/$PROJECT/pipelines/$PIPELINE"
    done
    
    
        8
  •  1
  •   Wolfgang    5 年前

    #!/bin/bash
    set -e
    
    TOKEN="---your token here--"
    PROJECT="PROJECT-ID"
    BASEURL="https://gitlab.com/api/v4/projects" # Base Url, if you host your 
    # Status values: created, waiting_for_resource, preparing, pending, running, success, failed, canceled, skipped, manual, scheduled
    STATUS=(failed canceled skipped) # separate status by space
    
    # How many to delete from the oldest.
    DELETE_CNT=500
    
    # -----
    CNT=0
    declare -A STATUS_RESULT
    
    for CUR_STATUS in "${STATUS[@]}";
    do
      TEMP_CNT=$CNT
      DOLOOP=true
      while [ "$DOLOOP" = true ]
      do
        DOLOOP=false
    
        for PIPELINE in $(curl --header "PRIVATE-TOKEN: $TOKEN" "$BASEURL/$PROJECT/pipelines?per_page=$DELETE_CNT&sort=asc&status=$CUR_STATUS" | jq '.[].id') ; do
          if [[ "$CNT" -lt "$DELETE_CNT" ]]; then
            echo "#$CNT Deleting pipeline $PIPELINE with status $CUR_STATUS"
            curl --header "PRIVATE-TOKEN: $TOKEN" --request "DELETE" "$BASEURL/$PROJECT/pipelines/$PIPELINE"
            let "CNT+=1"
            DOLOOP=true
          fi
        done
    
        if [ "$DOLOOP" = true ] ; then
          if [[ "$CNT" -ge "$DELETE_CNT" ]]; then
           DOLOOP=false
          fi
        fi
      done
    
      TEMP_CNT=$((CNT-TEMP_CNT))
      STATUS_RESULT[$CUR_STATUS]=$TEMP_CNT
      echo "Removed $TEMP_CNT pipelines with status $CUR_STATUS"
    done
    
    echo "===================================================="
    echo "= Summary of removed pipelines (max: $DELETE_CNT)"
    echo "=   Total: $CNT"
    echo "="
    for key in "${!STATUS_RESULT[@]}"; do
        CNT=${STATUS_RESULT[$key]}
        echo "=   $key: $CNT"
    done
    
        9
  •  1
  •   gdm    5 年前

    所有解决方案都有效,但请记住,您必须生成个人访问令牌,而不是API令牌。如果您必须是无所不在的令牌,则必须是管理员。

        10
  •  0
  •   Malcolm Boekhoff    5 年前

    全部的 一个项目的管道,但我相信您可以找出Perl的东西来跳过前10个

    curl -s --header "PRIVATE-TOKEN: ******" --request "GET" "https://gitlab.com/api/v4/projects/********/pipelines?per_page=32767" \
    | perl -MJSON -le '$d = decode_json(<>); map { print $_->{"id"} } @$d' - | while read i; do
      curl -s --header "PRIVATE-TOKEN: ********" --request "DELETE" "https://gitlab.com/api/v4/projects/******/pipelines/$i"
    done
    

    项目ID写在项目名称(项目页面)下,您可以从“编辑配置文件”“访问令牌”中获取访问令牌,只需选中“API”复选框即可。

    在Linux上通过以下方式在Perl中安装JSON模块:

    sudo apt -y install libjson-perl