'How to add config args to postgres service container in Github Action?

I am using Github Actions Service Container to start a postgres instance like so:

name: Pull Request

on:
  pull_request:
    branches:
      - main
      - staging

jobs:
  test-and-build:
    runs-on: ubuntu-latest

    services:
      redis:
        image: redis
        ports:
          - 6379:6379
      postgres:
        image: postgres
        env:
          POSTGRES_PASSWORD: postgres
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432

...

This works but I would like to speedup the postgres instance by disabling fsync as described here: https://dev.to/thejessleigh/speed-up-your-postgresql-unit-tests-with-one-weird-trick-364p

When running postgres locally in a docker container this speeds up my run massively, im hoping to do the same in Github Actions but am struggling to see how I configure the image. It appears that passing -c fsync=off into the options block above causes an error:

Exit code 125 returned from process: file name '/usr/bin/docker', arguments 'create --name 04a7236d2e964fccb8a7d95684b3cf05_postgres_0249b3 --label cc4956 --network github_network_3023184aafab424896b4e553d7ad9c38 --network-alias postgres -p 5432:5432 --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 -c fsync=off -e "POSTGRES_PASSWORD=postgres" -e GITHUB_ACTIONS=true -e CI=true postgres'.

Any ideas?



Solution 1:[1]

There does not seem to be a way to inject commands into services currently so that you can turn off the rsync feature. Github actions uses create command which does not support the -c attribute.

But you could use a different image from https://github.com/bitnami/bitnami-docker-postgresql which allows you to turn it off through an env variable. Here is an example:

name: Postgress test
on:
  push:
jobs:
  test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: 'bitnami/postgresql:latest'
        env:
          POSTGRESQL_DATABASE: test_postgress
          POSTGRESQL_USERNAME: test_user
          POSTGRESQL_PASSWORD: test_password
          POSTGRESQL_FSYNC: "off"
        options: >-
          --health-cmd "pg_isready -d test_postgress -U test_user -p 5432"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432
    steps:
      - run: echo test

Side note, this env is not documented in their readme, but you can find it in their source code https://github.com/bitnami/bitnami-docker-postgresql/blob/b2b1c1410293fc9a8b58a52b56f2ceabdac59bb1/9.6/debian-10/rootfs/opt/bitnami/scripts/libpostgresql.sh#L433-L445

Solution 2:[2]

If you are willing to create your own dockerfile you can run a bash script to make any config changes you like. You can also execute sql commands in the same manner.

Dockerfile:

FROM postgres

COPY docker-entrypoint-initdb.d/ /docker-entrypoint-initdb.d/

RUN ["chmod", "-R", "+x", "/docker-entrypoint-initdb.d/"]

Example file in docker-entrypoint-initdb.d/

#!/bin/bash

echo -e '\nfsync = off' >> /var/lib/postgresql/data/postgresql.conf

In your docker compose, simply replace image: postgres with build: pointing to this dockerfile.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 elexis