This is a basic implementation. For true CI/CD, it's advised that you review how this integrates into your process carefully.
#!/usr/bin/env bash
FILECHECKSUM=$(sha1sum yarn.lock|grep -o '^\S*')
echo "yarn.lock hash is ${FILECHECKSUM}"
CHECKSUM_NODE_MODULES=${JENKINS_HOME}/"${FILECHECKSUM}_node_modules"
if [ -f "$CHECKSUM_NODE_MODULES" ]; then
echo "Found matching node_modules folder. Extracting"
tar xf $CHECKSUM_NODE_MODULES.tar.gz
fi
yarn install --frozen-lockfile;
if [ ! -f "$CHECKSUM_NODE_MODULES" ]; then
echo "No matching node_modules folder was found. Archiving this one"
tar cfz "${CHECKSUM_NODE_MODULES}.tar.gz" node_modules
fi