可以基于CodeGuru分析(审查/分析器)来“失败”/“中止”管道吗?

huangapple go评论96阅读模式
英文:

Is it possible to 'FAIL'/'Abort' a pipeline based on CodeGuru analysis (review/profiler)?

问题

Can this be done with AWS CodeGuru as well? Can we define some kind of gate in AWS CodePipeline that can see the quality outcome from CodeGuru to decide on the fate of the pipeline progress?

英文:

When we use SonarQube, we can define rigor and a gate in Sonar. This helps to fail or abort a pipeline if the quality gate is not achieved.

Can this be done with AWS CodeGuru as well? Can we define some kind of gate in AWS CodePipeline that can see the quality outcome from CodeGuru to decide on the fate of the pipeline progress?

答案1

得分: 1

是的,这是可能的。尽管目前 AWS CodePipeline 和 Amazon CodeGuru 之间没有直接的集成,但可以通过服务 API 提供的功能来实现。

CodeGuru 用法

在讨论如何实现之前,重要的是确保我们在何时和何地应该在您的开发过程中使用 CodeGuru 方面达成一致。当我们开展新项目或现有项目时,我们寻求对我们的代码进行验证(是否符合安全最佳实践,是否实现业务目标等),然后将其合并到我们的主分支之前。这意味着一旦代码进入主分支,它可以在我们的环境中继续前进,进一步使用自动化来进行集成测试,以确定功能是否按预期运行。我们将这种软件交付方式称为 主干开发

这就是我们希望在此处使用 CodeGuru Reviewer 来防止不符合组织标准的代码进入交付流程。一旦合并了该代码,它将变得昂贵,需要使用补丁和回滚来允许其他部署的交付。因此,CodeGuru Reviewer 将用于通知审阅人员在建议已解决之前不予批准。

另一方面,CodeGuru Profiler 用于验证正在运行的工作负载。这创建了一个反馈循环,可以进一步微调应用程序的性能。在本答案的其余部分,我们将重点关注 CodeGuru Reviewer。

CodePipeline 集成

在 AWS 中,组织看到的核心优势之一是可以通过直接的 HTTP 交互或通过 SDK 抽象在集成中使用的 API。CodeGuru 也不例外,您可以在控制台中执行的许多操作都可以在 官方 SDK 中找到。

鉴于每个人的交付流程可能会有所不同,具有在某个阶段调用自定义逻辑的能力可以帮助调整流程以满足您的需求,即在这种情况下触发 CodeGuru Reviewer 分析。这通过集成 AWS Lambda 函数来实现,这是 CodePipeline 中的 受支持操作。通过这个 Lambda,您可以通过使用计算和编排,比如 AWS Step Functions 状态机或 AWS CodeBuild 项目,触发一个脚本(或一系列脚本)。

一旦工作流开始,接下来的任务是按正确的顺序触发正确的 API 调用。为了简化,以下 API 调用可用于获取您正在寻找的结果:

  • 要触发新的代码审查,可以使用 CreateCodeReview,或者要查找以前的代码审查,可以使用 ListCodeReviews
  • 使用 DescribeCodeReview 了解代码审查的状态,即是否已完成。您可以每分钟轮询此 API,以确定是否已进入“已完成”状态。
  • 使用 ListRecommendations 获取审查的输出。您可以在此处添加一些业务逻辑,以排除特定严重性或包括特定严重性的数量阈值。

通过您自己处理的结果,最后一步是通过传递原始 Lambda 函数中指定的 JobId 返回以下两个操作之一到 CodePipeline。

通过这个过程,流程现在将根据您的 CodeGuru Reviewer 的建议而继续或继续。这种方法使您能够根据组织的需求自定义工作流,同时利用现有的集成和 SDK 来处理一些繁重的工作。

英文:

Yes this is possible. Though today there is no direct integration between AWS CodePipeline and Amazon CodeGuru, this can be achieved through the functionality exposed via the services APIs.

CodeGuru Usage

Before discussing on how we would want to implement this, it’s important to ensure that we are aligned on when and where CodeGuru should be leveraged in your development experience. When we work on building new or existing projects we look for validation of our code (does it match security best practices, does it fulfil the business objective etc) before it is merged into our mainline branch. The idea is that once it has made its way into the mainline branch it can progress along our environments with further automation used to identify through integration testing whether the functionality operates as expected. We call this style of software delivery trunk-based development.

This is where we want to be using the CodeGuru Reviewer to assist in preventing code making its way into the delivery pipeline that does not match your organisations standards. Once that code is merged in it becomes expensive, with patching and rollbacks being used as mechanisms to allow the delivery of others deployments. Therefore CodeGuru Reviewer would act to inform the reviewers not to approve before recommendations have been resolved.

On the other hand, CodeGuru Profiler provides validation of workloads that are operating. This creates a feedback loop of where adjustments could be made to further fine-tune your applications performance. For the remainder of this answer we will focus on CodeGuru Reviewer.

CodePipeline Integration

With services in AWS, one of the core benefits that organisations see are the exposure of APIs that can be integrated either directly via HTTP interaction or abstracted in an SDK. CodeGuru is no different, many of the actions that you can perform in the console are available within the official SDKs that are published.

Given that everyones delivery pipelines can vary, having the ability to call custom logic in a stage can help to tailor the pipeline to meet your requirements which in this scenario are to trigger a CodeGuru Reviewer analysis. This is achieved through the integration of an AWS Lambda function which is a supported action within CodePipeline. From this Lambda you could trigger a script (or series of scripts) through the usage of compute and orchestration such as either an AWS Step Functions state machine or AWS CodeBuild project.

Once the workflow takes places, it then becomes a task to trigger the correct API calls in the correct order. For simplicity the following API calls can be used to get the results you are looking for:

  • Either CreateCodeReview if you want to trigger a new one, or ListCodeReviews if you want to find a previous one.
  • DescribeCodeReview to understand the status of the code review, i.e. has it completed. You could poll this API once a minute to determine whether it has entered the “Completed” status.
  • ListRecommendations to get the output of the review. You might add some business logic here to discount particular severities or to include a threshold for how many of a particular severity are allowed.

With the results processed by yourself, the final step is to return one of the two below actions to CodePipeline by passing in the JobId that was specified within the original Lambda function.

  • PutJobSuccessResult - Confirming to CodePipeline it can now resume with the remainder of its execution.
  • PutJobFailureResult - Instructing CodePipeline that the action failed, and therefore the pipeline should be halted.

Through this process, the pipeline will now either proceed or continue as a result of your CodeGuru Reviewers recommendation. This approach provides you with the flexibility to be able to tailor the workflow to your organisations needs, whilst leveraging existing integrations and SDKs to do some of the heavy lifting.

huangapple
  • 本文由 发表于 2023年3月7日 13:09:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定