简体   繁体   中英

Q: How to configure cloudbuild.yaml to run tests?

I'd like to set up a fully automated CI/CD pipeline on GCP. In Cloud Build, I already have a cloudbuild.yaml which build the Dockerfile and push the built Docker Image to the Container Registry. However, I couldn't find a solution to run the xUnit tests before building the Dockerfile, and if any of the test fails the Cloud Build should not build the Dockerfile and exit with an error/exception.

The cloudbuild.yaml I'm currently using:

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

I know that the Test step is not working at all, it just fails, I've tried several ways but to be frank, I do not know the syntax of it.

Any suggestion is very welcomed!

The possible way to do that is using docker-compose to run your test in a build step and take the exit code from the test_main service (the service container that run your tests).

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

To know more about testing with xUnit and docker-compose , read the medium article here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM