In my CI I need to make sure that my code works with mongo and so I'm using the official mongo docker image and passing my desired credentials as environment variables for the mongo image.
build-on-mongo:
runs-on: ${{ matrix.os }}
env:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: password
MONGO_INITDB_DATABASE: vote-stuysu-org
services:
mongo:
image: mongo
ports:
# Maps tcp port 27017 on service container to the host
- 27017:27017
strategy:
matrix:
os: [ubuntu-20.04]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '14.x'
- name: Install dependencies
run: npm install
- name: Test
run: npm test
env:
MONGO_URL: mongodb://root:password@localhost:27017/vote-stuysu-org
However, in the test step, an error gets logged saying:
(node:2158) UnhandledPromiseRejectionWarning: MongoError: Authentication failed.
I'm using mongoose alongside nodejs and so this is the code responsible for authentication:
mongoose.connect(process.env.MONGO_URL, {
useUnifiedTopology: true,
useNewUrlParser: true
});
I don't think that my connection uri is wrong but I'm not sure why the authentication is failing.