Example 1: Basic PostgreSQL Service Configuration
This example demonstrates defining a job that runs on an Ubuntu runner with a Node.js container. Under the job’s services, a PostgreSQL container is specified with the required environment variables and health checks. GitHub Actions automatically starts this service container before executing the job steps.Example 2: Using a Different Container Image and Health Command
In this configuration, a more recent Node.js container image is used alongside an explicit health command (--health-cmd pg_isready) to check if PostgreSQL is ready. This setup ensures that your tests run only after PostgreSQL has fully started.
Example 3: Running PostgreSQL Directly as the Container
Sometimes, you may run the PostgreSQL image directly as your container. In this case, environment variables and health checks are specified at the container level, and dependencies are installed using a clean installation withnpm ci.
Passing Environment Variables and Custom Database Names
You can pass environment variables into the Docker container to configure settings such as the PostgreSQL password or database name. In the following example, a custom database (e.g., FastAPI_test) is automatically created to match the settings expected by your code.In this YAML configuration, the port mapping for PostgreSQL is hardcoded as
5432:5432. Although you might consider using variables for flexibility, GitHub Actions requires these values to be specified explicitly.Running Tests and Verifying the Setup
After setting up your PostgreSQL service, your CI pipeline will automatically pull your code, install dependencies, and run tests using frameworks like pytest. Below is an example of console output indicating successful test execution:With these configurations, you’ve successfully integrated PostgreSQL into your CI pipeline and verified connectivity through your tests. Happy coding!