In this guide, you’ll learn how to set up a code coverage workflow in GitHub Actions by combining a job container with a MongoDB service container. This approach ensures all steps run in a consistent environment and can connect to your database service seamlessly.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
1. Recap: Unit Testing with a Service Container
Below is a sample job that runs unit tests in Node.js while using a MongoDB service container:| Service Name | Image | Ports | Environment Variables |
|---|---|---|---|
| mongo-db | siddharth67/mongo-db:non-prod | 27017:27017 | MONGO_URI, MONGO_USERNAME, MONGO_PASSWORD |
2. What Is a Job Container?
A job container allows you to execute every step of a job inside a specified Docker image instead of on the default runner VM. This ensures your CI environment matches production more closely.
By specifying
container.image, all subsequent steps run inside that Docker image. You no longer need a separate setup action for Node.js.3. Defining the Code Coverage Workflow
The following workflow runs a code coverage job in a Node.js 18 container and attaches a MongoDB service container for database operations:| Key Point | Details |
|---|---|
| Job Container Image | node:18 |
| Service Container | siddharth67/mongo-db:non-prod |
| Service Network Alias | mongo (used in MONGO_URI) |
No setup-node Step Needed | Node is preinstalled within the job container image |
Make sure the service container name (
mongo) matches the hostname used in MONGO_URI. Incorrect naming can lead to connection failures.4. Verify and Inspect Logs
After committing and pushing your workflow, navigate to the Actions tab in GitHub. You should see both the Unit Testing and Code Coverage jobs running in parallel.
