【问题标题】:Equivalent of healthcheck test in GitLab CI/CD等效于 GitLab CI/CD 中的健康检查测试
【发布时间】:2021-03-25 13:19:13
【问题描述】:

我有这个docker-compose.yml

version: '3.4'

services:
  localstack:
    image: localstack/localstack:0.11.2
    environment: 
      - AWS_DEFAULT_REGION=us-east-1
      - EDGE_PORT=4566
      - SERVICES=sns,sqs
    ports:
      - "4566:4566"
    volumes:
      - "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
    healthcheck:
      test:
        - CMD
        - bash
        - -c
        - awslocal sns list-topics
          && awslocal sqs list-queues
      interval: 5s
      timeout: 10s
      retries: 10

这样,它会利用容器有可用的awslocal 命令来检查healtheck。

假设我需要对 localstack 运行一些测试并且我想等待 localstack 服务启动并运行,那么 GitLab 的等价物是什么?

类似这样的:

tests:
  stage: test
  services:
    - name: localstack/localstack:0.11.2
      alias: localstack
  variables:
    AWS_DEFAULT_REGION: "us-east-1"
    EDGE_PORT: "4566"
    SERVICES: "sns,sqs"
  before_script:
    # should I use before_script? is there a better way? What's the linux command to test the same and wait?
  script: dotnet test --blame --configuration Release
  rules:
    - exists:
      - test/**/*Tests.csproj

更新 1: 一种可能的技巧是使用before_script,但很高兴听到更好的内置解决方案。这也不起作用

/bin/bash: line 125: awslocal: command not found
Failed rounds=9

但这是我的尝试

tests:
  stage: test
  services:
    - name: localstack/localstack:0.11.2
      alias: localstack
  variables:
    AWS_DEFAULT_REGION: "us-east-1"
    EDGE_PORT: "4566"
    SERVICES: "sns,sqs"
  before_script:
    - rounds=10;
      while [ $rounds -gt 0 ]; do
        awslocal sns list-topics && awslocal sqs list-queues && echo OK && break || echo Failed
        rounds=$(($rounds - 1));
        sleep 5;
      done;
  script: dotnet test --blame --configuration Release
  rules:
    - exists:
      - test/**/*Tests.csproj

【问题讨论】:

    标签: bash docker-compose gitlab-ci


    【解决方案1】:

    这是一个可行的解决方案。请注意,尽管在 localstack 容器中有可用的 awslocal(可能需要安装某些东西),但我不能在服务中使用 awslocal。

    无论如何,我用一个简单的 curl 解决了它。

    tests:
      stage: test
      services:
        - name: localstack/localstack:0.11.2
          alias: localstack
      variables:
        AWS_DEFAULT_REGION: "us-east-1"
        EDGE_PORT: "4566"
        SERVICES: "sns,sqs"
      before_script:
        - rounds=10;
          while [ $rounds -gt 0 ]; do
            curl http://localstack:4566 && echo OK && break || echo FAIL
            rounds=$rounds - 1;
            sleep 5;
          done;
      script: dotnet test --blame --configuration Release
      rules:
        - exists:
          - test/**/*Tests.csproj
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-03
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 2014-10-02
      • 2021-09-13
      • 2019-11-03
      相关资源
      最近更新 更多