wip
All checks were successful
default / test artifact actions (push) Successful in 27s

This commit is contained in:
Louis Seubert 2026-09-13 17:31:36 +02:00
commit dfb5aa33dd
Signed by: louis9902
GPG key ID: 4B9DB28F826553BD
2 changed files with 831 additions and 608 deletions

33
push/action.yml Normal file
View file

@ -0,0 +1,33 @@
# SPDX-License-Identifier: EUPL-1.2
name: "Artifact Push"
description: "Upload files as a workflow artifact"
author: "Louis Seubert"
inputs:
name:
description: >
The name of the artifact to upload.
required: true
path:
description: >
The base directory the patterns are resolved against.
required: true
pattern:
description: >
Newline separated list of glob patterns selecting the files to include in
the artifact.
required: false
default: "**/*"
outputs:
artifact-id:
description: >
The ID of the created artifact.
artifact-url:
description: >
URL to download the created artifact.
artifact-digest:
description: >
SHA-256 digest of the created artifact.
runs:
using: "docker"
image: "../Dockerfile"
args: ["push"]

View file

@ -1,142 +0,0 @@
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
}