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

Adds support for saving/restoring image layers from archive

This commit is contained in:
Jan Broer 2015-11-09 05:44:56 +01:00
parent dd43c76c88
commit fd95103a82
2 changed files with 81 additions and 0 deletions

24
DOCS.md
View File

@ -9,6 +9,9 @@ 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
* `file` - absolute or relative path to archive file
* `tag` - limit archiving to these tag(s) (optional)
The following is a sample Docker configuration in your .drone.yml file: The following is a sample Docker configuration in your .drone.yml file:
@ -53,6 +56,27 @@ publish:
Note that in the above example we quote the version numbers. If the yaml parser interprets the value as a number it will cause a parsing error. Note that in the above example we quote the version numbers. If the yaml parser interprets the value as a number it will cause a parsing error.
You may want to cache Docker image layers between builds to speed up the build process:
```
publish:
docker:
username: kevinbacon
password: $$DOCKER_PASSWORD
email: kevin.bacon@mail.com
repo: foo/bar
tag:
- latest
- "1.0.1"
archive:
file: docker/image.tar
tag: latest
cache:
mount:
- docker/image.tar
```
## Troubleshooting ## Troubleshooting
For detailed output you can set the `DOCKER_LAUNCH_DEBUG` environment variable in your plugin configuration. This starts Docker with verbose logging enabled. For detailed output you can set the `DOCKER_LAUNCH_DEBUG` environment variable in your plugin configuration. This starts Docker with verbose logging enabled.

57
main.go
View File

@ -5,12 +5,18 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"strings" "strings"
"time" "time"
"github.com/drone/drone-plugin-go/plugin" "github.com/drone/drone-plugin-go/plugin"
) )
type Archive struct {
File string `json:"file"`
Tag StrSlice `json:"tag"`
}
type Docker struct { type Docker struct {
Storage string `json:"storage_driver"` Storage string `json:"storage_driver"`
Registry string `json:"registry"` Registry string `json:"registry"`
@ -24,6 +30,7 @@ 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"`
} }
func main() { func main() {
@ -59,6 +66,12 @@ 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
if len(vargs.Archive.File) != 0 {
if ! filepath.IsAbs(vargs.Archive.File) {
vargs.Archive.File = filepath.Join(workspace.Path, vargs.Archive.File)
}
}
go func() { go func() {
args := []string{"-d"} args := []string{"-d"}
@ -125,6 +138,23 @@ func main() {
trace(cmd) trace(cmd)
cmd.Run() cmd.Run()
// Load archived image if exists
if len(vargs.Archive.File) != 0 {
if _, err := os.Stat(vargs.Archive.File); err != nil {
fmt.Printf("Archive %s does not exist. Building from scratch.\n", vargs.Archive.File)
} else {
cmd := exec.Command("/usr/bin/docker", "load", "-i", vargs.Archive.File)
cmd.Dir = workspace.Path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
trace(cmd)
err := cmd.Run()
if err != nil {
os.Exit(1)
}
}
}
// Build the container // Build the container
name := fmt.Sprintf("%s:%s", vargs.Repo, vargs.Tag.Slice()[0]) name := fmt.Sprintf("%s:%s", vargs.Repo, vargs.Tag.Slice()[0])
cmd = exec.Command("/usr/bin/docker", "build", "--pull=true", "--rm=true", "-f", vargs.File, "-t", name, vargs.Context) cmd = exec.Command("/usr/bin/docker", "build", "--pull=true", "--rm=true", "-f", vargs.File, "-t", name, vargs.Context)
@ -165,6 +195,33 @@ func main() {
} }
} }
// Save the image to the archive
if len(vargs.Archive.File) != 0 {
// if the path's directory does not exist, create it
dir := filepath.Dir(vargs.Archive.File)
os.MkdirAll(dir, 0755)
cmd = exec.Command("/usr/bin/docker", "save", "-o", vargs.Archive.File)
// Limit save command to the given tag(s)
if vargs.Archive.Tag.Len() != 0 {
for _, tag := range vargs.Archive.Tag.Slice() {
name_ := fmt.Sprintf("%s:%s", vargs.Repo, tag)
cmd.Args = append(cmd.Args, name_)
}
} else {
cmd.Args = append(cmd.Args, vargs.Repo)
}
cmd.Dir = workspace.Path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
trace(cmd)
err := cmd.Run()
if err != nil {
os.Exit(1)
}
}
} }
// Trace writes each command to standard error (preceded by a $ ) before it // Trace writes each command to standard error (preceded by a $ ) before it