为 aws codepipeline 中的每个拉取请求触发单元测试

Trigger unit test for every pull request in aws codepipeline

背景

我有一个 git 回购生物开发。目前在下面显示的 pipeline.yaml 文件中,我基本上是下载最新的代码,将其压缩并上传到 s3,然后再进行部署。

在其根目录中,我有一个名为 test_hello_world.py

的简单单元测试

目前我的管道 yaml 文件

# This describes an AWS "CodePipeline" -- an AWS continuous-deployment service
# A CodePipeline generated with this template will:
#  * subscribe to github push notifications on the bio-dev repo via a webhook
#  * when a commit is pushed to the specified branch:
#    * download the source code from that branch
#    * zip it up and copy it to the source-code S3 location for that branch

AWSTemplateFormatVersion: 2010-09-09
Parameters:
  RepositoryBranch:
    Type: String
    Description: >
      Branch of the bio-dev repository to monitor
  OAuthToken:
    Type: String
    Description: >
      OAuth Token for this code pipeline to connect to GitHub to download the source code
      when the webhook publishes a push event
    NoEcho: true

Resources:

  # NOTE: despite several Region properties, none of the elements of this Resource (or stack)
  # are region-specific -- S3 and IAM are global
  DeployFromGithubToS3CodePipeline:
    Type: AWS::CodePipeline::Pipeline
    Properties:
      Name: !Sub 'bio-dev-github-${RepositoryBranch}-to-s3'
      ArtifactStore:
        Location: 'source-code-for-download-by-ec2s'
        Type: S3
      RestartExecutionOnUpdate: true
      RoleArn: !ImportValue CodePipelineServiceRoleArn  # This is exported by the code_pipeline_role_and_policy.yaml stack
      Stages: 
        - Name: Source
          Actions:
            - Name: download_and_zip_code_from_github
              Region: !Ref "AWS::Region"
              ActionTypeId:
                Category: Source
                Owner: ThirdParty
                Version: 1
                Provider: GitHub
              Configuration:
                Owner: ProjectBatman
                Repo: 'bio-dev'
                PollForSourceChanges: false
                Branch: !Sub '${RepositoryBranch}'
                OAuthToken: !Sub '${OAuthToken}'
              RunOrder: 1
              InputArtifacts: []
              OutputArtifacts:
                - Name: zip_of_source_code
        - Name: Deploy
          Actions:
            - Name: copy_zip_of_source_code_to_s3
              Region: !Ref "AWS::Region"
              ActionTypeId:
                Category: Deploy
                Owner: AWS
                Version: 1
                Provider: S3
              Configuration:
                ObjectKey: !Sub 'BRANCHES/${RepositoryBranch}/repo.zip'  # ec2_init_user_data.sh depends on this, and there's a python abstraction to retrieve it in s3.py
                Extract: false
                BucketName: 'source-code-for-download-by-ec2s'
              RunOrder: 1
              InputArtifacts: 
                - Name: 'zip_of_source_code'
              OutputArtifacts: []

  AppPipelineWebhook:
    # TO DO: can all CodePipelines share a single github webhook, and filter to the branch-of-interest?
    # If not, every time we create a CodePipeline with CloudFormation, AWS creates another webhook
    # for the bio-dev repository, displayed here: https://github.com/ProjectBatman/bio-dev/settings/hooks
    Type: AWS::CodePipeline::Webhook
    Properties:
      Authentication: GITHUB_HMAC
      AuthenticationConfiguration:
        SecretToken: !Sub '${OAuthToken}'
      Filters:
        - 
          JsonPath: "$.ref"
          MatchEquals: !Sub 'refs/heads/${RepositoryBranch}'
      TargetPipeline: !Ref DeployFromGithubToS3CodePipeline
      TargetAction: download_and_zip_code_from_github
      Name: !Sub 'webhook-for-branch-${RepositoryBranch}'
      # NOTE: this appears to reference a "Version" property of the CodePipeline resource
      # But "Version" is not a valid property of an AWS::CodePipeline::Pipeline CloudFormation object
      # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codepipeline-pipeline.html
      # So I guess "Version" is managed dynamically by the CodePipeline service, and the reference in this webhook
      # automatically points to the latest version of the Pipeline
      TargetPipelineVersion: !GetAtt DeployFromGithubToS3CodePipeline.Version
      RegisterWithThirdParty: true

OBJECTIVE

理想情况下,我想 运行 对每个拉取请求进行此测试,但我不确定如何开始。

我通过浏览 aws 文档做了一些研究。据我了解,我需要创建一个 lambda 函数并将其添加为其中一个阶段的自定义操作。我理解正确还是我错了?

我很想听听所有的意见,因为我对 aws 还很陌生,而且我对 aws 上的信息不知所措,因为我找不到正确的入门方向。

I did a bit of research by going through aws documents. From what I understand is I would need to create a lambda function and add it as a custom action for one of the stages. Do I understand correctly or am i off?

不,这是不正确的。这比你想象的要容易。您不需要创建任何 Lambda 函数。

我注意到您没有在原始 post 中的任何地方提到 AWS CodeBuild。这是您缺少的概念。 AWS CodePipeline 并非旨在测试拉取请求。事实上,AWS CodePipeline 阶段通常包括 CodeBuild 作业。

AWS CodeBuild 将使用项目根目录下的配置文件 (buildspec.yaml),并将其用于 运行 构建流程、测试程序以及您真正想要的任何内容。 它会在每个 Pull Request create/update 时 运行 一个 CodeBuild 作业。 CodeBuild 将向 GitHub 报告测试是否通过。

可选:在 CodeBuild 执行结束时,您可以让它生成带有构建文件的 artifact.zip 并传递到 CodePipeline 的其他阶段以进行进一步处理。

这里有一个例子buildspec.yaml用于说明:

version: 0.2

phases:
  install:
    commands:
      - npm install
  pre_build:
    commands:
      - ./myscript.sh
  build:
    commands:
      - npm test
      - npm build

@solsglasses 的回答是正确的,因为您实际上想使用 CodeBuild 而不是 CodePipeline。为了 运行 每个 PR 的测试,您根本不需要 CodePipeline。

设置 CodeBuild 项目

  1. 创建 CodeBuild Project
    • 请务必将您的远程 git 存储库指定为来源(例如 GitHub、BitBucket 等)
    • 设置 environment variable 以授予 CodeBuild 访问存储库的权限(例如,对于 GitHub,您需要 GITHUB_TOKEN
  2. build specification 添加到您的 git 存储库

现在您可以跳转到 AWS 控制台并使用构建项目右上角的 Start Build 按钮手动触发构建。

根据拉取请求触发 CodeBuild

要在每次拉取请求时触发 CodeBuild 项目,您需要设置 Project Trigger with a Webhook Filter。你蜕皮可能需要 PULL_REQUEST_CREATEDPULL_REQUEST_UPDATED 过滤器,否则你的构建将触发对任何分支的任何推送,即使没有创建拉取请求(这将花费金钱并且没有提供任何价值)。

使用 CodePipeline

这部分是可选的,但通常与 CodeBuild 一起使用。

  1. 在推送到主分支时将 CodePipeline 配置为 运行
  2. 为您已经创建的 CodeBuild 项目添加一个阶段
  3. 更新您的构建规范文件以创建工件
  4. 将 CodeBuild 项目更新为 store the artifact in S3

这是您可以使用所需的任何工具触发部署的部分。该工具可以从 S3 中提取构建工件(只是一个普通的 zip 文件)并将其复制到您的服务器上,然后 运行 您的部署步骤。您可以通过 运行 某些脚本或单击您选择的部署应用程序中的按钮手动触发此操作,或者如果您使用 supported deployment service.[=20=,您可以配置 CodePipeline 为您执行部署]