woodpecker/pipeline/frontend/yaml/compiler/cacher.go
John Olheiser 71b9179078
Move plugin config to root.pipeline.[step].settings (#464)
* Move Vargs to Settings

* Allow both until deprecation

* Add more tests

* Adjust schema

* Add missing detach option, fix schema tests


Signed-off-by: jolheiser <john.olheiser@gmail.com>
Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: Anbraten <anton@ju60.de>
2021-12-04 16:44:18 +01:00

104 lines
2.4 KiB
Go

package compiler
import (
"path"
"strings"
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml"
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/types"
)
// Cacher defines a compiler transform that can be used
// to implement default caching for a repository.
type Cacher interface {
Restore(repo, branch string, mounts []string) *yaml.Container
Rebuild(repo, branch string, mounts []string) *yaml.Container
}
type volumeCacher struct {
base string
}
func (c *volumeCacher) Restore(repo, branch string, mounts []string) *yaml.Container {
return &yaml.Container{
Name: "rebuild_cache",
Image: "plugins/volume-cache:1.0.0",
Settings: map[string]interface{}{
"mount": mounts,
"path": "/cache",
"restore": true,
"file": strings.Replace(branch, "/", "_", -1) + ".tar",
"fallback_to": "master.tar",
},
Volumes: types.Volumes{
Volumes: []*types.Volume{
{
Source: path.Join(c.base, repo),
Destination: "/cache",
// TODO add access mode
},
},
},
}
}
func (c *volumeCacher) Rebuild(repo, branch string, mounts []string) *yaml.Container {
return &yaml.Container{
Name: "rebuild_cache",
Image: "plugins/volume-cache:1.0.0",
Settings: map[string]interface{}{
"mount": mounts,
"path": "/cache",
"rebuild": true,
"flush": true,
"file": strings.Replace(branch, "/", "_", -1) + ".tar",
},
Volumes: types.Volumes{
Volumes: []*types.Volume{
{
Source: path.Join(c.base, repo),
Destination: "/cache",
// TODO add access mode
},
},
},
}
}
type s3Cacher struct {
bucket string
access string
secret string
region string
}
func (c *s3Cacher) Restore(repo, branch string, mounts []string) *yaml.Container {
return &yaml.Container{
Name: "rebuild_cache",
Image: "plugins/s3-cache:latest",
Settings: map[string]interface{}{
"mount": mounts,
"access_key": c.access,
"secret_key": c.secret,
"bucket": c.bucket,
"region": c.region,
"rebuild": true,
},
}
}
func (c *s3Cacher) Rebuild(repo, branch string, mounts []string) *yaml.Container {
return &yaml.Container{
Name: "rebuild_cache",
Image: "plugins/s3-cache:latest",
Settings: map[string]interface{}{
"mount": mounts,
"access_key": c.access,
"secret_key": c.secret,
"bucket": c.bucket,
"region": c.region,
"rebuild": true,
"flush": true,
},
}
}