2017-06-22 19:06:28 +00:00
|
|
|
package compiler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml"
|
2021-10-30 15:52:02 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/types"
|
2017-06-22 19:06:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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",
|
2021-12-04 15:44:18 +00:00
|
|
|
Settings: map[string]interface{}{
|
2017-06-22 19:06:28 +00:00
|
|
|
"mount": mounts,
|
|
|
|
"path": "/cache",
|
|
|
|
"restore": true,
|
|
|
|
"file": strings.Replace(branch, "/", "_", -1) + ".tar",
|
|
|
|
"fallback_to": "master.tar",
|
|
|
|
},
|
2021-10-30 15:52:02 +00:00
|
|
|
Volumes: types.Volumes{
|
|
|
|
Volumes: []*types.Volume{
|
2017-06-22 19:06:28 +00:00
|
|
|
{
|
|
|
|
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",
|
2021-12-04 15:44:18 +00:00
|
|
|
Settings: map[string]interface{}{
|
2017-06-22 19:06:28 +00:00
|
|
|
"mount": mounts,
|
|
|
|
"path": "/cache",
|
|
|
|
"rebuild": true,
|
|
|
|
"flush": true,
|
|
|
|
"file": strings.Replace(branch, "/", "_", -1) + ".tar",
|
|
|
|
},
|
2021-10-30 15:52:02 +00:00
|
|
|
Volumes: types.Volumes{
|
|
|
|
Volumes: []*types.Volume{
|
2017-06-22 19:06:28 +00:00
|
|
|
{
|
|
|
|
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",
|
2021-12-04 15:44:18 +00:00
|
|
|
Settings: map[string]interface{}{
|
2017-06-22 19:06:28 +00:00
|
|
|
"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",
|
2021-12-04 15:44:18 +00:00
|
|
|
Settings: map[string]interface{}{
|
2017-06-22 19:06:28 +00:00
|
|
|
"mount": mounts,
|
|
|
|
"access_key": c.access,
|
|
|
|
"secret_key": c.secret,
|
|
|
|
"bucket": c.bucket,
|
|
|
|
"region": c.region,
|
|
|
|
"rebuild": true,
|
|
|
|
"flush": true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|