docker - Go测试失败时停止docker容器服务

标签 docker go jenkins build docker-compose

我正在使用docker-compose服务运行Go测试。我的测试失败时,我想使容器失败或构建。但是现在,当Go-tests失败时,docker容器会显示pr_validator_tests_1 exited with code 0不正确。

tests_1        | FAIL   go-testing-framework/tests  0.029s
tests_1        | FAIL
pr_validator_tests_1 exited with code 0

我正在使用以下命令运行docker-compose文件:docker-compose up --abort-on-container-exit --exit-code-from test
这就是我运行测试套件的方式。
func TestIntegration(t *testing.T) {
    suite.Run(t, new(TestSuite))
}

测试断言:
if !s.Equal(expectedCode, response.StatusCode) {
        s.T().Error(message)
        return
    }

docker-compose.yml文件
version: '3'

services:
  integration:
    build:
      context: ./
      dockerfile: Dockerfile
    healthcheck:
      test: ["CMD", "nc", "localhost", "3000"]
    environment:
      - some envies
    container_name: integration
    ports:
      - 3000:3000

  mockservice:
    image: mock:latest
    environment:
      - some envies
    ports:
      - 8801:8801

  tests:
    image: go-testing-framework:latest
    environment:
      - URL=http://integration:3000
      - and some envies
    links:
      - integration
    depends_on:
      - integration
      - mockservice

是否可以不使用Jenkins插件或使用golang代码使Jenkins构建失败?

最佳答案

我在make命令的末尾添加了||exit 1,还添加了一个Shell脚本来检索退出代码。

#!/bin/bash
make test
status=$?
if [[ $status != 0 ]]; then
  echo "exit code for test: " $status
  exit $status
fi

如果测试失败,此脚本将终止容器

关于docker - Go测试失败时停止docker容器服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60335832/

相关文章:

node.js - 如何从 node.js 应用程序获取 docker 映射端口?

node.js - Docker镜像在开发之前还是之后? (初学者)

xml - Golang 解析带有同名嵌套节点的 XML?

jenkins - 将 SonarQube 与 Jenkins 集成时出现错误

windows - 安装Docker时如何在Windows Server 2016 TP4中正确创建虚拟交换机? (不是boot2docker)

ruby-on-rails - 跨 docker 容器共享预编译 Assets

string - 你能用相同的值设置多个(不同的)标签吗?

scala - 在 Scala 中实现 Go 的并发模式难吗?

jenkins - readYaml 方法不适用于 Jenkins

Jenkins 共享库实例化 util 类并在工作流脚本中使用