Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I connect to a GitHub Action's job's service?

GitHub Actions allow you to run background services on a per-job basis. After following the examples, I can't figure out how to connect to a running PostgreSQL container.

I've attempted a few different approaches in this pull request, but none of them have worked.

name: dinosql test suite
on: [push]
jobs:

  build:
    name: Build
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres
        env:
          POSTGRES_PASSWORD: postgres
        ports:
          - 5432/tcp

    steps:
    - uses: actions/checkout@master

    - name: Test dinosql/ondeck
      run: go test -v ./...
      working-directory: internal/dinosql/testdata/ondeck
      env:
        PG_USER: postgres
        PG_DATABASE: postgres
        PG_PASSWORD: postgres
        PG_PORT: ${{ job.services.postgres.ports['5432'] }}

This setup results in the following error:

Run go test -v ./...
=== RUN   TestQueries
=== PAUSE TestQueries
=== RUN   TestPrepared
=== PAUSE TestPrepared
=== CONT  TestQueries
=== CONT  TestPrepared
--- FAIL: TestPrepared (0.00s)
##[error]    db_test.go:212: db: postgres://postgres:[email protected]:32768/postgres?sslmode=disable
##[error]    db_test.go:212: dial tcp 127.0.0.1:32768: connect: connection refused
--- FAIL: TestQueries (0.00s)
##[error]    db_test.go:83: db: postgres://postgres:[email protected]:32768/postgres?sslmode=disable
##[error]    db_test.go:83: dial tcp 127.0.0.1:32768: connect: connection refused
FAIL
FAIL    example.com/ondeck  0.005s
?       example.com/ondeck/prepared [no test files]
##[error]Process completed with exit code 1.

The tests should pass if a valid database connection could be made.

like image 567
Kyle Conroy Avatar asked Aug 15 '19 13:08

Kyle Conroy


1 Answers

I ran into the same problem and found this example via GitHub's code search after a lot of trial and error.

name: dinosql test suite
on: [push]
jobs:

  build:
    name: Build
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres
        env:
          POSTGRES_PASSWORD: postgres
        ports:
          - 5432/tcp
        # needed because the postgres container does not provide a healthcheck
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

    steps:
    - uses: actions/checkout@master

    - name: Test dinosql/ondeck
      run: go test -v ./...
      working-directory: internal/dinosql/testdata/ondeck
      env:
        # use postgres for the host here because we have specified a contaienr for the job.
        # If we were running the job on the VM this would be localhost
        PG_HOST: postgres
        PG_USER: postgres
        PG_DATABASE: postgres
        PG_PASSWORD: postgres
        PG_PORT: ${{ job.services.postgres.ports['5432'] }}

Adding the healthcheck options and changing the database hostname from 127.0.0.1 to postgres should do the trick.

It appears that without the healthcheck options, the postgres container will be shut down and won't be available for the tests.

like image 120
lukad Avatar answered Nov 15 '22 09:11

lukad