fix: ensure we push and cleanup the correct images

This commit is contained in:
Jose Diaz-Gonzalez
2021-08-05 01:13:36 -04:00
parent 39af6359be
commit d727dd76ce
3 changed files with 30 additions and 15 deletions

View File

@@ -49,17 +49,16 @@ func incrementTagVersion(appName string) (int, error) {
return version, nil
}
func pushToRegistry(appName string, tag int) error {
func pushToRegistry(appName string, tag int, imageID string, imageRepo string) error {
common.LogVerboseQuiet("Retrieving image info for app")
registryServer := getRegistryServerForApp(appName)
imageRepo := reportComputedImageRepo(appName)
imageTag, _ := common.GetRunningImageTag(appName)
image := common.GetAppImageName(appName, imageTag, "")
imageID, _ := common.DockerInspect(image, "{{ .Id }}")
common.LogVerboseQuiet(fmt.Sprintf("Tagging $IMAGE_REPO:%d in registry format", tag))
if !dockerTag(imageID, fmt.Sprintf("%s%s:%d", registryServer, imageRepo, tag)) {
fullImage := fmt.Sprintf("%s%s:%d", registryServer, imageRepo, tag)
common.LogVerboseQuiet(fmt.Sprintf("Tagging %s:%d in registry format", imageRepo, tag))
if !dockerTag(imageID, fullImage) {
// TODO: better error
return errors.New("Unable to tag image")
}
@@ -73,16 +72,17 @@ func pushToRegistry(appName string, tag int) error {
// This is only really important for registries that do not support creation on push
// Examples include AWS and Quay.io
common.LogVerboseQuiet("Pushing $IMAGE_REPO:$TAG")
if !dockerPush(fmt.Sprintf("%s%s:%d", registryServer, imageRepo, tag)) {
common.LogVerboseQuiet(fmt.Sprintf("Pushing %s", fullImage))
if !dockerPush(fullImage) {
// TODO: better error
return errors.New("Unable to push image")
}
common.LogVerboseQuiet("Cleaning up")
imageCleanup(appName, registryServer, imageRepo, imageTag, tag)
imageCleanup(appName, fmt.Sprintf("%s%s", registryServer, imageRepo), imageTag, tag)
imageCleanup(appName, imageRepo, imageTag, tag)
common.LogVerboseQuiet("Image $IMAGE_REPO:$TAG pushed")
common.LogVerboseQuiet(fmt.Sprintf("Image %s pushed", fullImage))
return nil
}
@@ -108,14 +108,14 @@ func dockerPush(imageTag string) bool {
return true
}
func imageCleanup(appName string, registryServer string, imageRepo string, imageTag string, tag int) {
func imageCleanup(appName string, imageRepo string, imageTag string, tag int) {
// # keep last two images in place
oldTag := tag - 1
tenImagesAgoTag := tag - 12
imagesToRemove := []string{}
for oldTag > 0 {
imagesToRemove = append(imagesToRemove, fmt.Sprintf("%s%s:%d", registryServer, imageRepo, oldTag))
common.LogInfo1(fmt.Sprintf("Removing image: %s:%d", imageRepo, oldTag))
imagesToRemove = append(imagesToRemove, fmt.Sprintf("%s:%d", imageRepo, oldTag))
oldTag = oldTag - 1
if tenImagesAgoTag == oldTag {

View File

@@ -34,7 +34,8 @@ func main() {
err = registry.TriggerPostDelete(appName)
case "post-release-builder":
appName := flag.Arg(1)
err = registry.TriggerPostReleaseBuilder(appName)
image := flag.Arg(2)
err = registry.TriggerPostReleaseBuilder(appName, image)
case "report":
appName := flag.Arg(0)
err = registry.ReportSingleApp(appName, "", "")

View File

@@ -1,7 +1,9 @@
package registry
import (
"errors"
"fmt"
"strings"
"github.com/dokku/dokku/plugins/common"
)
@@ -53,7 +55,19 @@ func TriggerPostDelete(appName string) error {
}
// TriggerPostReleaseBuilder pushes the image to the remote registry
func TriggerPostReleaseBuilder(appName string) error {
func TriggerPostReleaseBuilder(appName string, image string) error {
imageID, _ := common.DockerInspect(image, "{{ .Id }}")
imageRepo := common.GetAppImageRepo(appName)
computedImageRepo := reportComputedImageRepo(appName)
newImage := strings.Replace(image, imageRepo+":", computedImageRepo+":", 1)
if computedImageRepo != imageRepo {
if !dockerTag(imageID, newImage) {
// TODO: better error
return errors.New("Unable to tag image")
}
}
if !isPushEnabled(appName) {
return nil
}
@@ -64,5 +78,5 @@ func TriggerPostReleaseBuilder(appName string) error {
return err
}
return pushToRegistry(appName, imageTag)
return pushToRegistry(appName, imageTag, imageID, computedImageRepo)
}