Basic Workflow Without Service Containers
The simplest workflow runs tests immediately after code checkout and dependency installation. It’s ideal for unit tests that don’t require external services.This workflow is perfect for fast-running unit tests that do not depend on external databases or caches. If your tests require a service, see the next section.
Adding a MongoDB Service Container
To test against a MongoDB database, add aservices section. This example maps the container’s default MongoDB port to a host port on the runner:
- Service Container:
mongo:latestruns asmongodb-service. - Port Mapping: Container port
27017is mapped to runner port12345. - Environment Variables: Tests connect to
localhost:12345.
Common Service Containers for CI Workflows
| Service | Description | Docker Image |
|---|---|---|
| MongoDB | NoSQL document database | mongo:latest |
| PostgreSQL | Relational SQL database | postgres:13 |
| Redis | In-memory cache | redis:6 |
| MySQL | Widely-used SQL database | mysql:8 |
Be mindful of resource limits on GitHub-hosted runners. Running multiple heavyweight services can lead to slower startup times or timeouts.
Running Steps Inside a Job Container with Services
You can isolate your build environment by specifying a job container and still link to service containers through the Docker network:- Job Container runs every step inside
ghcr.io/node-and-packages:20. - Service Container
mongodb-serviceis discoverable on the default Docker bridge network. - Networking uses the service container’s hostname (
mongodb-service) without port mapping.