version: 2.1 executors: docker-executor: environment: IMAGE_NAME: peterevans/dockerhub-description docker: - image: circleci/buildpack-deps:stretch jobs: build: executor: docker-executor steps: - checkout - setup_remote_docker: docker_layer_caching: true - run: name: Build Docker image command: | docker build -t $IMAGE_NAME:latest . --label "org.opencontainers.image.version=${CIRCLE_TAG/v/''}" - run: name: Cache Docker image command: docker save -o image.tar $IMAGE_NAME - persist_to_workspace: root: . paths: - ./image.tar publish-tag: executor: docker-executor steps: - attach_workspace: at: /tmp/workspace - setup_remote_docker - run: name: Load cached Docker image command: docker load -i /tmp/workspace/image.tar - run: name: Publish Docker Image to Docker Hub command: | echo "$DOCKERHUB_PASSWORD" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin IMAGE_TAG=${CIRCLE_TAG/v/''} docker tag $IMAGE_NAME:latest $IMAGE_NAME:$IMAGE_TAG docker push $IMAGE_NAME:latest docker push $IMAGE_NAME:$IMAGE_TAG # If the minor version can be extracted then tag and push MINOR_VERSION=$(echo $CIRCLE_TAG | sed -n "s/^v\([0-9]*.[0-9]*\).[0-9]*$/\1/p") if [[ ${#MINOR_VERSION} -gt 0 ]]; then docker tag $IMAGE_NAME:latest $IMAGE_NAME:$MINOR_VERSION docker push $IMAGE_NAME:$MINOR_VERSION fi workflows: version: 2 build: jobs: - build build-and-publish-tags: jobs: - build: filters: tags: only: /^v.*/ branches: ignore: /.*/ - publish-tag: context: org-global requires: - build filters: tags: only: /^v.*/ branches: ignore: /.*/