Skip to content

Commit

Permalink
add file hash to artifact metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
zuzuleinen committed Dec 9, 2022
1 parent d9d9ffc commit 3fbc230
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
13 changes: 12 additions & 1 deletion bobtask/artifact_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"time"

"github.com/benchkram/bob/pkg/filehash"
"github.com/benchkram/errz"
"github.com/mholt/archiver/v3"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -76,13 +77,14 @@ func (t *Task) ArtifactCreate(artifactName hash.In) (err error) {
boblog.Log.V(3).Info(fmt.Sprintf("[task:%s] file in buildinfo %d", t.name, len(buildInfo.Filesystem.Files)))

// targets filesystem
var files []File
for fname := range buildInfo.Filesystem.Files {
info, err := os.Lstat(fname)
errz.Fatal(err)

// trim the tasks directory from the internal name
internalName := strings.TrimPrefix(fname, t.dir)
// saved docker images are temporarly stored in the tmp dir,
// saved docker images are temporarily stored in the tmp dir,
// this assures it's not added as prefix.
internalName = strings.TrimPrefix(internalName, os.TempDir())
internalName = strings.TrimPrefix(internalName, tempdir)
Expand Down Expand Up @@ -111,6 +113,14 @@ func (t *Task) ArtifactCreate(artifactName hash.In) (err error) {
})
errz.Fatal(err)

contentHash, err := filehash.HashOfFile(fname)
errz.Fatal(err)

files = append(files, File{
Path: fname,
Hash: contentHash,
})

err = file.Close()
errz.Fatal(err)
}
Expand Down Expand Up @@ -159,6 +169,7 @@ func (t *Task) ArtifactCreate(artifactName hash.In) (err error) {
metadata.Taskname = t.name
metadata.Project = t.Project()
metadata.InputHash = artifactName.String()
metadata.Files = files
bin, err := yaml.Marshal(metadata)
errz.Fatal(err)

Expand Down
10 changes: 9 additions & 1 deletion bobtask/artifact_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
)

type ArtifactMetadata struct {

// Project is the project in which the task is defined
Project string `yaml:"project,omitempty"`

Expand All @@ -17,11 +16,20 @@ type ArtifactMetadata struct {

// CreatedAt timestamp the artifact was created
CreatedAt time.Time `yaml:"created_at,omitempty"`

// Files of the target
Files []File `yaml:"files"`
}

type File struct {
Path string `yaml:"path"`
Hash string `yaml:"hash"`
}

func NewArtifactMetadata() *ArtifactMetadata {
am := &ArtifactMetadata{
CreatedAt: time.Now(),
Files: make([]File, 0),
}
return am
}

0 comments on commit 3fbc230

Please sign in to comment.