AWS CloudFormation CodePipeline:无法从GitHub获取存储库的内容

时间:2019-03-07 04:17:52

标签: github amazon-cloudformation aws-codepipeline github-webhook

我正在尝试使用CodePipeline和GitHub设置AWS CloudFormation配置。

我在自己的示例项目和教程Create a GitHub Pipeline with AWS CloudFormation中都失败了。

所有资源都已创建,但是在CodePipeline中,我在初始“源”阶段不断收到以下错误。

Could not fetch the contents of the repository from GitHub.

请参见下图:

CodePipeline with Github failing with "Could not fetch the contents of the repository from Github."

请注意,这不是权限错误。到现在为止,谷歌还没有其他东西。

如果我停止使用CloudFormation并通过控制台创建CodePipeline,可以将GitHub配置为可以工作,但是出于我的目的,我需要使用CloudFormation。需要坚持使用模板。

这是从教程复制的CloudFormation模板中的模板:

Parameters:
  BranchName:
    Description: GitHub branch name
    Type: String
    Default: master
  RepositoryName:
    Description: GitHub repository name
    Type: String
    Default: test
  GitHubOwner:
    Type: String
  GitHubSecret:
    Type: String
    NoEcho: true
  GitHubOAuthToken:
    Type: String
    NoEcho: true
  ApplicationName:
    Description: CodeDeploy application name
    Type: String
    Default: DemoApplication
  BetaFleet:
    Description: Fleet configured in CodeDeploy
    Type: String
    Default: DemoFleet
Resources:
  CodePipelineArtifactStoreBucket:
    Type: "AWS::S3::Bucket"
  CodePipelineArtifactStoreBucketPolicy:
    Type: "AWS::S3::BucketPolicy"
    Properties:
      Bucket: !Ref CodePipelineArtifactStoreBucket
      PolicyDocument:
        Version: 2012-10-17
        Statement:
          - Sid: DenyUnEncryptedObjectUploads
            Effect: Deny
            Principal: "*"
            Action: "s3:PutObject"
            Resource: !Join
              - ""
              - - !GetAtt
                  - CodePipelineArtifactStoreBucket
                  - Arn
                - /*
            Condition:
              StringNotEquals:
                "s3:x-amz-server-side-encryption": "aws:kms"
          - Sid: DenyInsecureConnections
            Effect: Deny
            Principal: "*"
            Action: "s3:*"
            Resource: !Join
              - ""
              - - !GetAtt
                  - CodePipelineArtifactStoreBucket
                  - Arn
                - /*
            Condition:
              Bool:
                "aws:SecureTransport": false
  AppPipelineWebhook:
    Type: "AWS::CodePipeline::Webhook"
    Properties:
      Authentication: GITHUB_HMAC
      AuthenticationConfiguration:
        SecretToken: !Ref GitHubSecret
      Filters:
        - JsonPath: $.ref
          MatchEquals: "refs/heads/{Branch}"
      TargetPipeline: !Ref AppPipeline
      TargetAction: SourceAction
      Name: AppPipelineWebhook
      TargetPipelineVersion: !GetAtt
        - AppPipeline
        - Version
      RegisterWithThirdParty: true
  AppPipeline:
    Type: "AWS::CodePipeline::Pipeline"
    Properties:
      Name: github-events-pipeline
      RoleArn: !GetAtt
        - CodePipelineServiceRole
        - Arn
      Stages:
        - Name: Source
          Actions:
            - Name: SourceAction
              ActionTypeId:
                Category: Source
                Owner: ThirdParty
                Version: 1
                Provider: GitHub
              OutputArtifacts:
                - Name: SourceOutput
              Configuration:
                Owner: !Ref GitHubOwner
                Repo: !Ref RepositoryName
                Branch: !Ref BranchName
                OAuthToken: !Ref GitHubOAuthToken
                PollForSourceChanges: false
              RunOrder: 1
        - Name: Beta
          Actions:
            - Name: BetaAction
              InputArtifacts:
                - Name: SourceOutput
              ActionTypeId:
                Category: Deploy
                Owner: AWS
                Version: 1
                Provider: CodeDeploy
              Configuration:
                ApplicationName: !Ref ApplicationName
                DeploymentGroupName: !Ref BetaFleet
              RunOrder: 1
      ArtifactStore:
        Type: S3
        Location: !Ref CodePipelineArtifactStoreBucket
  CodePipelineServiceRole:
    Type: "AWS::IAM::Role"
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - codepipeline.amazonaws.com
            Action: "sts:AssumeRole"
      Path: /
      Policies:
        - PolicyName: AWS-CodePipeline-Service-3
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action:
                  - "codecommit:CancelUploadArchive"
                  - "codecommit:GetBranch"
                  - "codecommit:GetCommit"
                  - "codecommit:GetUploadArchiveStatus"
                  - "codecommit:UploadArchive"
                Resource: "*"
              - Effect: Allow
                Action:
                  - "codedeploy:CreateDeployment"
                  - "codedeploy:GetApplicationRevision"
                  - "codedeploy:GetDeployment"
                  - "codedeploy:GetDeploymentConfig"
                  - "codedeploy:RegisterApplicationRevision"
                Resource: "*"
              - Effect: Allow
                Action:
                  - "codebuild:BatchGetBuilds"
                  - "codebuild:StartBuild"
                Resource: "*"
              - Effect: Allow
                Action:
                  - "devicefarm:ListProjects"
                  - "devicefarm:ListDevicePools"
                  - "devicefarm:GetRun"
                  - "devicefarm:GetUpload"
                  - "devicefarm:CreateUpload"
                  - "devicefarm:ScheduleRun"
                Resource: "*"
              - Effect: Allow
                Action:
                  - "lambda:InvokeFunction"
                  - "lambda:ListFunctions"
                Resource: "*"
              - Effect: Allow
                Action:
                  - "iam:PassRole"
                Resource: "*"
              - Effect: Allow
                Action:
                  - "elasticbeanstalk:*"
                  - "ec2:*"
                  - "elasticloadbalancing:*"
                  - "autoscaling:*"
                  - "cloudwatch:*"
                  - "s3:*"
                  - "sns:*"
                  - "cloudformation:*"
                  - "rds:*"
                  - "sqs:*"
                  - "ecs:*"
                Resource: "*"

我已采取以下步骤:

  • 提供的Github组织,回购和分支机构
  • 在GitHub上设置个人访问令牌,并将其提供给模板GitHubOAuthToken参数,以访问repo:alladmin:repo_hook
  • 设置随机字符串并将其提供给GitHubSecret
  • 尝试不像其他许多示例一样包含GitHubSecret
  • 已验证我所在地区的AWS CodePipeline已在Github应用程序的“授权OAuth应用程序”下列出

为了尝试从一个清晰的表盘开始,我还做了以下操作:

  • 在启动aws codepipeline list-webhooksaws codepipeline delete-webhook --name之前清除了所有GitHub webhooks
  • 添加了新的个人访问令牌
  • 尝试了多个存储库和分支机构

有什么想法可以使GitHub与CloudFormation和CodePipeline一起使用吗?

1 个答案:

答案 0 :(得分:0)

找到了解决方案。 Github组织名称区分大小写。