.net - 问:如何配置cloudbuild.yaml以运行测试?

标签 .net docker google-cloud-platform google-cloud-build

我想在GCP上建立一个完全自动化的CI / CD管道。在Cloud Build中,我已经有了一个cloudbuild.yaml,它可以构建Dockerfile并将构建的Docker镜像推送到Container Registry。但是,在构建Dockerfile之前,我找不到运行xUnit测试的解决方案,并且如果任何测试失败,则Cloud Build不应构建Dockerfile并退出并返回错误/异常。

我目前正在使用的cloudbuild.yaml:

steps:
  - name: gcr.io/cloud-builders/dotnet
    entrypoint: 'bash'
    args:
        - '-c'
        - |
            dotnet test --no-build -c ${_BUILD_CONFIG} $REPO_NAME\\TestCore
    id: Test
  - name: gcr.io/cloud-builders/docker
    args:
      - build
      - '-t'
      - '$_IMAGE_NAME:$COMMIT_SHA'
      - .
      - '-f'
      - $_DOCKERFILE_NAME
    dir: $_DOCKERFILE_DIR
    id: Build
  - name: gcr.io/cloud-builders/docker
    args:
      - push
      - '$_IMAGE_NAME:$COMMIT_SHA'
    id: Push
  - name: gcr.io/cloud-builders/gke-deploy
    args:
      - prepare
      - '--filename=$_K8S_YAML_PATH'
      - '--image=$_IMAGE_NAME:$COMMIT_SHA'
      - '--app=$_K8S_APP_NAME'
      - '--version=$COMMIT_SHA'
      - '--namespace=$_K8S_NAMESPACE'
      - '--label=$_K8S_LABELS'
      - '--annotation=$_K8S_ANNOTATIONS,gcb-build-id=$BUILD_ID'
      - '--create-application-cr'
      - >-
        --links="Build
        details=https://console.cloud.google.com/cloud-build/builds/$BUILD_ID?project=$PROJECT_ID"
      - '--output=output'
    id: Prepare deploy
  - name: gcr.io/cloud-builders/gsutil
    args:
      - '-c'
      - |-
        if [ "$_OUTPUT_BUCKET_PATH" != "" ]
        then
          gsutil cp -r output/suggested gs://$_OUTPUT_BUCKET_PATH/config/$_K8S_APP_NAME/$BUILD_ID/suggested
          gsutil cp -r output/expanded gs://$_OUTPUT_BUCKET_PATH/config/$_K8S_APP_NAME/$BUILD_ID/expanded
        fi
    id: Save configs
    entrypoint: sh
  - name: gcr.io/cloud-builders/gke-deploy
    args:
      - apply
      - '--filename=output/expanded'
      - '--cluster=$_GKE_CLUSTER'
      - '--location=$_GKE_LOCATION'
      - '--namespace=$_K8S_NAMESPACE'
    id: Apply deploy
images:
  - '$_IMAGE_NAME:$COMMIT_SHA'
options:
  substitutionOption: ALLOW_LOOSE
substitutions:
  _IMAGE_NAME: gcr.io/resolute-land-274909/github_nagydominik_onlineretailer_partial
  _GKE_LOCATION: europe-west3-a
  _K8S_NAMESPACE: default
  _OUTPUT_BUCKET_PATH: resolute-land-274909_cloudbuild/deploy
  _K8S_APP_NAME: productapi
  _K8S_LABELS: ''
  _DOCKERFILE_DIR: ''
  _DOCKERFILE_NAME: Dockerfile_productapi
  _K8S_YAML_PATH: Kubernetes/productapi.yaml
  _GKE_CLUSTER: onlineretailer-cluster
  _K8S_ANNOTATIONS: gcb-trigger-id=cb8757e5-7dce-4aa9-bd43-6c4418f9dacb
tags:
  - gcp-cloud-build-deploy
  - $_K8S_APP_NAME

Cloud Source Repository tree

我知道“测试”步骤根本不起作用,只是失败了,我尝试了几种方法,但坦率地说,我不知道它的语法。

任何建议都非常欢迎!

最佳答案

可能的方法是使用docker-compose在构建步骤中运行测试,并从test_main服务(运行测试的服务容器)中获取退出代码。

- id: testingWithDockerCompose
  name: 'docker/compose:1.22.0'
  args: ['-f', 'docker-compose-test.yml', 'up', '--abort-on-container-exit', '--exit-code-from' , 'test_main']

要了解有关使用xUnit和docker-compose进行测试的更多信息,请阅读中级文章here

关于.net - 问:如何配置cloudbuild.yaml以运行测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61999426/

相关文章:

mysql - docker 组成 : spring boot connection to mysql database refused

git - 来自 docker 的 git 的主机 key 验证失败

javascript - Google Cloud Function 未处理的拒绝

c# - Log4Net - 如何添加仅用于特定代码部分的第二个记录器

c# - 如何在 Xamarin 中拍照?

opencv - 如何永久保存docker容器中的数据和软件?

google-cloud-platform - 当预期的关联事件不存在时触发警报

google-cloud-platform - Google Cloud Run 身份验证服务到服务

.net - 64 位 .net 上 Int32/Int64 操作的线程安全

c# - WPF:如何与非事件窗口交互?