Can't use project/context variables to deploy over SSH or build Docker image with tag

My current workflow is to build, push Docker image and then deploy over SSH. Despite already setting up variables within project and context settings, I still can’t access them through bash shell or set in parameters of using orbs. Below is my config, if there is anything wrong pls let me know. Thanks in advance.

version: 2.1

orbs:
  docker: circleci/docker@2.1.4
  slack: circleci/slack@4.10.1

jobs:
  build-and-push-image:
    executor: docker/docker
    steps:
      - setup_remote_docker:
          docker_layer_caching: true
      - checkout
      - docker/check
      - docker/build:
          image: $DOCKER_LOGIN/$CIRCLE_PROJECT_REPONAME
          tag: $CIRCLECI_BRANCH
          debug: true
          use-buildkit: true
      - docker/push:
          image: $DOCKER_LOGIN/$CIRCLE_PROJECT_REPONAME
      - slack/notify:
          event: pass
          chanel: $SLACK_DEFAULT_CHANNEL
          template: basic_success_1
      - slack/notify:
          event: fail
          chanel: $SLACK_DEFAULT_CHANNEL
          template: basic_fail_1

  deploy-dev:
    machine: true
    environment:
      DEPLOY_DIR: docker-apps/handbooq-nodejs
    steps:
      - add_ssh_keys:
          fingerprints:
            - "host fingerprint"
      - run:
          name: Deploy Over SSH
          command: |
            ssh $REMOTE_USER@$DEV_HOST -v -t "cd $DEPLOY_DIR; docker pull $DOCKER_LOGIN/$CIRCLE_PROJECT_REPONAME:$CIRCLE_BRANCH"

workflows:
  dev:
    jobs:
      - build-and-push-image:
          context:
            - handbooq
            - build-env-vars
            - docker-hub-creds
      - deploy-dev:
          requires:
            - build-and-push-image
          filters:
            branches:
              only:
                - my branch
          context:
            - handbooq
            - build-env-vars
            - docker-hub-creds

Hi @hongphuc5497,

You’re only applying the handbooq, build-env-vars, and docker-hub-creds contexts to the deploy-dev job. Is that intentional?

It would be problematic if the DOCKER_LOGIN environment variable is declared in any of those contexts, because then the docker/build and docker/push jobs wouldn’t have access to it.

Also, I see you’re referencing a CIRCLECI_BRANCH environment variable. Is it an environment variable you declared (either in the project settings or in a context)? Or is it a typo, and what you actually want to reference is the CircleCI built-in environment variable CIRCLE_BRANCH?

Hi @yannCI

  • About build-env-vars and docker-hub-creds contexts, it’s intentional from the beginning, I applied them based on CircleCI tutorial post, but after that, I removed them from my config, I only left the one I created.
  • DOCKER_LOGIN and DOCKER_PASSWORD were created within Project Setting, I accessed it successfully afterward.
  • Ya, it’s a typo.
    Finally, my pipeline runs as I expected. Thanks for your support. Speaking of which, can I ask does the context name itself (handbooq in this case) is marked in the bash shell? When I checked the shell output, it didn’t display correctly.

Hello @hongphuc5497,

Glad to hear your pipeline now runs as expected! :tada:

Could you clarify what you mean by:

When I checked the shell output, it didn’t display correctly.

Is it that the output is masked (showing ****** characters)?

If so, this is an expected behaviour due to the “secrets masking” feature.

1 Like