npm install
command, ./node_modules
is generated in the project and is cached in the ~/.npm
directory, which is more compact and universal.Package management tool | Cache directory |
Maven | /root/.m2/ |
Gradle | /root/.gradle/ |
npm | /root/.npm/ |
composer | /root/.cache/composer/ |
pip3 | /root/.cache/pip/ |
yarn | /usr/local/share/.cache/yarn/ |
pipeline {agent anystages {stage('check out') {steps {checkout([$class: 'GitSCM',branches: [[name: env.GIT_BUILD_REF]],userRemoteConfigs: [[url: env.GIT_REPO_URL, credentialsId: env.CREDENTIALS_ID]]])}}stage('Java cache') {agent {docker {image 'adoptopenjdk:11-jdk-hotspot'args '-v /root/.gradle/:/root/.gradle/ -v /root/.m2/:/root/.m2/'reuseNode true}}steps {sh './gradlew test'}}stage('npm cache') {steps {script {docker.image('node:14').inside('-v /root/.npm/:/root/.npm/') {sh 'npm install'}}}}}}
/home/ubuntu/.npm/
, and the codes are as follows:docker.image('node:14').inside('-v /home/ubuntu/.npm/:/root/.npm/') {sh 'npm install'}
Dockerfile
image and CI agent image, need to be pulled for every build, the process can be accelerated by caching the images.Jenkinsfile
below by modifying the image name:pipeline {agent anyenvironment{DOCKER_CACHE_EXISTS = fileExists '/root/.cache/docker/php-8.0-cli.tar'}stages {stage('Load cache') {when { expression { DOCKER_CACHE_EXISTS == 'true' } }steps {sh 'docker load -i /root/.cache/docker/php-8.0-cli.tar'}}stage('Use images (modify this section)') {agent {docker {image 'php:8.0-cli'args '-v /root/.cache/:/root/.cache/'reuseNode 'true'}}steps {sh "php -v"}}stage('Generate cache (run once only)') {when { expression { DOCKER_CACHE_EXISTS == 'false' } }steps {sh 'mkdir -p /root/.cache/docker/'sh 'docker save -o /root/.cache/docker/php-8.0-cli.tar php:8.0-cli'}}}}
/root/.cache/
in "Cache Directory". The duration of the second build is significantly shorten due to the cache:⚠️Keep cached images up to date with official updates.
Dockerfile
as the build environment in Continuous Integration, instead of running the docker build
command at initialization, save the built Docker images to a repository to pull and reuse them again.// Creates a CODING Docker repository and obtains the username, password, and repository URLsh "docker login -u $DOCKER_USER -p $DOCKER_PASSWORD my-team-docker.pkg.coding.net"// Use MD5 of Dockerfile as tagmd5 = sh(script: "md5sum Dockerfile | awk '{print \\$1}'", returnStdout: true).trim()imageFullName = "my-team-docker.pkg.coding.net/my-project/my-repo/my-app:dev-${md5}"// Check if images exist in remote repositorydockerNotExists = sh(script: "docker manifest inspect $imageFullName > /dev/null", returnStatus: true)def testImage = nullif (dockerNotExists) {testImage = docker.build("$imageFullName", "--build-arg APP_ENV=testing ./")sh "docker push $imageFullName"} else {testImage = docker.image(imageFullName)}// Use images for automated testingtestImage.inside("-e 'APP_ENV=testing'") {stage('Test') {echo 'testing...'sh 'ls'echo 'test done.'}}
docker manifest inspect ecoding/foo:barno such manifest$ echo $?1
Was this page helpful?