手动从CI中删除工件

时间:2017-02-28 15:28:49

标签: gitlab-ci

我在gitlab.com上有一个使用CI功能的私人存储库。某些CI作业会创建存储的工件文件。我刚刚实现了一天后通过将其添加到CI配置中自动删除工件:

expire_in: 1 day

这很有效 - 然而,旧的工件不会被删除(如预期的那样)。所以我的问题是:

如何删除未过期的旧工件或工件? (在gitlab.com上,无法直接访问服务器)

6 个答案:

答案 0 :(得分:7)

如果您无法直接访问服务器,则可以使用GitLab REST API删除作业中的工件。这是使用API​​的sample curl script

#!/bin/bash

# project_id, find it here: https://gitlab.com/[organization name]/[repository name]/edit inside the "General project settings" tab
project_id="3034900"

# token, find it here: https://gitlab.com/profile/personal_access_tokens
token="Lifg_azxDyRp8eyNFRfg"
server="gitlab.com"

# go to https://gitlab.com/[organization name]/[repository name]/-/jobs
# then open JavaScript console
# copy/paste => copy(_.uniq($('.ci-status').map((x, e) => /([0-9]+)/.exec(e.href)).toArray()).join(' '))
# press enter, and then copy the result here :
# repeat for every page you want
job_ids=(48875658 48874137 48873496 48872419)

for job_id in ${job_ids[@]};
do
 URL="https://$server/api/v4/projects/$project_id/jobs/$job_id/erase"
 echo "$URL"
 curl --request POST --header "PRIVATE-TOKEN:${token}" "$URL"
 echo "\n"
done

答案 1 :(得分:5)

我不是GItlab 8.17并且能够通过导航到服务器本身的存储目录来删除特定作业的工件,默认路径是:

            $('.grid').packery({
        // options
        itemSelector: '.grid-item',
        gutter: 0,

      });

      $('.grid').packery({
        percentPosition: true
      })

删除作业的整个文件夹或仅删除内容,从GitLab pipline页面删除工件视图

可以按照文档中的说明更改存储路径: https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/administration/job_artifacts.md#storing-job-artifacts

答案 2 :(得分:3)

以@David的答案为基础, @Philipp指出,现在有一个api端点仅删除作业工件,而不是整个作业。

您可以直接在控制台中运行此脚本,也可以使用node-fetch在node.js中运行。

//Go to: https://gitlab.com/profile/personal_access_tokens
const API_KEY = "API_KEY";

//You can find project id inside the "General project settings" tab
const PROJECT_ID = 12345678;
const PROJECT_URL = "https://gitlab.com/api/v4/projects/" + PROJECT_ID + "/"

let jobs = [];
for(let i = 0, currentJobs = []; i == 0 || currentJobs.length > 0; i++){
    currentJobs = await sendApiRequest(
        PROJECT_URL + "jobs/?per_page=100&page=" + (i + 1)
    ).then(e => e.json());
    jobs = jobs.concat(currentJobs);
}

//skip jobs without artifacts
jobs = jobs.filter(e => e.artifacts);

//keep the latest build.
jobs.shift();

for(let job of jobs)
    await sendApiRequest(
        PROJECT_URL + "jobs/" + job.id + "/artifacts",
        {method: "DELETE"}
    );

async function sendApiRequest(url, options = {}){
    if(!options.headers)
        options.headers = {};
    options.headers["PRIVATE-TOKEN"] = API_KEY;

    return fetch(url, options);
}

答案 3 :(得分:1)

根据the documentation,删除整个作业日志(单击垃圾箱)也将删除工件。

答案 4 :(得分:0)

如果您意外删除了所有作业(认为工件会消失,但它们没有),那么可以用蛮力强制循环范围吗?

我有这段代码,可以对一系列数字进行暴力破解。但是由于我使用的是gitlab.com公共赛跑者,所以这是远程的

    # project_id, find it here: https://gitlab.com/[organization name]/[repository name]/edit inside the "General project settings" tab
project_id="xxxxxx" #

# token, find it here: https://gitlab.com/profile/personal_access_tokens
token="yyyyy"
server="gitlab.com"


# Get a range of the oldest known job and the lastet known one, then bruteforce. Used in the case when you deleted pipelines and can't retrive Job Ids.

# https://stackoverflow.com/questions/52609966/for-loop-over-sequence-of-large-numbers-in-bash
for (( job_id = 59216999; job_id <= 190239535; job_id++ )) do
echo "$job_id"

echo Job ID being deleted is "$job_id"

curl --request POST --header "PRIVATE-TOKEN:${token}" "https://${server}/api/v4/projects/${project_id}/jobs/${job_id}/erase"
echo -en '\n'
echo -en '\n'
done

答案 5 :(得分:0)

这个 Python 解决方案适用于 GitLab 13.11.3。

    FlatlandCBS(p::object railEnv) :
        m_railEnv(railEnv),
        map(
          p::extract<np::ndarray>(
            p::extract<p::object>(
              railEnv.attr("distance_map")
            ).attr("get")()
          )
        )
    { }

我会保留更新版本 cppreference。随时联系并改进代码。