Git aws代码管道中每个pull请求的触发单元测试

Git aws代码管道中每个pull请求的触发单元测试,git,amazon-web-services,amazon-s3,aws-codepipeline,Git,Amazon Web Services,Amazon S3,Aws Codepipeline,背景 我有一个git repo bio-dev。目前在下面显示的pipeline.yaml文件中,我基本上下载了我最新的代码zip,并将其上传到s3,然后部署 在它的根目录中,我有一个名为test\u hello\u world.py的简单单元测试 当前我的管道yaml文件 # This describes an AWS "CodePipeline" -- an AWS continuous-deployment service # A CodePipeline generated with t

背景

我有一个git repo bio-dev。目前在下面显示的pipeline.yaml文件中,我基本上下载了我最新的代码zip,并将其上传到s3,然后部署

在它的根目录中,我有一个名为test\u hello\u 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上的信息淹没了,因为我无法找到正确的开始方向

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

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

我注意到你在原来的帖子中没有提到AWS代码构建。这就是你所缺少的概念。AWS代码管道不是为测试拉请求而设计的。事实上,AWS代码管道阶段通常包括代码构建作业

AWS CodeBuild将使用项目根目录下的配置文件(
buildspec.yaml
),并使用它来运行构建过程、测试过程,以及您真正想要的任何东西它将在每次请求创建/更新时运行代码构建作业。CodeBuild将向GitHub报告测试是否通过。

可选:在代码构建执行结束时,您可以让它与构建文件一起生成一个
artifact.zip
,并传递到代码管道的其他阶段进行进一步处理

下面是一个示例
buildspec.yaml
,用于说明:

version: 0.2

phases:
  install:
    commands:
      - npm install
  pre_build:
    commands:
      - ./myscript.sh
  build:
    commands:
      - npm test
      - npm build
我通过查阅aws文件做了一些研究。据我所知,我需要创建一个lambda函数,并将其添加为其中一个阶段的自定义操作。我理解正确吗?还是我错了

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

我注意到你在原来的帖子中没有提到AWS代码构建。这就是你所缺少的概念。AWS代码管道不是为测试拉请求而设计的。事实上,AWS代码管道阶段通常包括代码构建作业

AWS CodeBuild将使用项目根目录下的配置文件(
buildspec.yaml
),并使用它来运行构建过程、测试过程,以及您真正想要的任何东西它将在每次请求创建/更新时运行代码构建作业。CodeBuild将向GitHub报告测试是否通过。

可选:在代码构建执行结束时,您可以让它与构建文件一起生成一个
artifact.zip
,并传递到代码管道的其他阶段进行进一步处理

下面是一个示例
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上运行测试,您根本不需要代码管道

建立一个代码构建项目
  • 创建一个
    • 确保将远程git repo指定为源(例如GitHub、BitBucket等)
    • 设置一个用于授予代码构建对回购的访问权限(例如,对于GitHub,您需要
      GitHub\u令牌
  • 向您添加git repo
  • 现在,您可以跳入AWS控制台,使用构建项目右上角的
    Start build
    按钮手动触发构建

    在拉请求上触发代码构建 要在每个拉请求上触发CodeBuild项目,您需要使用设置。您可能不希望创建
    pull\u请求
    和更新
    pull\u请求
    过滤器,否则您的构建将在推送到任何分支时触发,即使没有创建拉请求(这将花费金钱并且没有任何价值)

    使用代码管道 这部分是可选的,但通常与代码构建一起使用

  • 将CodePipeline配置为在到主分支的推送上运行
  • 为您已经创建的代码构建项目添加一个阶段
  • 更新buildspec文件以创建工件
  • 将CodeBuild项目更新为

  • 这是您可以使用任何工具触发部署的部分从S3将其复制到服务器上,然后运行部署步骤。您可以通过运行一些脚本或单击所选部署应用程序中的按钮手动触发此操作,或者,如果您使用了,您可以配置CodePipeline来为您进行部署。

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

    建立一个代码构建项目
  • 创建一个
    • 确保将远程git repo指定为源(例如GitHub、BitBucket等)
    • 设置一个用于授予代码构建对回购的访问权限(例如,对于GitHub,您需要
      GitHub\u令牌
  • 向您添加git repo
  • 现在,您可以跳入AWS控制台,使用构建项目右上角的
    Start build
    按钮手动触发构建

    在拉请求上触发代码构建 要在每次拉取请求时触发CodeBuild项目,您需要使用设置。您可能希望创建
    pull\u请求
    并更新
    pull\u请求
    过滤器,否则您的构建将