Migrating tag based deployment to 2.0

I’m using CircleCI to build Docker images. I’m confused about how to do tag based deployments with 2.0.

I was using CircleCI 1.0 in such a way that only a semver tag (like 1.0.6) was triggering a build. I’ve migrated the build to 2.0, however I don’t know how can I achieve the same tag based behaviour.

I’d like to:

  1. Only run the build command if a semver tag /[0-9]+(\.[0-9]+)*/ has been pushed to the master branch.
  2. When a semver tag has been pushed, I need access to the current tag, like how I could simply use $CIRCLE_TAG in 1.0.

Here is my old config:

machine:
  services:
    - docker
  environment:
    image_user: hyperknot
    image_repo: baseimage16
    image_root: image

general:
  branches:
    only:
      - skip

dependencies:
  override:
    - docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASS

    - "docker build --rm=false -t $image_user/$image_repo:build_cache .":
        pwd: $image_root

test:
  override:
    - exit 0

deployment:
  hub:
    tag: /[0-9]+(\.[0-9]+)*/
    commands:
      - pip install -U docker-squash 'docker < 3' --disable-pip-version-check
      - docker-squash -t $image_user/$image_repo:$CIRCLE_TAG $image_user/$image_repo:build_cache

      - docker images
      - docker push $image_user/$image_repo:$CIRCLE_TAG

and new config:

version: 2

jobs:
  build:
    environment:
      image_user: hyperknot
      image_repo: baseimage16

    docker:
      - image: circleci/python:3.7

    steps:
      - checkout
      - setup_remote_docker

      - run: env
      - run: docker version

      - run:
          command: docker build -t $image_user/$image_repo:build_cache .
          working_directory: image

      - run: sudo pip install docker-squash --disable-pip-version-check
      - run: docker-squash -t $image_user/$image_repo:$CIRCLE_TAG $image_user/$image_repo:build_cache

      - run: docker images

      - run: docker push $image_user/$image_repo:$CIRCLE_TAG

What do I need to add to the config? How can I access a $CIRCLE_TAG like variable in 2.0?

I believe what you’re looking for is a function of workflows – the docs on that particular function are here. Let me know if you need a hand setting it up!

It was tricky, as $CIRCLE_TAG wasn’t present anywhere in the docs, but the following workflows: code made it work, including magically adding $CIRCLE_TAG.

version: 2

jobs:
  build:
    environment:
      image_user: hyperknot
      image_repo: dockerbase

    docker:
      - image: circleci/python:3.7

    steps:
      - checkout
      - setup_remote_docker

      - run:
          command: docker build -t $image_user/$image_repo:build_cache .

      - run: sudo pip install docker-squash --disable-pip-version-check
      - run: docker-squash -t $image_user/$image_repo:$CIRCLE_TAG $image_user/$image_repo:build_cache

      - run: docker images

      - run: docker login -u $DOCKER_USER -p $DOCKER_PASS
      - run: docker push $image_user/$image_repo:$CIRCLE_TAG


workflows:
  version: 2
  tagged-build:
    jobs:
      - build:
          filters:
            branches:
              ignore: /.*/
            tags:
              only: /^\d+\.\d+\.\d+$/
1 Like

Not sure when then last update to this page was, but here is a full list of all the builtin CircleCI environment variables. It also presently includes the $CIRCLE_TAG env var.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.