Dockerfile からの相対で hooks/build というファイルに次のようなスクリプトを置きます。
# !/bin/bash
docker build --build-arg SOURCE_COMMIT=$SOURCE_COMMIT -f $DOCKERFILE_PATH -t $IMAGE_NAME .
例えば次のようなレイアウトです(Build Context が hoge/ な例)。
./hoge/
Dockerfile
hooks/
build
Dockerfile で SOURCE_COMMIT がビルドの ARG として使えるようになります。
FROM busybox
ARG SOURCE_COMMIT
ENV SOURCE_COMMIT $SOURCE_COMMIT
CMD ["env"]
詳細
Advanced options for Autobuild and Autotest の Custom build phase hooks を用いたものです。
hooks/build というファイルがあれば docker build の代わりに呼ばれます。このスクリプトでは以下の環境変数が利用可能です。
-
SOURCE_BRANCH: the name of the branch or the tag that is currently being tested.- e.g.
master
- e.g.
-
SOURCE_COMMIT: the SHA1 hash of the commit being tested.- e.g.
5852b96005ac94839ac828b0771e5338cc7e9376
- e.g.
-
COMMIT_MSG: the message from the commit being tested and built.- e.g.
first commit
- e.g.
-
DOCKER_REPO: the name of the Docker repository being built.- e.g.
index.docker.io/your/awesome
- e.g.
-
DOCKERFILE_PATH: the dockerfile currently being built.- e.g.
Dockerfile
- e.g.
-
CACHE_TAG: the Docker repository tag being built.- e.g.
latest
- e.g.
-
IMAGE_NAME: the name and tag of the Docker repository being built. (This variable is a combination ofDOCKER_REPO:CACHE_TAG.)- e.g.
index.docker.io/your/awesome:latest
- e.g.
これらを docker build の --build-arg で渡せば Dockerfile で ARG として使用できます。
hooks/build 以外に hooks/test や hooks/push で自動ビルドの各フェーズが上書きできるようです。
また、以下のようなスクリプトでビルドの各フェーズ間に処理を挟むことができます。
hooks/post_checkouthooks/pre_buildhooks/post_buildhooks/pre_testhooks/post_testhooks/pre_pushhooks/post_push
例えば hooks/post_push を次のような内容で作成しておけば、自動ビルドの Build Rules とは別に Git のコミットIDを Docker タグにできます。
# !/bin/bash
docker tag $IMAGE_NAME $DOCKER_REPO:$SOURCE_COMMIT
docker push $DOCKER_REPO:$SOURCE_COMMIT