Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

github actions - Service elasticsearch is not visible when run tests

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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/

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...