Skip to content

Commit

Permalink
Docker: only write to cache if creds available
Browse files Browse the repository at this point in the history
  • Loading branch information
brndnmtthws committed Jun 4, 2024
1 parent 0bc625f commit 8186547
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions .github/scripts/docker-build.bash
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#!/usr/bin/env bash
set -ex

DOCKERHUB_USERNAME="${DOCKERHUB_USERNAME:-conky}"
DOCKERHUB_IMAGE_ID=$DOCKERHUB_USERNAME/$IMAGE_NAME
DH_USERNAME="${DOCKERHUB_USERNAME:-conky}"
DOCKERHUB_IMAGE_ID=$DH_USERNAME/$IMAGE_NAME

# Change all uppercase to lowercase
DOCKERHUB_IMAGE_ID=$(echo $DOCKERHUB_IMAGE_ID | tr '[A-Z]' '[a-z]')

# Only build amd64 on PRs, build all platforms on main. The arm builds
# take far too long.
image_platforms="--platform linux/amd64"
push_image=""
cache_tag="pr-cache"

image_tags=()

# Strip git ref prefix from version
Expand All @@ -24,23 +30,24 @@ if [[ "$RELEASE" == ON ]]; then
image_tags+=("--tag" "$DOCKERHUB_IMAGE_ID:latest")
fi

# Only build amd64 on PRs, build all platforms on main. The arm builds
# take far too long.
image_platforms="--platform linux/amd64"
push_image=""
cache_tag="pr-cache"

# Only push on main
if [[ "$GITHUB_REF" == refs/heads/main ]]; then
push_image="--push"
image_platforms="--platform linux/arm/v7,linux/arm64/v8,linux/amd64"
cache_tag="main-cache"
fi

# Only write to cache if credentials are available
if [[ -z "$DOCKERHUB_USERNAME" ]]; then
write_cache=""
else
write_cache="--cache-to=type=registry,ref=$DOCKERHUB_IMAGE_ID:$cache_tag,mode=max"
fi

docker buildx build \
${push_image} \
${image_platforms} \
--cache-from=type=registry,ref=$DOCKERHUB_USERNAME/$IMAGE_NAME:$cache_tag \
--cache-to=type=registry,ref=$DOCKERHUB_USERNAME/$IMAGE_NAME:$cache_tag,mode=max \
--cache-from=type=registry,ref=$DOCKERHUB_IMAGE_ID:$cache_tag \
${write_cache} \
"${image_tags[@]}" \
.

0 comments on commit 8186547

Please sign in to comment.