0
0
mirror of https://github.com/thegeeklab/wp-docker-buildx.git synced 2024-11-22 20:20:39 +00:00

Merge pull request #14 from janeczku/cache-revamp

Separate 'archive' parameter in 'load' and 'save'
This commit is contained in:
Brad Rydzewski 2015-11-19 13:22:06 -08:00
commit f510688b48
2 changed files with 37 additions and 26 deletions

12
DOCS.md
View File

@ -9,9 +9,10 @@ The following parameters are used to configure this plugin:
* `tag` - repository tag for the image * `tag` - repository tag for the image
* `insecure` - enable insecure communication to this registry * `insecure` - enable insecure communication to this registry
* `storage_driver` - use `aufs`, `devicemapper`, `btrfs` or `overlay` driver * `storage_driver` - use `aufs`, `devicemapper`, `btrfs` or `overlay` driver
* `archive` - save and restore image layers to/from a tarred archive * `save` - save image layers to the specified tar file (see [docker save](https://docs.docker.com/engine/reference/commandline/save/))
* `file` - absolute or relative path to archive file * `file` - absolute / relative destination path
* `tag` - limit archiving to these tag(s) (optional) * `tags` - cherry-pick tags to save (optional)
* `load` - restore image layers from the specified tar file
The following is a sample Docker configuration in your .drone.yml file: The following is a sample Docker configuration in your .drone.yml file:
@ -68,9 +69,10 @@ publish:
tag: tag:
- latest - latest
- "1.0.1" - "1.0.1"
archive: load: docker/image.tar
save:
file: docker/image.tar file: docker/image.tar
tag: latest tags: latest
cache: cache:
mount: mount:

51
main.go
View File

@ -12,9 +12,11 @@ import (
"github.com/drone/drone-plugin-go/plugin" "github.com/drone/drone-plugin-go/plugin"
) )
type Archive struct { type Save struct {
File string `json:"file"` // Absolute or relative path
Tag StrSlice `json:"tag"` File string `json:"destination"`
// Only save specified tags (optional)
Tags StrSlice `json:"tag"`
} }
type Docker struct { type Docker struct {
@ -30,7 +32,8 @@ type Docker struct {
File string `json:"file"` File string `json:"file"`
Context string `json:"context"` Context string `json:"context"`
Dns []string `json:"dns"` Dns []string `json:"dns"`
Archive Archive `json:"archive"` Load string `json:"load"`
Save Save `json:"save"`
} }
func main() { func main() {
@ -66,10 +69,16 @@ func main() {
if vargs.Tag.Len() == 0 { if vargs.Tag.Len() == 0 {
vargs.Tag = StrSlice{[]string{"latest"}} vargs.Tag = StrSlice{[]string{"latest"}}
} }
// Archive file can be both a relative or absolute path // Get absolute path for 'save' file
if len(vargs.Archive.File) != 0 { if len(vargs.Save.File) != 0 {
if ! filepath.IsAbs(vargs.Archive.File) { if ! filepath.IsAbs(vargs.Save.File) {
vargs.Archive.File = filepath.Join(workspace.Path, vargs.Archive.File) vargs.Save.File = filepath.Join(workspace.Path, vargs.Save.File)
}
}
// Get absolute path for 'load' file
if len(vargs.Load) != 0 {
if ! filepath.IsAbs(vargs.Load) {
vargs.Load = filepath.Join(workspace.Path, vargs.Load)
} }
} }
@ -138,12 +147,12 @@ func main() {
trace(cmd) trace(cmd)
cmd.Run() cmd.Run()
// Load archived image if exists // Restore from tarred image repository
if len(vargs.Archive.File) != 0 { if len(vargs.Load) != 0 {
if _, err := os.Stat(vargs.Archive.File); err != nil { if _, err := os.Stat(vargs.Load); err != nil {
fmt.Printf("Archive %s does not exist. Building from scratch.\n", vargs.Archive.File) fmt.Printf("Archive %s does not exist. Building from scratch.\n", vargs.Load)
} else { } else {
cmd := exec.Command("/usr/bin/docker", "load", "-i", vargs.Archive.File) cmd := exec.Command("/usr/bin/docker", "load", "-i", vargs.Load)
cmd.Dir = workspace.Path cmd.Dir = workspace.Path
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
@ -195,17 +204,17 @@ func main() {
} }
} }
// Save the image to the archive // Save to tarred image repository
if len(vargs.Archive.File) != 0 { if len(vargs.Save.File) != 0 {
// if the path's directory does not exist, create it // if the destination directory does not exist, create it
dir := filepath.Dir(vargs.Archive.File) dir := filepath.Dir(vargs.Save.File)
os.MkdirAll(dir, 0755) os.MkdirAll(dir, 0755)
cmd = exec.Command("/usr/bin/docker", "save", "-o", vargs.Archive.File) cmd = exec.Command("/usr/bin/docker", "save", "-o", vargs.Save.File)
// Limit save command to the given tag(s) // Limit saving to the given tags
if vargs.Archive.Tag.Len() != 0 { if vargs.Save.Tags.Len() != 0 {
for _, tag := range vargs.Archive.Tag.Slice() { for _, tag := range vargs.Save.Tags.Slice() {
name_ := fmt.Sprintf("%s:%s", vargs.Repo, tag) name_ := fmt.Sprintf("%s:%s", vargs.Repo, tag)
cmd.Args = append(cmd.Args, name_) cmd.Args = append(cmd.Args, name_)
} }