Trouble in caching dependency in CircleCI

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,178评论 0 10
  • 按阶段划分 一.单元测试 二.集成测试 三.系统测试 四.验收测试 按阶段划分-单元测试 什么是单元测试(Unit...
    南方小镇_阅读 2,571评论 0 0
  • 众所周知,睡眠和饮食是养生的两大法宝。说到睡眠,就不能不提午睡。夏季气候炎热,大部分人会失眠,因此午睡就显得更重要...
    乞丐科技阅读 1,378评论 0 0
  • 7月28日晚上就九点多,我们在北京首都机场,准备去英国。说实话,这一路我肯能有有一些忐忑,因为到那边我要上...
    cy诺ooo阅读 1,378评论 3 6
  • 文/珠峰仰坐 返寒的天真叫人倒胃口。 三、八前的天,还见穿丝袜裙子的淑女逛店,穿衬衫凉鞋的男...
    珠峰仰坐阅读 1,445评论 0 0

友情链接更多精彩内容