From 7d841fadb37afda8a5577c8c21e973c5c9f837b6 Mon Sep 17 00:00:00 2001 From: Pau Rodriguez-Estivill Date: Sun, 7 Mar 2021 14:39:40 +0100 Subject: [PATCH] Add local revision tag --- BUILD.md | 8 +++++- docker-bake.hcl | 66 ++++++++++++++++++++++++++++++++++++++++--------- generate.sh | 26 ++++++++++++++++--- publish.sh | 9 ++++--- 4 files changed, 89 insertions(+), 20 deletions(-) diff --git a/BUILD.md b/BUILD.md index 9c569f7..5a162b9 100644 --- a/BUILD.md +++ b/BUILD.md @@ -39,5 +39,11 @@ docker buildx bake --pull -f config.hcl In order to publish directly to the repository run this command instead: ```sh -docker buildx bake --pull --set common.output=type=registry -f config.hcl +docker buildx bake --pull --push -f config.hcl +``` + +Also, optionally, it can also generate build revision tags from last git commit (like `./publish.sh` script does): + +```sh +BUILDREV=$(git rev-parse --short HEAD) docker buildx bake --pull --push -f config.hcl ``` diff --git a/docker-bake.hcl b/docker-bake.hcl index 5a706b4..c6e99a1 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -2,6 +2,10 @@ group "default" { targets = ["debian-latest", "alpine-latest", "debian-12", "debian-11", "debian-10", "debian-9.6", "debian-9.5", "alpine-12", "alpine-11", "alpine-10", "alpine-9.6", "alpine-9.5"] } +variable "BUILDREV" { + default = "" +} + target "common" { platforms = ["linux/amd64", "linux/arm64", "linux/arm/v7"] args = {"GOCRONVER" = "v0.0.9"} @@ -20,71 +24,109 @@ target "alpine" { target "debian-latest" { inherits = ["debian"] args = {"BASETAG" = "13"} - tags = ["prodrigestivill/postgres-backup-local:latest", "prodrigestivill/postgres-backup-local:13"] + tags = [ + "prodrigestivill/postgres-backup-local:latest", + "prodrigestivill/postgres-backup-local:13", + notequal("", BUILDREV) ? "prodrigestivill/postgres-backup-local:13-debian-${BUILDREV}" : "" + ] } target "alpine-latest" { inherits = ["alpine"] args = {"BASETAG" = "13-alpine"} - tags = ["prodrigestivill/postgres-backup-local:alpine", "prodrigestivill/postgres-backup-local:13-alpine"] + tags = [ + "prodrigestivill/postgres-backup-local:alpine", + "prodrigestivill/postgres-backup-local:13-alpine", + notequal("", BUILDREV) ? "prodrigestivill/postgres-backup-local:13-alpine-${BUILDREV}" : "" + ] } target "debian-12" { inherits = ["debian"] args = {"BASETAG" = "12"} - tags = ["prodrigestivill/postgres-backup-local:12"] + tags = [ + "prodrigestivill/postgres-backup-local:12", + notequal("", BUILDREV) ? "prodrigestivill/postgres-backup-local:12-debian-${BUILDREV}" : "" + ] } target "alpine-12" { inherits = ["alpine"] args = {"BASETAG" = "12-alpine"} - tags = ["prodrigestivill/postgres-backup-local:12-alpine"] + tags = [ + "prodrigestivill/postgres-backup-local:12-alpine", + notequal("", BUILDREV) ? "prodrigestivill/postgres-backup-local:12-alpine-${BUILDREV}" : "" + ] } target "debian-11" { inherits = ["debian"] args = {"BASETAG" = "11"} - tags = ["prodrigestivill/postgres-backup-local:11"] + tags = [ + "prodrigestivill/postgres-backup-local:11", + notequal("", BUILDREV) ? "prodrigestivill/postgres-backup-local:11-debian-${BUILDREV}" : "" + ] } target "alpine-11" { inherits = ["alpine"] args = {"BASETAG" = "11-alpine"} - tags = ["prodrigestivill/postgres-backup-local:11-alpine"] + tags = [ + "prodrigestivill/postgres-backup-local:11-alpine", + notequal("", BUILDREV) ? "prodrigestivill/postgres-backup-local:11-alpine-${BUILDREV}" : "" + ] } target "debian-10" { inherits = ["debian"] args = {"BASETAG" = "10"} - tags = ["prodrigestivill/postgres-backup-local:10"] + tags = [ + "prodrigestivill/postgres-backup-local:10", + notequal("", BUILDREV) ? "prodrigestivill/postgres-backup-local:10-debian-${BUILDREV}" : "" + ] } target "alpine-10" { inherits = ["alpine"] args = {"BASETAG" = "10-alpine"} - tags = ["prodrigestivill/postgres-backup-local:10-alpine"] + tags = [ + "prodrigestivill/postgres-backup-local:10-alpine", + notequal("", BUILDREV) ? "prodrigestivill/postgres-backup-local:10-alpine-${BUILDREV}" : "" + ] } target "debian-9.6" { inherits = ["debian"] args = {"BASETAG" = "9.6"} - tags = ["prodrigestivill/postgres-backup-local:9.6"] + tags = [ + "prodrigestivill/postgres-backup-local:9.6", + notequal("", BUILDREV) ? "prodrigestivill/postgres-backup-local:9.6-debian-${BUILDREV}" : "" + ] } target "alpine-9.6" { inherits = ["alpine"] args = {"BASETAG" = "9.6-alpine"} - tags = ["prodrigestivill/postgres-backup-local:9.6-alpine"] + tags = [ + "prodrigestivill/postgres-backup-local:9.6-alpine", + notequal("", BUILDREV) ? "prodrigestivill/postgres-backup-local:9.6-alpine-${BUILDREV}" : "" + ] } target "debian-9.5" { inherits = ["debian"] args = {"BASETAG" = "9.5"} - tags = ["prodrigestivill/postgres-backup-local:9.5"] + tags = [ + "prodrigestivill/postgres-backup-local:9.5", + notequal("", BUILDREV) ? "prodrigestivill/postgres-backup-local:9.5-debian-${BUILDREV}" : "" + ] } target "alpine-9.5" { inherits = ["alpine"] args = {"BASETAG" = "9.5-alpine"} - tags = ["prodrigestivill/postgres-backup-local:9.5-alpine"] + tags = [ + "prodrigestivill/postgres-backup-local:9.5-alpine", + notequal("", BUILDREV) ? "prodrigestivill/postgres-backup-local:9.5-alpine-${BUILDREV}" : "" + ] } diff --git a/generate.sh b/generate.sh index 39cd1bb..df2bfa0 100755 --- a/generate.sh +++ b/generate.sh @@ -21,6 +21,10 @@ group "default" { targets = [$T] } +variable "BUILDREV" { + default = "" +} + target "common" { platforms = [$P] args = {"GOCRONVER" = "$GOCRONVER"} @@ -39,13 +43,21 @@ target "alpine" { target "debian-latest" { inherits = ["debian"] args = {"BASETAG" = "$MAIN_TAG"} - tags = ["$IMAGE_NAME:latest", "$IMAGE_NAME:$MAIN_TAG"] + tags = [ + "$IMAGE_NAME:latest", + "$IMAGE_NAME:$MAIN_TAG", + notequal("", BUILDREV) ? "$IMAGE_NAME:$MAIN_TAG-debian-\${BUILDREV}" : "" + ] } target "alpine-latest" { inherits = ["alpine"] args = {"BASETAG" = "$MAIN_TAG-alpine"} - tags = ["$IMAGE_NAME:alpine", "$IMAGE_NAME:$MAIN_TAG-alpine"] + tags = [ + "$IMAGE_NAME:alpine", + "$IMAGE_NAME:$MAIN_TAG-alpine", + notequal("", BUILDREV) ? "$IMAGE_NAME:$MAIN_TAG-alpine-\${BUILDREV}" : "" + ] } EOF @@ -54,13 +66,19 @@ for TAG in $TAGS_EXTRA; do cat >> "$DOCKER_BAKE_FILE" << EOF target "debian-$TAG" { inherits = ["debian"] args = {"BASETAG" = "$TAG"} - tags = ["$IMAGE_NAME:$TAG"] + tags = [ + "$IMAGE_NAME:$TAG", + notequal("", BUILDREV) ? "$IMAGE_NAME:$TAG-debian-\${BUILDREV}" : "" + ] } target "alpine-$TAG" { inherits = ["alpine"] args = {"BASETAG" = "$TAG-alpine"} - tags = ["$IMAGE_NAME:$TAG-alpine"] + tags = [ + "$IMAGE_NAME:$TAG-alpine", + notequal("", BUILDREV) ? "$IMAGE_NAME:$TAG-alpine-\${BUILDREV}" : "" + ] } EOF done diff --git a/publish.sh b/publish.sh index 3930e57..b9a4d4d 100755 --- a/publish.sh +++ b/publish.sh @@ -9,13 +9,16 @@ TMPFILE=$(mktemp) trap 'rm -vf "$TMPFILE"' EXIT if [ -n "$@" ]; then - export TAGS="$@" + TAGS="$@" echo "Generate configuration for only this tags: $TAGS" + export TAGS else echo "Generate configuration for all predefined tags." fi ./generate.sh "$TMPFILE" echo "Generated docker bake HCL script at: $TMPFILE" -echo "Starting building and publish..." -docker buildx bake --pull --set common.output=type=registry -f "$TMPFILE" +BUILDREV=$(git rev-parse --short HEAD) +echo "Starting building and publish revision $BUILDREV..." +export BUILDREV +docker buildx bake --pull --push -f "$TMPFILE"