How to add record to hosts file?

Hello! I need to add a record to the hosts file in my app. Here is my config:

version: 2.1
jobs:
  run_api_tests:
    docker:
      - image: circleci/node:14
    steps:
      - checkout
      - run:
          name: Install npm dependencies
          command: |
            npm ci
      - run:
          name: Run API tests
          command: |
            npm run test-api
workflows:
  build_test:
    jobs:
      - run_api_tests

In my tests I am calling domain.com, so I’d like to add
domain.com 127.0.0.1
to the /etc/hosts

But how to do this? I get permissions denied if I try to modify the file. Thanks.

Try like this.

version: 2.1
jobs:
  run_api_tests:
    docker:
      - image: circleci/node:14
    steps:
      - run:
          name: Add domain.com
          command: |
            sudo tee -a /etc/hosts \<<<'127.0.0.1 domain.com'
            cat /etc/hosts
workflows:
  build_test:
    jobs:
      - run_api_tests

Thank you!!! You saved my day, it works!

1 Like

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