首页 > 解决方案 > 在 aws codepipeline 中为每个拉取请求触发单元测试

问题描述

背景

我有一个 git repo bio-dev。目前在下面显示的我的 pipeline.yaml 文件中,我基本上下载了我最新的代码 zip 并将其上传到 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

客观的

理想情况下,我想在每个拉取请求上运行这个测试,但我不知道如何开始。

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

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

标签: gitamazon-web-servicesamazon-s3aws-codepipeline

解决方案


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

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

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

AWS CodeBuild 将使用您项目根目录中的配置文件 ( buildspec.yaml) 并使用它来运行构建过程、测试过程以及您真正想要的任何东西。它将在每次拉取请求创建/更新时运行 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

推荐阅读