Hi everyone!
Been hoping I could get some help here.
Right now, our builds take up to 20 minutes, which is insanely long, but that’s because every build all the dependencies are downloaded again. My idea is to implement file-based caching to speed up build time. Any ideas? Java apps, gradle. Develocity gradle cache isn’t an option.
Hi @ramili4.aktobe,
Could you explain more about the steps you used to build it and the resources you’re using to build the application?
Hi! Sure thing - it’s gradle, and I suppose it keeps cache in /workspace/.gradle. The idea is to zip the entire folder at the end of the pipeline and then push it to our artifactory (Jfrog) to reuse in consequent builds.
I’m thinking about this:
stages {
stage('Cache Gradle') {
steps {
script {
def cacheVersion = sh(script: "date +%Y%m%d%H%M%S", returnStdout: true).trim()
sh """
mkdir -p cache
zip -r cache/gradle-cache-${cacheVersion}.zip /workspace/.gradle
"""
withCredentials([usernamePassword(credentialsId: 'jfrog-user', usernameVariable: 'JFROG_USER', passwordVariable: 'JFROG_PASSWORD')]) {
sh """
curl -u $JFROG_USER:$JFROG_PASSWORD -T cache/gradle-cache-${cacheVersion}.zip \\
$ARTIFACTORY_URL/$ARTIFACTORY_REPO/gradle-cache-${cacheVersion}.zip
"""
}
writeFile file: 'cache_version.txt', text: cacheVersion
}
}
}
}
}
and then in consequent build reuse it:
stage('Restore Gradle Cache') {
steps {
script {
def cacheVersion = sh(script: "curl -s -u $JFROG_USER:$JFROG_PASSWORD $ARTIFACTORY_URL/$ARTIFACTORY_REPO/ | grep -o 'gradle-cache-[0-9]*.zip' | sort | tail -1", returnStdout: true).trim()
if (cacheVersion) {
sh """
curl -u $JFROG_USER:$JFROG_PASSWORD -o gradle-cache.zip $ARTIFACTORY_URL/$ARTIFACTORY_REPO/$cacheVersion
unzip -o gradle-cache.zip -d /
"""
} else {
echo "No cache found, skipping restore"
}
}
}
}
``` If there is a simpler and more elegant solution - I only welcome that!
Yes, please try it and let me know if you face any problem!