【问题标题】:Trigger unit test for every pull request in aws codepipeline在 aws codepipeline 中为每个拉取请求触发单元测试
【发布时间】:2020-03-28 08:13:48
【问题描述】:

背景

我有一个 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 的信息感到不知所措,因为我无法找到正确的入门方向。

【问题讨论】:

    标签: git amazon-web-services amazon-s3 aws-codepipeline


    【解决方案1】:

    我通过浏览 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
    

    【讨论】:

    • 是的,这有帮助,但是每次运行时我如何触发我的 test.py 文件?
    • 我现在意识到您想谈论 CodeBuild,而不是 CodePipeline。这两个服务的名称很容易混淆。我编辑了我的帖子。再看看。
    • stackoverflow.com/questions/59256454/…我发布了另一个问题,您可能已经回答了
    • @solsglasses 您的答案的粗体部分记录在哪里:“CodeBuild 将向 GitHub 报告测试是否通过”?
    【解决方案2】:

    @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 TriggerWebhook Filter。您可能需要 PULL_REQUEST_CREATEDPULL_REQUEST_UPDATED 过滤器,否则即使没有创建拉取请求,您的构建也会在任何推送到任何分支时触发(这将花费金钱并且没有任何价值)。

    使用 CodePipeline

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

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

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

    【讨论】:

    • 有道理,我有一个问题,如果我通过 aws 控制台 ui 手动进行设置,有没有办法让我获取 YAML 文件,然后我可以通过云形成进行设置?
    • 对不起@Dinero,我不知道你是怎么做到的。希望您能找到解决方案!
    • stackoverflow.com/questions/59256454/…我发布了另一个问题,你可能已经回答了
    猜你喜欢
    • 2020-02-23
    • 1970-01-01
    • 2018-12-23
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 2021-05-16
    相关资源
    最近更新 更多