Compare commits
2 Commits
2026.02.15
...
2026.02.15
| Author | SHA1 | Date | |
|---|---|---|---|
| 4ff5680310 | |||
| 6d3a5b1930 |
@@ -36,7 +36,7 @@ jobs:
|
||||
cd sh.actions.package-cleanup
|
||||
|
||||
# Build for Linux x64
|
||||
dotnet publish -c Release -r linux-x64 -p:Version=$(cat ../../version.txt) -o publish/linux-x64
|
||||
dotnet publish -c Release -r linux-x64 -p:Version=$(cat ../version.txt) -o publish/linux-x64
|
||||
|
||||
- name: Upload to Gitea Generic Package Registry
|
||||
run: |
|
||||
@@ -44,4 +44,4 @@ jobs:
|
||||
curl -X PUT \
|
||||
-H "Authorization: token ${{ secrets.CI_ACCESS_TOKEN }}" \
|
||||
-T publish/linux-x64/sh.actions.package-cleanup \
|
||||
"https://git.sh-edraft.de/api/packages/sh-edraft.de/generic/package-cleanup/$(cat ../../version.txt)/package-cleanup-linux-x64"
|
||||
"https://git.sh-edraft.de/api/packages/sh-edraft.de/generic/package-cleanup/$(cat ../version.txt)/package-cleanup-linux-x64"
|
||||
|
||||
@@ -15,5 +15,9 @@ builder.Services
|
||||
.AddHostedService<Worker>()
|
||||
.AddHttpClient<IGiteaPackageService, GiteaPackageService>();
|
||||
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Logging.AddConsole();
|
||||
builder.Logging.SetMinimumLevel(LogLevel.Debug);
|
||||
|
||||
var host = builder.Build();
|
||||
await host.RunAsync();
|
||||
|
||||
@@ -11,50 +11,26 @@ public class Worker(
|
||||
IGiteaPackageService giteaPackageService
|
||||
) : BackgroundService
|
||||
{
|
||||
private async Task<int> DeletePackages(List<GiteaPackage> packages, CancellationToken cancellationToken = default)
|
||||
private async Task DeletePackages(List<GiteaPackage> packages, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var dryRun = configuration["DRY_RUN"]?.ToLower() == "true";
|
||||
if (dryRun)
|
||||
{
|
||||
logger.LogInformation("Dry run enabled, not deleting {Count} packages", packages.Count);
|
||||
return 0;
|
||||
}
|
||||
|
||||
var deletedCount = 0;
|
||||
foreach (var giteaPackage in packages)
|
||||
{
|
||||
try
|
||||
{
|
||||
await giteaPackageService.DeletePackage(giteaPackage, cancellationToken);
|
||||
deletedCount++;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to delete package {PackageName} version {Version}",
|
||||
logger.LogError(ex, "Failed to delete package {PackageName} version {Version}",
|
||||
giteaPackage.Name, giteaPackage.Version);
|
||||
}
|
||||
}
|
||||
return deletedCount;
|
||||
}
|
||||
|
||||
private void WriteGitHubOutput(string name, string value)
|
||||
{
|
||||
var githubOutput = configuration["GITHUB_OUTPUT"];
|
||||
if (string.IsNullOrEmpty(githubOutput))
|
||||
{
|
||||
logger.LogDebug("GITHUB_OUTPUT not set, skipping output: {Name}={Value}", name, value);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
File.AppendAllText(githubOutput, $"{name}={value}{Environment.NewLine}");
|
||||
logger.LogInformation("Wrote to GITHUB_OUTPUT: {Name}={Value}", name, value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to write to GITHUB_OUTPUT file");
|
||||
}
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
|
||||
@@ -68,43 +44,33 @@ public class Worker(
|
||||
if (names.Length == 0)
|
||||
{
|
||||
logger.LogWarning("No package names configured");
|
||||
WriteGitHubOutput("deleted_packages", "0");
|
||||
WriteGitHubOutput("processed_names", "0");
|
||||
appLifetime.StopApplication();
|
||||
return;
|
||||
}
|
||||
|
||||
var totalDeleted = 0;
|
||||
|
||||
// Process each name separately: collect -> filter -> delete
|
||||
foreach (var name in names)
|
||||
{
|
||||
logger.LogInformation("Processing packages for name '{Name}'", name);
|
||||
|
||||
|
||||
var packages = (await giteaPackageService.GetPackagesByNameAsync(name, cancellationToken)).ToList();
|
||||
logger.LogInformation("Found {Count} packages for name '{Name}'", packages.Count, name);
|
||||
|
||||
|
||||
var packagesToDelete = packageService.FilterPackagesToDelete(packages);
|
||||
logger.LogInformation("Found {Count} packages to delete for name '{Name}'", packagesToDelete.Count, name);
|
||||
|
||||
var deletedCount = await DeletePackages(packagesToDelete, cancellationToken);
|
||||
totalDeleted += deletedCount;
|
||||
logger.LogInformation("Deleted {Count} packages for name '{Name}'", deletedCount, name);
|
||||
|
||||
logger.LogInformation("Found {Count} packages to delete for name '{Name}'", packagesToDelete.Count,
|
||||
name);
|
||||
|
||||
await DeletePackages(packagesToDelete, cancellationToken);
|
||||
logger.LogInformation("Deleted {Count} packages for name '{Name}'", packagesToDelete.Count, name);
|
||||
|
||||
logger.LogInformation("Cleanup finished for name '{Name}'", name);
|
||||
}
|
||||
|
||||
|
||||
logger.LogInformation("All package names processed successfully");
|
||||
|
||||
// Write outputs to GITHUB_OUTPUT
|
||||
WriteGitHubOutput("deleted_packages", totalDeleted.ToString());
|
||||
WriteGitHubOutput("processed_names", names.Length.ToString());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Cleanup failed with an error");
|
||||
WriteGitHubOutput("deleted_packages", "0");
|
||||
WriteGitHubOutput("processed_names", "0");
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user