DEV Community

loading...
Cover image for TIL Using Postgres in GitHub Actions

TIL Using Postgres in GitHub Actions

lxxxvi profile image Mario ・1 min read

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.

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
    # ...

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

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

(Photo by Andrew Rice)

Discussion (1)

pic
Editor guide
Collapse
cescquintero profile image
Francisco Quintero 🇨🇴

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