首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >aws代码中每个拉请求的触发单元测试

aws代码中每个拉请求的触发单元测试
EN

Stack Overflow用户
提问于 2019-12-03 15:38:09
回答 2查看 2.1K关注 0票数 1

背景

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

在根目录中,我有一个名为test_hello_world.py的简单单元测试。

我的管道yaml文件当前

代码语言:javascript
复制
# 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和我是不知所措的信息,因为我无法找到正确的方向开始。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-03 16:28:05

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

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

我注意到您在最初的文章中没有提到AWS CodeBuild。这是你错过的概念。AWS CodePipeline不是为测试拉请求而设计的。事实上,AWS CodePipeline阶段通常包括CodeBuild作业。

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

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

下面是一个示例buildspec.yaml以供说明:

代码语言:javascript
复制
version: 0.2

phases:
  install:
    commands:
      - npm install
  pre_build:
    commands:
      - ./myscript.sh
  build:
    commands:
      - npm test
      - npm build
票数 4
EN

Stack Overflow用户

发布于 2019-12-04 22:00:49

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

建立一个CodeBuild项目

  1. 创建一个CodeBuild项目
    • 一定要指定您的远程git作为源(例如。( GitHub、BitBucket等)
    • 环境变量设置为允许CodeBuild访问回购(例如。对于GitHub,您需要GITHUB_TOKEN

  1. 构建规范添加到git中

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

在拉请求中触发CodeBuild

要在每个拉请求中触发CodeBuild项目,您需要使用一个Webhook滤波器来设置一个项目触发器。您可能不需要PULL_REQUEST_CREATEDPULL_REQUEST_UPDATED过滤器,否则您的构建将触发对任何分支的任何推送,即使没有创建拉请求(这将花费金钱和没有提供任何价值)。

使用CodePipeline

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

  1. 配置CodePipeline以继续推送到主分支
  2. 为您已经创建的CodeBuild项目添加一个舞台
  3. 更新buildspec文件以创建工件
  4. 将CodeBuild项目更新为将工件存储在S3中

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59160553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档