简体   繁体   中英

How to access a service in Github Actions CI/CD?

I'm trying to set up a CI/CD pipeline in GitHub Actions for my Elixir project.

I can fetch dependencies, compile them, check formatting, credo... But when the tests starts, I'm not able to reach the PostgreSQL service declared on the YAML.

How can I link both containers? (Elixir and PostgreSQL)

According to the logs shown on GitHub Actions, both containers are on the same Docker network, so they should be reachable from each other using their network aliases. However, when I try to connect to the postgres one, it says NXDOMAIN . Also the ping doesn't work, as expected.

The content of my workflow:

name: Elixir CI

on: push

jobs:
  build:
    runs-on: ubuntu-18.04

    container:
      image: elixir:1.9.1

    services:
      postgres:
        image: postgres
        ports:
        - 5432:5432
        env:
          POSTGRES_USER: my_app
          POSTGRES_PASSWORD: my_app
          POSTGRES_DB: my_app_test

    steps:
    - uses: actions/checkout@v1
    - name: Install Dependencies
      env:
        MIX_ENV: test
      run: |
        cp config/test.secret.ci.exs config/test.secret.exs
        mix local.rebar --force
        mix local.hex --force
        apt-get update -qqq && apt-get install make gcc -y -qqq
        mix deps.get
    - name: Compile
      env:
        MIX_ENV: test
      run: mix compile --warnings-as-errors
    - name: Run formatter
      env:
        MIX_ENV: test
      run: mix format --check-formatted
    - name: Run Credo
      env:
        MIX_ENV: test
      run: mix credo
    - name: Run Tests
      env:
        MIX_ENV: test
      run: mix test

Also, on Elixir I have set up the test task to connect to postgres:5432 , but it says the host does not exist.

According to some tutorials and examples I found on the Internet, this configurations looks like valid, but nothing I could do made it work.

You need to pass the name of the service ( "postgres" ) as POSTGRES_HOST to the application and set the port POSGRES_PORT: ${{ job.services.postgres.ports[5432] }} (spaces matter.)

Github CI dynamically routes port and host to it.

I wrote a blog post on the subject a couple of days ago.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM