地方エンジニアの学習日記

興味ある技術の雑なメモだったりを書いてくブログ。たまに日記とガジェット紹介。

DockerHubのタグを動的生成

CircleCIでのデプロイ時に使える 見たいページが404だったり500でなんでだよってなったのでメモ

前準備としてGitHubとDockerHubの連携時のビルドルールを下記のように設定しておく。

f:id:ryuichi1208:20190912233947p:plain

連携したらとりあえずGitHubへタグ付けをしてみる。 自動ビルドが走ってタグ付きのImageが生成されたら成功。

$ git tag -a "0.0.1-rc" -m "Py 0.0.0"
$ git push origin --tags

肝心の連携方法ですが下記のような感じでJobを書きます。 (前後はだいぶ省略してますがだいたいこんな感じ)

      - run:
          name: Setup git
          command: |
            git config push.default current
            git config user.name "CircleCI"
            git config user.email ${EMAIL}
      - run:
          name: Elm version up
          command: |
            VERSION=`curl -s https://api.github.com/repos/elm/compiler/tags | jq --raw-output .[0].name`
            echo "Elm version => ${VERSION}"
            count=`curl -sI https://github.com/elm/compiler/releases/download/${VERSION}/binaries-for-linux.tar.gz | grep "Status: 302" | wc -l`
            if [ ${count} == "0" ]; then
              echo "${VERSION} is not released yet"
              exit 0
            fi
            # Edit Dockerfile
            sed -ie "s/ENV ELM_VERSION .*$/ENV ELM_VERSION ${VERSION}/g" Dockerfile
            git add Dockerfile
            set +o pipefail
            git commit -m "update: circleci has updated elm version to ${VERSION}" | true
            git tag -a ${VERSION} -m "Elm ${VERSION}" | true
            set -o pipefail
            git push origin --tags
            git push origin

明日使えるDockerイメージのタグを取得するワンライナー

$ curl --silent "https://registry.hub.docker.com/v2/repositories/${DOCKER_IMAGE}/tags/" \
  | jq -r '."results"[]["name"]'