如何编写CI/CD管道以在Google kubernetes集群上运行java微服务的集成测试?

如何编写CI/CD管道以在Google kubernetes集群上运行java微服务的集成测试?,kubernetes,google-cloud-platform,google-kubernetes-engine,bitbucket-pipelines,google-cloud-build,Kubernetes,Google Cloud Platform,Google Kubernetes Engine,Bitbucket Pipelines,Google Cloud Build,背景: 我在GKE集群中有8-9个基于spring的私有集群微服务。所有的微服务都捆绑了集成测试。我使用bitbucket和maven作为构建工具 所有的微服务都通过rest调用相互通信,url为:http://:8080/rest/api/fetch 要求:我已经准备好了测试环境,所有docker映像都在GKE测试集群上。我希望在将代码合并到service-A的master之后,管道应该将映像部署到tes env并运行集成测试用例。如果测试用例通过,它应该部署到QA环境中,否则将service

背景: 我在GKE集群中有8-9个基于spring的私有集群微服务。所有的微服务都捆绑了集成测试。我使用bitbucket和maven作为构建工具

所有的微服务都通过rest调用相互通信,url为:http://:8080/rest/api/fetch

要求:我已经准备好了测试环境,所有docker映像都在GKE测试集群上。我希望在将代码合并到service-A的master之后,管道应该将映像部署到tes env并运行集成测试用例。如果测试用例通过,它应该部署到QA环境中,否则将service-A的映像回滚到上一个映像

问题:在每次代码合并到master时,我都能够运行service-A的JUNIT测试用例,构建其docker映像,将其推送到GCR上,并将其部署到测试环境集群上。但是,如果集成测试用例失败,如何在部署后触发集成测试用例并回滚到以前部署的映像?有办法吗


TIA

有很多方法可以做到这一点。根据以上信息,不清楚您正在使用哪个构建工具

  • 假设您使用的是竹子,那么您可以为竹子创建一个任务,并将其包含在SDLC过程中。大多数情况下,任务可以有竹文或易写文字

  • 您还可以创建一个单独的shell脚本,以便在部署后运行集成测试套件


  • 您可能应该检查提供的内容。
    Tekton Pipelines项目为声明CI/CD样式的管道提供了k8s样式的资源。

    如果使用Gitlab CICD,可以按如下方式中断阶段:

    You can create different steps for each part:
    
    pipelines:
      branches:
        BRANCH_NAME:
        - step:
            script: 
              - BUILD
        - step:
            script: 
              - DEPLOY
        - step:
            script: 
              - First set of JUNIT test
        - step:
            script:
              - Run Integration Tests (Here you can add if you fail to do rollback)
            script:
              - Upload to QA
    
    stages:
     - compile
     - build
     - test
     - push
     - review
     - deploy
    
    您应该在第一阶段编译代码,然后在下一阶段从中构建docker映像,然后提取映像并运行它们来执行所有测试(包括集成测试)

    以下是它的外观模型:

    compile-stage:
      stage: compile
      script:
        - echo 'Compiling Application'
        # - bash my compile script
      # Compile artifacts can be used in the build stage.
      artifacts:
        paths:
        - out/dist/dir
        expire_in: 1 week
    
    build-stage:
      stage: build
      script:
        - docker build . -t "${CI_REGISTRY_IMAGE}:testversion" ## Dockerfile should make use of out/dist/dir
        - docker push "${CI_REGISTRY_IMAGE}:testversion"
    
    test-stage1:
       stage: test
       script:
         - docker run -it ${CI_REGISTRY_IMAGE}:testversion bash unit_test.sh
    
    test-stage2:
       stage: test
       script:
         - docker run -d ${CI_REGISTRY_IMAGE}:testversion
         - ./integeration_test.sh
    
    ## You will only push the latest image if the build will pass all the tests.
    push-stage:
       stage: push
       script:
         - docker pull ${CI_REGISTRY_IMAGE}:testversion
         - docker tag ${CI_REGISTRY_IMAGE}:testversion -t ${CI_REGISTRY_IMAGE}:latest
         - docker push ${CI_REGISTRY_IMAGE}:latest
    
    ## An app will be deployed on staging if it has passed all the tests.
    ## The concept of CICD is generally that you should do all the automated tests before even deploying on staging. Staging can be used for User Acceptance and Quality Assurance Tests etc.
    deploy-staging:
      stage: review
      environment:
         name: review/$CI_BUILD_REF_NAME
         url: https://$CI_ENVIRONMENT_SLUG.$KUBE_INGRESS_BASE_DOMAIN
         on_stop: stop_review
      only:
        - branches
      script:
         - kubectl apply -f deployments.yml
    
    ## The Deployment on production environment will be manual and only when there is a version tag committed.
    deploy-production:
      stage: deploy
      environment:
         name: prod
         url: https://$CI_ENVIRONMENT_SLUG.$KUBE_INGRESS_BASE_DOMAIN
      only:
        - tags
      script:
         - kubectl apply -f deployments.yml
      when:
       - manual
    

    我希望上面的片段能对你有所帮助。如果您想了解更多有关使用

    部署微服务的信息,我们将使用maven构建