version: 2.1 orbs: docker: circleci/docker@1.0.1 commands: docker-build: description: | Build a Docker image parameters: dockerfile: default: Dockerfile description: | Name of dockerfile to use, defaults to Dockerfile type: string extra_build_args: default: '' description: > Extra flags to pass to docker build. For examples, see https://docs.docker.com/engine/reference/commandline/build type: string registry: default: docker.io description: | Comma separated list of registry to use, defaults to docker.io type: string image: default: cdalvaro/saltstack-master description: | Name of image to build type: string tag: default: ${CIRCLE_SHA1} description: | Image tag, defaults to the value of $CIRCLE_SHA1 type: string path: default: . description: | Path to the directory containing your Dockerfile and build context, defaults to . (working directory) type: string cache_from: default: '' description: | Comma-separated list of images. Images will first be pulled, then passed as the --cache-from build argument https://docs.docker.com/engine/reference/commandline/build/ type: string no_output_timeout: default: 10m description: | No output timeout for build step type: string steps: - when: condition: <> steps: - run: name: Build image for <> no_output_timeout: <> command: > echo "<>" | sed -n 1'p' | tr ',' '\n' | while read image; do echo "Pulling ${image} ..."; docker pull "${image}" || true done docker_tag_args="" IFS="," read -ra DOCKER_REGISTRIES \<<< "<>" for registry in "${DOCKER_REGISTRIES[@]}"; do IFS="," read -ra DOCKER_TAGS \<<< "<>" for tag in "${DOCKER_TAGS[@]}"; do docker_tag_args="${docker_tag_args} -t ${registry}/<>:${tag}" done done docker build <<#parameters.extra_build_args>><><> \ --cache-from <> \ -f <>/<> \ ${docker_tag_args} \ <> - unless: condition: <> steps: - run: name: Building image for <> no_output_timeout: <> command: > docker_tag_args="" IFS="," read -ra DOCKER_REGISTRIES \<<< "<>" for registry in "${DOCKER_REGISTRIES[@]}"; do IFS="," read -ra DOCKER_TAGS \<<< "<>" for tag in "${DOCKER_TAGS[@]}"; do docker_tag_args="${docker_tag_args} -t ${registry}/<>:${tag}" done done docker build <<#parameters.extra_build_args>><><> \ -f <>/<> \ ${docker_tag_args} \ <> docker-save: description: | Save one or more images to a tar archive parameters: registry: default: docker.io description: | Comma separated list of registry to use, defaults to docker.io type: string image: description: | Name of image to save type: string tag: default: ${CIRCLE_SHA1} description: | Image tag, defaults to the value of $CIRCLE_SHA1 type: string steps: - run: name: Save image to tar archive command: > docker_images="" IFS="," read -ra DOCKER_REGISTRIES \<<< "<>" for registry in "${DOCKER_REGISTRIES[@]}"; do IFS="," read -ra DOCKER_TAGS \<<< "<>" for tag in "${DOCKER_TAGS[@]}"; do docker_images="${docker_images} ${registry}/<>:${tag}" done done mkdir -p ~/docker/ docker save -o ~/docker/docker-images.tar ${docker_images} - persist_to_workspace: root: ~/ paths: - docker docker-load: description: | Load tar archive steps: - attach_workspace: at: ~/ - run: name: Load images from tar archive command: > docker load -i ~/docker/docker-images.tar jobs: build: executor: docker/machine steps: - checkout - docker-build: registry: docker.io,quay.io image: cdalvaro/saltstack-master tag: ${CIRCLE_TAG:-latest} cache_from: docker.io/cdalvaro/saltstack-master:latest extra_build_args: '--build-arg VCS_REF=${CIRCLE_TAG:-${CIRCLE_SHA1}} --build-arg BUILD_DATE="$(date +"%Y-%m-%d %H:%M:%S%:z")"' no_output_timeout: 45m - docker-save: registry: docker.io,quay.io image: cdalvaro/saltstack-master tag: ${CIRCLE_TAG:-latest} test: executor: docker/machine steps: - checkout - docker-load - run: name: Launch docker container command: docker run -it --rm -d --name salt_master cdalvaro/saltstack-master:${CIRCLE_TAG:-latest} - run: name: Container info command: docker container ls - run: name: Wait for salt-master bootup command: sleep 60 - run: name: Test image bootup command: docker exec -it salt_master /usr/local/bin/healthcheck workflows: build-and-test: jobs: - build: filters: tags: only: /^[0-9._]+$/ - test: requires: - build filters: tags: only: /^[0-9._]+$/