Modify CIRCLE_TAG before use

I’m using the circleci/aws-ecr orb to build and push an image. I’d like to use the git tag (CIRCLE_TAG env var) as the docker image tag, however, the git tag is in the following format:

@org/package@version

Which causes an error because @ and / aren’t allowed characters.

Is there a way that the use of CIRCLE_TAG in the below snippet can be modified to remove invalid characters, before being applied to the tag parameter?

workflows:
  deploy-to-develop:
    jobs:
      - aws-ecr/build-and-push-image:
          name: "build-graphql-server-develop"
          repo: "repo"
          dockerfile: "./docker/Dockerfile.graphql-server"
          tag: "latest,${CIRCLE_TAG}"

Hi @dnln, and welcome to the CircleCI community!

I’d suggest using sed to modify the content of CIRCLE_TAG, and set another variable with the new content.

For example:
NICE_TAG=$(sed 's/@//' <<< $CIRCLE_TAG | sed 's/\//-/' | sed 's/@/./')

will turn @org/package@version into org-package.version

You can then use tag: "latest,${NICE_TAG}"

Hey @yannCI, thanks so much for getting back to me, and for your suggestion.

I phrased my question badly, but what I was wondering was, where can I run the NICE_TAG=$(sed 's/@//' <<< $CIRCLE_TAG | sed 's/\//-/' | sed 's/@/./') line so that it can be used in the orb job params?

Hi @dnln,

Sorry for not providing a more accurate and comprehensive reply.

Using interpolation when setting environment variables is a little tricky, but it’s still possible (https://circleci.com/docs/2.0/env-vars/#setting-an-environment-variable-in-a-shell-command).

And because you’re using the build-and-push-image job defined in the circleci/aws-ecr orb, I believe the easiest way to proceed is to leverage the pre-steps feature (https://circleci.com/docs/2.0/reusing-config/#using-pre-and-post-steps).

Here’s my suggestion:

workflows:
  deploy-to-develop:
    jobs:
      - aws-ecr/build-and-push-image:
          name: "build-graphql-server-develop"
          repo: "repo"
          dockerfile: "./docker/Dockerfile.graphql-server"
          tag: "latest,${NICE_TAG}"

          pre-steps:
            - run:
                command: |
                  echo "NICE_TAG=$(sed 's/@//' \<<< ${CIRCLE_TAG} | sed 's/\//-/' | sed 's/@/./')" >> $BASH_ENV
                  source $BASH_ENV

Let me know if this works for you.

pre-step was exactly what I was missing. That works perfectly. Thanks very much for your help @yannCI.

1 Like

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