Background
I am doing some tests in CircleCI but used our own docker image instead of CircleCI's one, so I'd like to install dockerize
to do some monitor tasks.
When trying to cache the dependency in CircleCI in an irregular way, I got an error as the following.
Error computing cache key: template: cacheKey:1: unexpected unclosed action in command
The part of config file:
jobs:
test:
steps:
- run:
name: create dockerize version file
command: echo "${DOCKERIZE_VERSION}" > /tmp/_dockerize_version_file
- restore_cache:
key: dockerize-bin-cache-{{ checksum "/tmp/_dockerize_version_file" }}
- run:
name: install dockerize utility
command: |
if ( ! type dockerize > /dev/null 2>&1 ) || [[ "$(dockerize --version)" != "$DOCKERIZE_VERSION" ]]; then
wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
else
echo "Binary exists. Dockerize's installation is skipped."
fi
- save_cache:
key: dockerize-bin-cache-{{ checksum "/tmp/_dockerize_version_file" }}
paths:
- /usr/local/bin/dockerize
- run:
name: cleanup dockerize version file
command: rm -rf /tmp/_dockerize_version_file
Solution
Couldn't find any solutions from the official document and forums and finally solved it by using the old style cache syntax.
jobs:
test:
steps:
- run:
name: create dockerize version file
command: echo "${DOCKERIZE_VERSION}" > /tmp/_dockerize_version_file
- type: cache-restore
key: dockerize-bin-cache-{{ checksum "/tmp/_dockerize_version_file" }}
- run:
name: install dockerize utility
command: |
if ( ! type dockerize > /dev/null 2>&1 ) || [[ "$(dockerize --version)" != "$DOCKERIZE_VERSION" ]]; then
wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
else
echo "Binary exists. Dockerize's installation is skipped."
fi
- type: cache-save
key: dockerize-bin-cache-{{ checksum "/tmp/_dockerize_version_file" }}
paths:
- /usr/local/bin/dockerize
- run:
name: cleanup dockerize version file
command: rm -rf /tmp/_dockerize_version_file
You may want to ask why not use the DOCKERIZE_VERSION env var directly instead of saving to a tempfile. Actually I tried to set the env var in job scope, step scope, in shell but failed on all cases. There is a feature request. The official document tells us {{ .Environment.XXX }} can be used to retrieve exported or context based env var. I didn't try that.
Reference
Official document of restore_cache
Cannot use circle.yml environment variables in cache keys
网友评论