wip
This commit is contained in:
parent
ab5ca1717b
commit
046e7995ab
2 changed files with 370 additions and 7 deletions
144
save/github.go
144
save/github.go
|
|
@ -1,144 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ActionClient struct {
|
||||
*http.Client
|
||||
base string
|
||||
token string
|
||||
}
|
||||
|
||||
func NewActionClient() *ActionClient {
|
||||
c := &ActionClient{Client: &http.Client{}}
|
||||
c.base = os.Getenv("ACTIONS_RESULTS_URL")
|
||||
c.token = fmt.Sprintf("Bearer %s", os.Getenv("ACTIONS_RUNTIME_TOKEN"))
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *ActionClient) Do(req *http.Request) (*http.Response, error) {
|
||||
req.Header.Set("Authorization", c.token)
|
||||
if !req.URL.IsAbs() {
|
||||
u, err := url.Parse(fmt.Sprintf("%s%s", strings.TrimRight(c.base, "/"), req.URL))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.URL = u
|
||||
}
|
||||
return c.Client.Do(req)
|
||||
}
|
||||
|
||||
func HasContentType(r *http.Response, mimetype string) bool {
|
||||
contentType := r.Header.Get("Content-type")
|
||||
if contentType == "" {
|
||||
return mimetype == "application/octet-stream"
|
||||
}
|
||||
|
||||
for _, v := range strings.Split(contentType, ",") {
|
||||
t, _, err := mime.ParseMediaType(v)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
if t == mimetype {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type ApiClient interface {
|
||||
Do(*http.Request) (*http.Response, error)
|
||||
}
|
||||
|
||||
type GitHub struct {
|
||||
client ApiClient
|
||||
}
|
||||
|
||||
type CreateArtifactRequest struct {
|
||||
RunID string `json:"workflowRunBackendId"`
|
||||
JobRunID string `json:"workflowJobRunBackendId"`
|
||||
Name string `json:"name"`
|
||||
Version int32 `json:"version"`
|
||||
|
||||
ExpiresAt *time.Time `json:"expiresAt,omitempty"`
|
||||
}
|
||||
|
||||
type CreateArtifactResponse struct {
|
||||
Ok bool `json:"ok"`
|
||||
SignedUploadUrl string `json:"signed_upload_url"`
|
||||
}
|
||||
|
||||
type FinalizeArtifactRequest struct {
|
||||
RunID string `json:"workflowRunBackendId"`
|
||||
JobRunID string `json:"workflowJobRunBackendId"`
|
||||
Name string `json:"name"`
|
||||
Size int `json:"size"`
|
||||
Hash string `json:"hash,omitempty"`
|
||||
}
|
||||
|
||||
type FinalizeArtifactResponse struct {
|
||||
Ok bool `json:"ok"`
|
||||
ArtifactId string `json:"artifactId"`
|
||||
}
|
||||
|
||||
func (gh *GitHub) CreateArtifact(ctx context.Context, req CreateArtifactRequest) (*CreateArtifactResponse, error) {
|
||||
endpoint := "/twirp/github.actions.results.api.v1.ArtifactService/CreateArtifact"
|
||||
var result CreateArtifactResponse
|
||||
if err := gh.twirp(ctx, endpoint, req, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (gh *GitHub) FinalizeArtifact(ctx context.Context, req FinalizeArtifactRequest) (*FinalizeArtifactResponse, error) {
|
||||
endpoint := "/twirp/github.actions.results.api.v1.ArtifactService/FinalizeArtifact"
|
||||
var result FinalizeArtifactResponse
|
||||
if err := gh.twirp(ctx, endpoint, req, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (gh *GitHub) twirp(ctx context.Context, url string, in any, out any) error {
|
||||
var b bytes.Buffer
|
||||
|
||||
if err := json.NewEncoder(&b).Encode(in); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, &b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
res, err := gh.client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if !HasContentType(res, "application/json") {
|
||||
return fmt.Errorf("content-type: %q should be %q",
|
||||
res.Header.Get("content-type"), "application/json")
|
||||
}
|
||||
dec := json.NewDecoder(io.LimitReader(res.Body, 32*1024))
|
||||
|
||||
if res.StatusCode >= 200 && res.StatusCode < 300 {
|
||||
return dec.Decode(out)
|
||||
} else {
|
||||
body, _ := io.ReadAll(res.Body)
|
||||
return fmt.Errorf("twirp call %s failed: %d: %s", url, res.StatusCode, string(body))
|
||||
}
|
||||
}
|
||||
65
save/main.go
65
save/main.go
|
|
@ -1,11 +1,18 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"code.geekeey.de/actions/artifacts"
|
||||
"code.geekeey.de/actions/sdk"
|
||||
"code.geekeey.de/actions/sdk/iox"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
|
@ -20,9 +27,7 @@ func main() {
|
|||
|
||||
type SaveArtifactAction struct {
|
||||
*sdk.Action
|
||||
repository string
|
||||
path string
|
||||
reference string
|
||||
name string
|
||||
}
|
||||
|
||||
func (action *SaveArtifactAction) setup() error {
|
||||
|
|
@ -33,5 +38,59 @@ func (action *SaveArtifactAction) Run(ctx context.Context) error {
|
|||
if err := action.setup(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
run, job, err := artifacts.JobInfo()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to get job info: %v", err)
|
||||
}
|
||||
|
||||
github := artifacts.NewGitHub(artifacts.NewActionClient())
|
||||
|
||||
expire := time.Now().Add(20 * time.Hour)
|
||||
create, err := github.CreateArtifact(ctx, artifacts.CreateArtifactRequest{
|
||||
RunID: run,
|
||||
JobRunID: job,
|
||||
Name: action.name,
|
||||
Version: 4,
|
||||
ExpiresAt: &expire,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot create artifact %w", err)
|
||||
}
|
||||
if !create.Ok {
|
||||
return fmt.Errorf("cannot get pre-signed URL")
|
||||
}
|
||||
|
||||
rd, err := action.CreateArtifact(ctx)
|
||||
|
||||
res, err := artifacts.PushBlob_(ctx, rd, create.SignedUploadUrl, 1024*1024*1, runtime.NumCPU())
|
||||
|
||||
finish, err := github.FinalizeArtifact(ctx, artifacts.FinalizeArtifactRequest{
|
||||
RunID: run,
|
||||
JobRunID: job,
|
||||
Name: action.name,
|
||||
Size: res.Size,
|
||||
Hash: res.SHA256Sum,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot finish artifact %w", err)
|
||||
}
|
||||
if !create.Ok {
|
||||
return fmt.Errorf("cannot finish artifact upload")
|
||||
}
|
||||
|
||||
action.Noticef("created artifact: %s", finish.ArtifactId)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (action *SaveArtifactAction) CreateArtifact(ctx context.Context) (io.Reader, error) {
|
||||
rd, wr, err := os.Pipe()
|
||||
go func() {
|
||||
defer wr.Close()
|
||||
|
||||
t := tar.NewWriter(wr)
|
||||
t.AddFS(iox.NewGlobFS(os.DirFS(""), ""))
|
||||
}()
|
||||
return rd, err
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue