【问题标题】:Terraform: AWS Codepipeline multiple Codecommit sourcesTerraform:AWS Codepipeline 多个 Codecommit 源
【发布时间】:2021-11-13 01:37:57
【问题描述】:

我正在从 Github.com 转移到 Codecommit,多年来我一直在利用 terraforms 模块化方法将 GitHub 存储库作为模块导入。也就是说,Codecommit 在本质上是非常不同的。我已经看到人们在哪里利用 SSH 在本地克隆存储库,但我也注意到 codepipeline 可以利用多个来源。我需要一种将多个 repos 添加到我的管道的方法,以便我可以复制 terraform 提供的模块化 github 方法。我希望该代码在本地以模块化方式执行。

我在谷歌上搜索了一个示例,该示例向我展示了如何在我的管道中利用多个 codecommmit 资源,但我找不到任何清楚地概述如何在 terraform 中利用多个资源的内容。有没有人弄清楚这一点或有他们可以指出我的例子?

【问题讨论】:

  • docs.aws.amazon.com/codebuild/latest/userguide/… - 在 Google 上很容易找到。 CodePipeline 专注于使用 AWS 原生结构进行部署,因此没有太多关于将它与 Terraform 一起使用的信息对我来说并不奇怪。从本质上讲,您将使用 Codebuild 项目来调用 Terraform,因此最好建议您专注于如何将 CodeBuild 项目与 CodePipeline 一起使用(在提供的示例中具有多个输入源),并理解这取决于您做什么在那些 Codebuild 项目中
  • 注意如何使用多个输入工作目录,它们不是嵌套的,例如 $CODEBUILD_SRC_DIR 和 $CODEBUILD_SRC_DIR_source2
  • @ronan 感谢您的回复。我较早地发现了该链接,并通过 terraform 资源构建了第二个源。非常感谢目录上的提醒会对此进行调查。
  • 不用担心。也许我误解了?您是使用 Terraform 创建管道还是运行 Terraform 的管道 - 或两者兼而有之?
  • 很棒的评论,我两者都做了,我创建了 terraform 来创建管道和构建项目。如果您想看到它,请告诉我,那部分工作得很好。我正在努力的部分是让 terraform 创建第二个资源,在这种情况下,它是另一个代码提交 repo 到源代码中。让我知道这是否可以为您解决问题。

标签: terraform aws-codepipeline aws-codecommit


【解决方案1】:
#    _____  ____  _    _ _____   _____ ______ 
#   / ____|/ __ \| |  | |  __ \ / ____|  ____|
#  | (___ | |  | | |  | | |__) | |    | |__   
#   \___ \| |  | | |  | |  _  /| |    |  __|  
#   ____) | |__| | |__| | | \ \| |____| |____ 
#  |_____/ \____/ \____/|_|  \_\\_____|______|
                                            
                                            
      Stages:
        - Name: Source
          Actions:
            - ActionTypeId:
                Category: Source
                Owner: AWS
                Provider: CodeStarSourceConnection
                Version: "1"
              Configuration: 
                ConnectionArn: !Ref CodeStarConnectionArn
                FullRepositoryId: !Ref BitBucketRepo
                BranchName: !Ref BitBucketRepoReleaseBranch
                OutputArtifactFormat: "CODE_ZIP"
                DetectChanges: true
              Name: SourceCode
              OutputArtifacts:
                - Name: !Sub ${SourceArtifactName}
              Namespace: SourceVariables1
              RunOrder: 1
            - ActionTypeId:
                Category: Source
                Owner: AWS
                Provider: CodeStarSourceConnection
                Version: "1"
              Configuration: 
                ConnectionArn: !Ref CodeStarConnectionArn
                FullRepositoryId: !Ref PipelineBitBucketRepo
                BranchName: !Ref PipelineBitBucketRepoReleaseBranch
                OutputArtifactFormat: "CODE_ZIP"
                DetectChanges: true
              Name: PipelineDefinition
              OutputArtifacts:
                - Name: !Sub ${PipelineCodeArtifactName}
              Namespace: SourceVariables2
              RunOrder: 1



#    _____ ______ _      ______   __  __ _    _ _______    _______ ______ 
#   / ____|  ____| |    |  ____| |  \/  | |  | |__   __|/\|__   __|  ____|
#  | (___ | |__  | |    | |__    | \  / | |  | |  | |  /  \  | |  | |__   
#   \___ \|  __| | |    |  __|   | |\/| | |  | |  | | / /\ \ | |  |  __|  
#   ____) | |____| |____| |      | |  | | |__| |  | |/ ____ \| |  | |____ 
#  |_____/|______|______|_|      |_|  |_|\____/   |_/_/    \_\_|  |______|
                                                                        
                                                                        


        - !If
          - ShouldUpatePipelineStackOnChange
          - Name: UpdatePipeline

            Actions:
              - Name: CreateChangeSet
                ActionTypeId:
                  Category: Deploy
                  Owner: AWS
                  Provider: CloudFormation
                  Version: "1"
                Configuration:
                  ActionMode: CHANGE_SET_REPLACE
                  StackName: !Ref AWS::StackName
                  ChangeSetName: !Sub ${AWS::StackName}-ChangeSet
                  TemplatePath: !Sub ${PipelineCodeArtifactName}::${PipelineTemplateName}
                  Capabilities: CAPABILITY_NAMED_IAM
                  RoleArn: !GetAtt PipelineStackCloudFormationExecutionRole.Arn
                InputArtifacts:
                  - Name: !Sub ${PipelineCodeArtifactName}
                RunOrder: 1
              - Name: ExecuteChangeSet
                ActionTypeId:
                  Category: Deploy
                  Owner: AWS
                  Provider: CloudFormation
                  Version: "1"
                Configuration:
                  ActionMode: CHANGE_SET_EXECUTE
                  StackName: !Ref AWS::StackName
                  ChangeSetName: !Sub ${AWS::StackName}-ChangeSet
                  RoleArn: !GetAtt PipelineStackCloudFormationExecutionRole.Arn
                OutputArtifacts:
                  - Name: !Sub ${AWS::StackName}ChangeSet
                RunOrder: 2
          - !Ref AWS::NoValue

【讨论】:

  • Ronan 让我检查一下,看看我是如何工作的。将与您取得进展。谢谢你。
【解决方案2】:

对此,我发现它在任何地方都没有很好的记录,这实际上非常令人沮丧。利用 hashcorp 对服务的模糊描述和 AWS 多输入示例,我终于能够为 terraform 想出这个:

 "aws_codepipeline" "foo" {
  name     = "tf-test-pipeline"
  role_arn = "codepipeline service role arn"

  artifact_store {
    location = "s3 bucket name, NOT THE ARN"
    type     = "S3"
  }

  stage {
    name = "Source"

    action {
      name             = "Source"
      category         = "Source"
      owner            = "AWS"
      provider         = "CodeCommit"
      version         = "1"
      output_artifacts = ["src"]

      configuration = {
        RepositoryName = "vpc" //MUST BE the name of the your codecommit repo
        BranchName = "master"
      }

      run_order = "1"
    }

    action {
      name             = "2ndSource" //you can make this any name
      category         = "Source"
      owner            = "AWS"
      provider         = "CodeCommit"
      version         = "1"
      output_artifacts = ["src2"]

      configuration = {
        RepositoryName = "ec2" 
        BranchName = "master"
      }
      run_order = "2"
    }


  }


  stage {
    name = "Build"

    action {
      name            = "Build"
      category        = "Build"
      owner           = "AWS"
      provider        = "CodeBuild"
      input_artifacts = ["src","src2"] //pass through both repositories
      version         = "1"

      configuration = {
        ProjectName = "codebuild_project_name"
        PrimarySource = "Source"
      }
    }
  }
}

这里的诀窍是在一个阶段中添加额外的资源,而不是单独的。下面的参考资料显示了其中两个,但我可以毫无问题地添加三个。

参考链接:

Hashicorp 代码管道 https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/codepipeline#run_order

AWS 多输入 JSON 示例: https://docs.aws.amazon.com/codebuild/latest/userguide/sample-pipeline-multi-input-output.html

对于那些第一次开始的人,我推荐这个链接,它非常全面,可以引导你完成整个构建过程,包括角色和策略: https://medium.com/swlh/intro-to-aws-codecommit-codepipeline-and-codebuild-with-terraform-179f4310fe07

【讨论】:

    猜你喜欢
    • 2019-05-02
    • 1970-01-01
    • 2017-12-12
    • 2018-03-16
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    相关资源
    最近更新 更多