Commit 57420e79 authored by Dominik Hebeler's avatar Dominik Hebeler
Browse files

initial commit

parents
Pipeline #4013 passed with stage
in 1 minute and 5 seconds
variables:
DOCKER_HOST: "tcp://docker-dind.gitlab-suma:2375"
AUTO_DEVOPS_BUILD_IMAGE_EXTRA_ARGS: "--network host"
POSTGRES_ENABLED: "false"
CODE_QUALITY_DISABLED: "true"
CONTAINER_SCANNING_DISABLED: "true"
DAST_DISABLED: "true"
DEPENDENCY_SCANNING_DISABLED: "true"
LICENSE_MANAGEMENT_DISABLED: "true"
PERFORMANCE_DISABLED: "true"
SAST_DISABLED: "true"
TEST_DISABLED: "true"
include:
- template: Jobs/Build.gitlab-ci.yml
FROM dtzar/helm-kubectl
RUN apk add --update \
curl \
php7-cli \
php7-curl \
php7-json \
&& rm -rf /var/cache/apk
COPY scripts /scripts
WORKDIR /scripts
CMD php deleteOldBranches.php
\ No newline at end of file
<?php
/**
* Deletes old registries from our gitlab that do not have a corresponding branch to it
*/
$gitlab_url = getenv("GITLAB_URL");
$token = getenv("TOKEN");
$projects = getProjects();
print("Checking for registry images that belong to non existing branches!" . PHP_EOL);
foreach ($projects as $index => $project) {
print("[" . ($index + 1) . "/" . sizeof($projects) . "] Checking project " . $project["name_with_namespace"] . PHP_EOL);
$registries = getRegistry($project["id"]);
foreach ($registries as $index => $registry) {
$branch = getBranch($registry["project_id"], $registry["name"]);
if (empty($branch)) {
print("\t[" . ($index + 1) . "/" . sizeof($registries) . "] Branch " . $registry["name"] . " existiert nicht! Removing registry entry... ");
deleteRegistryEntry($project["id"], $registry["id"]);
print(PHP_EOL);
}
}
}
function deleteRegistryEntry($projectId, $registryId)
{
global $token, $gitlab_url;
$url = $gitlab_url . "/api/v4/projects/$projectId/registry/repositories/$registryId?private_token=$token";
$ch = curl_init();
try {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
print($httpCode);
} finally {
curl_close($ch);
}
}
function getBranch($projectId, $branchName)
{
global $token, $gitlab_url;
$branch = file_get_contents($gitlab_url . "/api/v4/projects/$projectId/repository/branches?private_token=$token" . "&search=^" . urlencode($branchName));
$branch = json_decode($branch);
return $branch;
}
function getRegistry($id)
{
global $token, $gitlab_url;
$resultregistries = [];
$page = 1;
while (true) {
$registries = file_get_contents($gitlab_url . "/api/v4/projects/$id/registry/repositories?private_token=$token" . "&page=$page");
$registries = json_decode($registries, true);
$resultregistries = array_merge($resultregistries, $registries);
$nextPage;
foreach ($http_response_header as $header) {
if (stripos($header, "X-Next-Page") !== false) {
$nextPage = trim(explode(":", $header)[1]);
}
}
if (empty($nextPage)) {
break;
} else {
$page = $nextPage;
}
}
return $resultregistries;
}
function getProjects()
{
global $token, $gitlab_url;
$resultProjects = [];
$page = 1;
while (true) {
$projects = file_get_contents($gitlab_url . "/api/v4/projects?simple=true&private_token=$token" . "&page=$page");
$projects = json_decode($projects, true);
$resultProjects = array_merge($resultProjects, $projects);
$nextPage;
foreach ($http_response_header as $header) {
if (stripos($header, "X-Next-Page") !== false) {
$nextPage = trim(explode(":", $header)[1]);
}
}
if (empty($nextPage)) {
break;
} else {
$page = $nextPage;
}
}
return $resultProjects;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment