Updated download previous artifacts script.

This commit is contained in:
2025-07-10 13:08:06 -07:00
parent 2ab54559bd
commit 9f43626e25
6 changed files with 173 additions and 164 deletions

View File

@@ -1,69 +0,0 @@
name: github-download-previous-artifacts
description: "Download artifacts from a previous Workflow run."
inputs:
host:
description: "Gitea host to query."
required: true
default: "${{ github.server_url }}"
username:
description: "Gitea user to query with."
required: true
default: "${{ github.repository_owner }}"
password:
description: "Credentials to use for Gitea. If not specified, the GitHub/Gitea token will be used."
required: true
default: "${{ github.token }}"
accessToken:
description: "Access token for access to other repositories."
required: true
default: ""
workflowPattern:
description: "Pattern of the workflow name to match."
required: true
artifactPattern:
description: "Pattern of the artifacts to match."
required: true
default: "*"
artifactName:
description: "Name of the artifact when it is downloaded. If not specified, the artifact will be named what the artifact is named."
required: false
default: ""
unzipDir:
description: "Directory to unzip the artifacts into. If not specified, the artifacts will not be unzipped."
required: false
default: ""
nugetSources:
description: "List of additional NuGet sources to use."
required: false
default: "https://gitea.studiowhy.net/api/packages/FORK/nuget/index.json"
nugetUsernames:
description: "List of additional NuGet usernames to use."
required: false
default: "${{ github.actor }}"
nugetPasswords:
description: "List of additional NuGet passwords to use."
required: false
default: "${{ github.token }}"
# outputs:
# query:
# description: "The query result."
# value: ${{ steps.query.outputs.console }}
runs:
using: "composite"
steps:
- run: |
ls -al "${{ github.action_path }}"
shell: bash
- name: "Download artifacts."
uses: act/common/dotnet/dotnet-10@master
env:
WORKFLOW_FILENAME: ${{ inputs.workflowPattern }}
ARTIFACT_NAME: ${{ inputs.filePattern }}
ARTIFACT_FILENAME: ${{ inputs.artifactName }}
UNZIP_DIR: ${{ inputs.unzipDir }}
INPUTS: ${{ toJSON(inputs) }}
with:
command: run "${{ github.action_path }}/download-previous-artifacts.cs"
nugetSources: ${{ inputs.nugetSources }}
nugetUsernames: ${{ inputs.nugetUsernames }}
nugetPasswords: ${{ inputs.nugetPasswords }}

View File

@@ -1,86 +0,0 @@
#!/usr/bin/env -S dotnet run
#:package StudioWhy.Gitea.Net.API@1.24.2.*
using System.Text.Json;
using System.Text;
using Gitea.Net.Api;
using Gitea.Net.Client;
using Gitea.Net.Extensions;
using Gitea.Net.Model;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
string jsonInput = Environment.GetEnvironmentVariable("INPUTS") ?? "{}";
const string name = "Download Previous Artifacts";
Console.WriteLine($"::group::{name} - Inputs");
Console.WriteLine(jsonInput);
Console.WriteLine("::endgroup::");
byte[] jsonBytes = Encoding.UTF8.GetBytes(jsonInput);
using MemoryStream memoryStream = new(jsonBytes);
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonStream(memoryStream)
.AddEnvironmentVariables()
.Build();
PrintConfiguration(configuration);
string host = configuration["Host"]!;
string username = configuration["Username"]!;
string password = configuration["Password"]!;
ServiceCollection services = new();
services.AddApi(o =>
{
BasicToken basicToken = new(username, password);
o.AddTokens(basicToken);
});
UriBuilder hostUriBuilder = new(host);
hostUriBuilder.Path = "/api/v1";
using HttpClient httpClient = new()
{
BaseAddress = hostUriBuilder.Uri,
};
services.AddLogging(configure => configure.AddConsole());
services.AddSingleton(httpClient);
services.AddSingleton<OrganizationApi>();
services.AddSingleton<RepositoryApi>();
ServiceProvider serviceProvider = services.BuildServiceProvider();
OrganizationApi organizationApi = serviceProvider.GetRequiredService<OrganizationApi>();
RepositoryApi repositoryApi = serviceProvider.GetRequiredService<RepositoryApi>();
IOrgGetAllApiResponse response = await organizationApi.OrgGetAllAsync();
if (response.TryOk(out List<Organization> organizations))
{
foreach (Organization org in organizations)
{
Console.WriteLine($"Organization: {org.Name}");
}
//Console.WriteLine($"Repository: {repo?.Name}");
}
// string owner = configuration["GITHUB_REPOSITORY_OWNER"]!;
// string repoName = configuration["GITHUB_REPOSITORY"]!;
// repoName = Path.GetFileName(repoName);
// Repository repo = await repoApi.RepoGetAsync(owner, repoName);
// Console.WriteLine($"Repository:\n {JsonSerializer.Serialize(repo)}");
static void PrintConfiguration(IConfiguration configuration)
{
foreach (var kvp in configuration.AsEnumerable())
{
Console.WriteLine($"{kvp.Key}: {kvp.Value}");
}
}

View File

@@ -1,67 +0,0 @@
module.exports = async ({
github,
context,
core
}) => {
const owner = context.repo.owner;
const repo = context.repo.repo;
// See: https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#list-repository-workflows
const workflows = await github.rest.actions.listRepoWorkflows({
owner,
repo
})
const workflow = workflows.data.workflows.find(w => w.path.includes(process.env.WORKFLOW_FILENAME));
if (!workflow) {
core.setFailed("No matching workflow found.");
return;
}
// See: https://docs.github.com/en/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-repository
const runs = await github.rest.actions.listWorkflowRuns({
owner,
repo,
workflow_id: workflow.id,
status: "success",
per_page: 1
})
if (runs.data.total_count === 0) {
core.setFailed("No workflow runs found.");
return;
}
// See: https://docs.github.com/en/rest/actions/artifacts?apiVersion=2022-11-28#list-workflow-run-artifacts
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner,
repo,
run_id: runs.data.workflow_runs[0].id
});
// Other formats are not supported by the GitHub API for downloading artifacts.
const extension = "zip"
const artifact = artifacts.data.artifacts.find(artifact => artifact.name === process.env.ARTIFACT_NAME);
if (artifact) {
// See: https://docs.github.com/en/rest/actions/artifacts?apiVersion=2022-11-28#download-an-artifact
const response = await github.rest.actions.downloadArtifact({
owner,
repo,
artifact_id: artifact.id,
archive_format: extension
});
const artifactFilename = process.env.ARTIFACT_FILENAME || `${artifact.name}.${extension}`;
require('fs').writeFileSync(artifactFilename, Buffer.from(response.data));
console.log("Artifact downloaded successfully.");
const unzip_dir = process.env.UNZIP_DIR;
if (unzip_dir) {
console.log("Unzipping artifact to directory: %s", unzip_dir);
require('child_process').execSync(`unzip -o ${artifactFilename} -d ${process.env.UNZIP_DIR}`);
}
} else {
core.setFailed("No artifact found.");
}
}