-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci-tools: Add rtool subcommand to download artifact
This is useful to keep the runner images up-to-date.
- Loading branch information
Showing
2 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Licensed under the Apache-2.0 license | ||
|
||
package runner | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
"log" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/google/go-github/v53/github" | ||
) | ||
|
||
func findArtifact(artifacts *github.ArtifactList, name string) (*github.Artifact, error) { | ||
for _, artifact := range artifacts.Artifacts { | ||
if artifact.GetName() == name { | ||
return artifact, nil | ||
} | ||
} | ||
return nil, errors.New("could not find artifact") | ||
} | ||
|
||
func DownloadArtifact(ctx context.Context, client *github.Client, workflowFilename string, artifactName string) error { | ||
repo := "caliptra-sw" | ||
workflow, _, err := client.Actions.GetWorkflowByFileName(ctx, githubOrg, repo, workflowFilename) | ||
if err != nil { | ||
return err | ||
} | ||
runs, _, err := client.Actions.ListWorkflowRunsByID(ctx, githubOrg, repo, workflow.GetID(), &github.ListWorkflowRunsOptions{ | ||
Branch: "main", | ||
Status: "completed", | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
if len(runs.WorkflowRuns) == 0 { | ||
return errors.New("no workflow runs") | ||
} | ||
artifacts, _, err := client.Actions.ListWorkflowRunArtifacts(ctx, githubOrg, repo, runs.WorkflowRuns[0].GetID(), &github.ListOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
artifact, err := findArtifact(artifacts, artifactName) | ||
if err != nil { | ||
return err | ||
} | ||
url, _, err := client.Actions.DownloadArtifact(ctx, githubOrg, repo, artifact.GetID(), true) | ||
if err != nil { | ||
return err | ||
} | ||
resp, err := http.Get(url.String()) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode != http.StatusOK { | ||
body, _ := io.ReadAll(resp.Body) | ||
log.Fatalf("Response failed with status code: %d and body %s\n", resp.StatusCode, body) | ||
} | ||
_, err = io.Copy(os.Stdout, resp.Body) | ||
if err != nil { | ||
return errors.New("unable to copy response body") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters