142 lines
3.2 KiB
Go
142 lines
3.2 KiB
Go
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"archive/zip"
|
||
|
|
"compress/flate"
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"io"
|
||
|
|
"os"
|
||
|
|
"os/signal"
|
||
|
|
"runtime"
|
||
|
|
"strings"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"code.geekeey.de/actions/artifacts"
|
||
|
|
"code.geekeey.de/actions/sdk"
|
||
|
|
"code.geekeey.de/actions/sdk/glob"
|
||
|
|
)
|
||
|
|
|
||
|
|
func main() {
|
||
|
|
action := &pushArtifactAction{Action: sdk.New()}
|
||
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
||
|
|
defer cancel()
|
||
|
|
if err := action.Run(ctx); err != nil {
|
||
|
|
action.Errorf("%s", err)
|
||
|
|
os.Exit(1)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
type pushArtifactAction struct {
|
||
|
|
*sdk.Action
|
||
|
|
name string
|
||
|
|
path string
|
||
|
|
patterns []string
|
||
|
|
}
|
||
|
|
|
||
|
|
func (action *pushArtifactAction) setup() error {
|
||
|
|
action.name = action.GetInput("name")
|
||
|
|
if len(action.name) == 0 {
|
||
|
|
return fmt.Errorf("input 'name': is empty")
|
||
|
|
}
|
||
|
|
|
||
|
|
action.path = action.GetInput("path")
|
||
|
|
if len(action.path) == 0 {
|
||
|
|
return fmt.Errorf("input 'path': is empty")
|
||
|
|
}
|
||
|
|
|
||
|
|
patterns := strings.Split(action.GetInput("pattern"), "\n")
|
||
|
|
for _, pattern := range patterns {
|
||
|
|
if len(pattern) == 0 {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
action.patterns = append(action.patterns, pattern)
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (action *pushArtifactAction) 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(action.path, action.patterns, ctx)
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("cannot create archive: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
res, err := artifacts.PushBlob(ctx, rd, create.SignedUploadUrl, 1024*1024*1, runtime.NumCPU())
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("cannot upload artifact: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
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 *pushArtifactAction) CreateArtifact(path string, inputs []string, ctx context.Context) (io.Reader, error) {
|
||
|
|
if stat, err := os.Stat(path); err != nil || !stat.IsDir() {
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("cannot find directory: %w", err)
|
||
|
|
} else {
|
||
|
|
return nil, fmt.Errorf("source path is not a directory")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
gfs, err := glob.NewGlobFS(os.DirFS(path), inputs...)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("cannot apply glob patterns: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
rd, wr, err := os.Pipe()
|
||
|
|
go func() {
|
||
|
|
defer wr.Close()
|
||
|
|
|
||
|
|
a := zip.NewWriter(wr)
|
||
|
|
a.RegisterCompressor(zip.Deflate, func(w io.Writer) (io.WriteCloser, error) { return flate.NewWriter(w, flate.BestCompression) })
|
||
|
|
if err := a.AddFS(gfs); err != nil {
|
||
|
|
panic(err)
|
||
|
|
}
|
||
|
|
if err := a.Close(); err != nil {
|
||
|
|
panic(err)
|
||
|
|
}
|
||
|
|
}()
|
||
|
|
return rd, err
|
||
|
|
}
|