diff --git a/.github/workflows/my-workflow.yml b/.github/workflows/my-workflow.yml index 996c5e3..17aa7e5 100644 --- a/.github/workflows/my-workflow.yml +++ b/.github/workflows/my-workflow.yml @@ -15,6 +15,24 @@ jobs: install: runs-on: ubuntu-latest + # Service containers to run with `container-job` + services: + # Label used to access the service container + postgres: + # Docker Hub image + image: postgres + # Provide the password for postgres + env: + POSTGRES_DB: "cicd_database" + POSTGRES_USER: "cicd_user" + POSTGRES_PASSWORD: "cicd_password" + # Set health checks to wait until postgres has started + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + steps: - name: Checkout repository uses: actions/checkout@v4 @@ -33,3 +51,14 @@ jobs: - name: Test test if: ${{ inputs.exe_test == 'false' }} run: npm test + + - name: Connect to PostgreSQL + # Runs a script that creates a PostgreSQL table, populates + # the table with data, and then retrieves the data. + run: node client.js + # Environment variables used by the `client.js` script to create a new PostgreSQL table. + env: + # The hostname used to communicate with the PostgreSQL service container + POSTGRES_HOST: postgres + # The default PostgreSQL port + POSTGRES_PORT: 5432 diff --git a/test/database.test.ts b/test/database.test.ts new file mode 100644 index 0000000..d91e06d --- /dev/null +++ b/test/database.test.ts @@ -0,0 +1,20 @@ +import { Client } from "pg"; + +describe("Database", () => { + test("connection", async () => { + const database = new Client({ + host: "127.0.0.1", + port: 5432, + database: "cicd_database", + user: "cicd_user", + password: "cicd_password", + }); + + const connected = await database + .connect() + .then(() => true) + .catch(() => false) + .finally(async () => await database.end()); + expect(connected).toBe(true); + }); +}); \ No newline at end of file