DEV Community

Cover image for TIL Using Postgres in GitHub Actions
Mario
Mario

Posted on

TIL Using Postgres in GitHub Actions

I recently made my first experiences with GitHub Actions.

Setting up Postgres took me several attempts because of a misunderstanding with variables in the service's env block.

I thought that I could just define POSTGRES_PASSWORD in the global env block and this would then be available and used in Postgres' service. However, not having set POSTGRES_PASSWORD in services.postgres.env gave me this nice, quite unhelpful error message:

##[error]Failed to initialize, postgres service is unhealthy.
Enter fullscreen mode Exit fullscreen mode

After a while I learned that the environment variable POSTGRES_PASSWORD in services.postgres.env is mandatory and must not be blank.

Also, the variables defined in services.postgres.env are passed into the docker image, thus they are somewhat different compared to regular env blocks in Github Action Workflow syntax... I guess?

However, my final result looks like this.

env:
  # ...
  POSTGRES_PASSWORD: mysecretpassword

jobs:
  a-job-that-uses-postgres:
    # ...
    services:
      postgres:
        image: postgres:latest
        env:
          POSTGRES_PASSWORD: ${{ env.POSTGRES_PASSWORD }}
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432
    # ...
Enter fullscreen mode Exit fullscreen mode

Which is being translated into this command, which makes it clear what happens with the variables.

docker create\
  # many others\
  -p 5432:5432\
  --health-cmd pg_isready\
  --health-interval 10s\
  --health-timeout 5s\
  --health-retries 5\
  -e "POSTGRES_PASSWORD=mysecretpassword"\    # <= 💡
  -e GITHUB_ACTIONS=true\
  -e CI=true\
  postgres:latest
Enter fullscreen mode Exit fullscreen mode

My learning may be trivial, but I hope that this helps someone having the same issue.

(Photo by Andrew Rice)

Oldest comments (1)

Collapse
 
cescquintero profile image
Francisco Quintero 🇨🇴

Thanks, very useful the part of not leaving empty the POSTGRES_PASSWORD.