运行测试时看不到服务elasticsearch

时间:2020-10-05 07:41:32

标签: elasticsearch github-actions

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/

我正在自行运行测试,我在托管测试的docker ps容器(redis和elasticsearch)上看到了它们。

我访问一个redis容器,安装了curl并运行curl -X GET http://elasticsearch:9200/,并且在60秒(等待服务时间)之前看到了可以的响应

在步骤运行测试上,我收到错误消息“无法解析主机:elasticsearch”

因此,在服务/容器内部,我看到一个主机弹性搜索,但是在步骤运行测试上没有。我能做什么?

1 个答案:

答案 0 :(得分:1)

您必须映射服务容器的端口,并在GitHub Actions运行器上运行的步骤中使用localhost:host-port作为地址。

如果将作业配置为直接在运行器计算机上运行,​​并且您的步骤不使用容器操作,则必须将所有必需的Docker服务容器端口映射到Docker主机(运行器计算机)。您可以使用localhost和映射的端口访问服务容器。

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/

替代: 还可以在容器中运行作业。然后作业可以通过主机名访问服务容器。

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/