Service elasticsearch is not visible when run tests

こ雲淡風輕ζ 提交于 2021-02-04 08:14:46

问题


name: Rspec
on: [push]
jobs:
  build:
    runs-on: [self-hosted, linux]
    services:
      elasticsearch:
        image: docker.elastic.co/elasticsearch/elasticsearch:7.9.2
        env:
          discovery.type: single-node
        options: >-
          --health-cmd "curl http://localhost:9200/_cluster/health"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 10
      redis:
        image: redis
        options: --entrypoint redis-server
    steps:
      - uses: actions/checkout@v2
      - name: running tests
        run: |
          sleep 60
          curl -X GET http://elasticsearch:9200/

I am running tests self hosted, I see on host with docker ps the containers (redis and elasticsearch) when they up to test.

I access a container of redis, install a curl and run curl -X GET http://elasticsearch:9200/ and i see a response ok before 60 sec (wait time to service up)

On step running tests I got error message "Could not resolve host: elasticsearch"

So, inside service/container redis I see a host elasticsearch but on step running tests no. What I can do?


回答1:


You have to map the ports of your service containers and use localhost:host-port as address in your steps running on the GitHub Actions runner.

If you configure the job to run directly on the runner machine and your step doesn't use a container action, you must map any required Docker service container ports to the Docker host (the runner machine). You can access the service container using localhost and the mapped port.

https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions#jobsjob_idservices

name: Rspec
on: [push]
jobs:
  build:
    runs-on: [self-hosted, linux]
    services:
      elasticsearch:
        image: docker.elastic.co/elasticsearch/elasticsearch:7.9.2
        env:
          discovery.type: single-node
        options: >-
          --health-cmd "curl http://localhost:9200/_cluster/health"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 10
        ports:
        # <port on host>:<port on container>
        - 9200:9200
      redis:
        image: redis
        options: --entrypoint redis-server
    steps:
      - uses: actions/checkout@v2
      - name: running tests
        run: |
          sleep 60
          curl -X GET http://localhost:9200/

Alternative: Also run your job in a container. Then the job has to access the service containers by hostname.

name: Rspec
on: [push]
jobs:
  build:
    services:
      elasticsearch:
        image: docker.elastic.co/elasticsearch/elasticsearch:7.9.2
        env:
          discovery.type: single-node
        options: >-
          --health-cmd "curl http://localhost:9200/_cluster/health"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 10
      redis:
        image: redis
        options: --entrypoint redis-server
    # Containers must run in Linux based operating systems
    runs-on: [self-hosted, linux]
    # Docker Hub image that this job executes in, pick any image that works for you
    container: node:10.18-jessie
    steps:
      - uses: actions/checkout@v2
      - name: running tests
        run: |
          sleep 60
          curl -X GET http://elasticsearch:9200/


来源:https://stackoverflow.com/questions/64204333/service-elasticsearch-is-not-visible-when-run-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!