Skip to content

Instantly share code, notes, and snippets.

@jprinet
Last active June 3, 2026 09:10
Show Gist options
  • Select an option

  • Save jprinet/64f20d91d8ca6babdee018dea9410cb6 to your computer and use it in GitHub Desktop.

Select an option

Save jprinet/64f20d91d8ca6babdee018dea9410cb6 to your computer and use it in GitHub Desktop.
Develocity Artifact Cache CLI - GitLab CI template for Gradle (downloads the CLI from the public docs.gradle.com download location). Adapted from the official GitHub Actions example.
# Artifact Cache CLI / Gradle + npm template for GitLab CI
#
# Adapted from the official GitHub Actions example at:
# https://docs.gradle.com/develocity/artifact-cache/1.2/how-to/configure-github-actions/#complete-workflow-example
#
# The Artifact Cache CLI is downloaded directly from the public Develocity
# download location, so no repository credentials are required:
# https://docs.gradle.com/develocity/artifact-cache/1.2/#download
#
# DEVELOCITY_ACCESS_KEY is expected to be injected as a masked env var at the
# group/instance level - no per-project configuration needed.
stages:
- build
variables:
# Artifact Cache configuration
ARTIFACT_CACHE_CLI_VERSION: "1.2.0"
# Base image name. The current branch is appended automatically, and restore
# falls back to the default-branch image - see the before_script below and
# https://docs.gradle.com/develocity/artifact-cache/1.2/concepts/images/#multiple-fallback-images
ARTIFACT_CACHE_IMAGE: "sample-gradle"
# Develocity
DV_SERVER: "https://<your-develocity-server>"
# Full URL to the Artifact Cache CLI jar (public download location)
ARTIFACT_CACHE_CLI_URL: "https://docs.gradle.com/downloads/develocity-artifact-cache-cli/develocity-artifact-cache-cli-${ARTIFACT_CACHE_CLI_VERSION}.jar"
# Extra CLI flags (optional)
ARTIFACT_CACHE_OPTS: ""
ARTIFACT_CACHE_TOOL_DIR: "${CI_PROJECT_DIR}/.tools/develocity/artifact-cache"
build:
stage: build
image: eclipse-temurin:21-jdk
cache:
key: artifact-cache-cli-$ARTIFACT_CACHE_CLI_VERSION
paths:
- .tools/develocity/artifact-cache/
policy: pull-push
before_script:
- |
set -euo pipefail
JAR_PATH="${ARTIFACT_CACHE_TOOL_DIR}/${ARTIFACT_CACHE_CLI_VERSION}/develocity-artifact-cache-cli.jar"
mkdir -p "$(dirname "$JAR_PATH")"
if [ ! -f "$JAR_PATH" ]; then
echo "Downloading Artifact Cache CLI v${ARTIFACT_CACHE_CLI_VERSION}..."
curl --location --fail --silent --show-error \
--connect-timeout 5 --max-time 30 \
--retry 3 --retry-delay 3 --retry-max-time 60 \
"${ARTIFACT_CACHE_CLI_URL}" \
--output "$JAR_PATH"
fi
export ARTIFACT_CACHE_CMD="java -jar $JAR_PATH"
# npm cache location: honour NPM_CONFIG_CACHE if the build sets it, otherwise
# fall back to ~/.npm - the same resolution the CLI's npm auto-detection uses.
# https://docs.gradle.com/develocity/artifact-cache/1.2/reference/cli-commands/
NPM_HOME="${NPM_CONFIG_CACHE:-${HOME}/.npm}"
# Point the CLI at Gradle's default user home (~/.gradle) and the npm cache so
# both restore and store collect Gradle and npm artifacts in one pass.
COMMON_OPTS="--dv-server=${DV_SERVER} --gradle-home=${HOME}/.gradle --npm-home=${NPM_HOME} ${ARTIFACT_CACHE_OPTS}"
# Best-practice image naming: one image per branch, with the default branch
# as a fallback so feature branches start from the main cache.
# https://docs.gradle.com/develocity/artifact-cache/1.2/concepts/images/#multiple-fallback-images
# GitLab's CI_COMMIT_REF_SLUG is already lowercased/sanitized; slugify the
# default branch the same way so the fallback matches what main stores.
DEFAULT_SLUG=$(printf '%s' "${CI_DEFAULT_BRANCH:-main}" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9]+/-/g; s/^-+//; s/-+$//' | cut -c1-63)
BRANCH_SLUG="${CI_COMMIT_REF_SLUG:-$DEFAULT_SLUG}"
# restore: try the current branch image first, then fall back to the default branch
RESTORE_IMAGES="--image-name=${ARTIFACT_CACHE_IMAGE}-${BRANCH_SLUG}"
if [ "${BRANCH_SLUG}" != "${DEFAULT_SLUG}" ]; then
RESTORE_IMAGES="${RESTORE_IMAGES} --image-name=${ARTIFACT_CACHE_IMAGE}-${DEFAULT_SLUG}"
fi
export ARTIFACT_CACHE_RESTORE_OPTS="${COMMON_OPTS} ${RESTORE_IMAGES}"
# store: write only to the current branch image (store does not take fallbacks)
export ARTIFACT_CACHE_STORE_OPTS="${COMMON_OPTS} --image-name=${ARTIFACT_CACHE_IMAGE}-${BRANCH_SLUG}"
script:
# Restore - warn but do not fail
- |
$ARTIFACT_CACHE_CMD restore $ARTIFACT_CACHE_RESTORE_OPTS \
|| echo "WARNING - Could not restore the Artifact Cache. The build will proceed but may be slower."
# Build (uses ~/.gradle, which is what the CLI restored into)
- ./gradlew build
# Store - warn but do not fail
- |
$ARTIFACT_CACHE_CMD store $ARTIFACT_CACHE_STORE_OPTS \
|| echo "WARNING - Failed to store in the Artifact Cache."
after_script:
# Move the CLI log into the workspace so GitLab can publish it as an artifact.
- cp -f ~/.develocity/artifact-cache/artifact-cache.log artifact-cache.log 2>/dev/null || true
artifacts:
when: always
expire_in: 7 days
paths:
- artifact-cache.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment