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
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!