Unable to login DockerHUB registry with the github secret

echo "$DOCKERHUB_PASS" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin

I created the valid secrets specific to the github repository, but the circleci.yml fail to fetch the secret

version: 2
jobs:
  build:
    working_directory: ~/app
    environment:
      IMAGE_NAME: xyz
    docker:
      - image: circleci/buildpack-deps:stretch
    steps:
      - checkout:
      - setup_remote_docker
      - run:
          name: build application docker image
          command: |
            docker build  -t json-server  .
      - run:
          name: Validating the test results
          command: |
            docker run -i json-server  > result
            sh -c "[ -s result ]"
  publish-latest:
    working_directory: ~/app
    environment:
      IMAGE_NAME: xyz
      # DOCKER_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }}
      # DOCKER_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
    docker:
      - image: circleci/buildpack-deps:stretch
    steps:
      - checkout:
      - setup_remote_docker
      - run:
          name: Publish Docker Image
          command: |
                  echo "$DOCKERHUB_PASSWORD" | docker login --username "$DOCKERHUB_USERNAME" --password-stdin
                  docker push $IMAGE_NAME:latest

workflows:
  version: 2
  build-master:
    jobs:
      - build:
          filters:
            branches:
              only: master
      - publish-latest:
          requires:
            - build
          filters:
            branches:
              only: master

Hi there,

It looks like you are setting the secrets within your environment image and doubling up on the image reference - I’d recommend setting this with a context and referencing the secrets under an auth key under the docker key like so:

version: 2.1

workflows:
  my-workflow:
    jobs:
      - run-tests:
          context:
            - org-global
            - my-context

jobs:
  run-tests:
    docker:
      - image: cimg/base:2020.01
        auth:
          username: mydockerhub-user
          password: $DOCKERHUB_PASSWORD  # context / project UI env-var reference
    steps:
      - checkout
      - run:
          name: "echo environment variables from org-global context"
          command: echo $MY_ENV_VAR