CodePipelineでビルドした成果物をS3へあげる(Cloudformation)

  • 2020.05.23
  • AWS
CodePipelineでビルドした成果物をS3へあげる(Cloudformation)

タイトルの通り。作成してみたので、ブログに残しておきます。前提として、CodeBuildやCodePiplelineのIAMロール(S3へのアクセス権限の付与)などはされていること、CodeBuild上でartifactを生成するbuildspec.ymlは作成されているものとします。これらの情報は、検索すればたくさん出てくるはず。

AWSTemplateFormatVersion: '2010-09-09'
Description: CloudFormation template for pipeline.

Parameters:
  CodeBuildServiceRole:
    Description: IAM role for CodeBuild.
    Type: String
  CodePipelineServiceRole:
    Description: IAM role for CodePipeline.
    Type: String
  RepositoryName: 
    Type: String
    Description: Repository name
  BranchName:
    Description: CodeCommit branch name.
    Type: String
    Default: master
  DeployBucketName:
    Description: Deploy S3 bucket name.
    Type: String
  ArtifactBucketName:
    Description: Artifact S3 bucket name.
    Type: String

Resources:
  CodeBuild:
    Type: AWS::CodeBuild::Project
    Properties:
      Name: codebuild
      Artifacts:
        Type: CODEPIPELINE
      Description: codebuild
      Environment:
        ComputeType: BUILD_GENERAL1_MEDIUM
        Image: aws/codebuild/standard:4.0
        Type: LINUX_CONTAINER
        PrivilegedMode: True
      ServiceRole: !Sub arn:aws:iam::${AWS::AccountId}:role/${CodeBuildServiceRole}
      Source:
        Type: CODEPIPELINE
      TimeoutInMinutes: 30
      Cache:
        Type: LOCAL
        Modes:
          - LOCAL_DOCKER_LAYER_CACHE

  CodePipeline:
    Type: AWS::CodePipeline::Pipeline
    Properties:
      ArtifactStore:
        Location: !Ref ArtifactBucketName
        Type: S3
      Name: codepipeline
      RestartExecutionOnUpdate: false
      RoleArn: !Sub arn:aws:iam::${AWS::AccountId}:role/${CodePipelineServiceRole}
      Stages:
        - Name: Source
          Actions:
            - Name: CodeCommit
              ActionTypeId: 
                Category: Source
                Owner: AWS
                Provider: CodeCommit
                Version: 1
              OutputArtifacts: 
                - Name: source
              Configuration: 
                RepositoryName: !Ref RepositoryName
                BranchName: !Ref BranchName
                PollForSourceChanges: false
              RunOrder: 1
        - Name: Build
          Actions:
            - Name: CodeBuild
              InputArtifacts:
                - Name: source
              ActionTypeId: 
                Category: Build
                Owner: AWS
                Provider: CodeBuild
                Version: 1
              Configuration: 
                ProjectName: !Ref CodeBuild
              OutputArtifacts: 
                - Name: build
              RunOrder: 1
        - Name: Deploy
          Actions:
            - Name: CodeDeploy
              ActionTypeId:
                Category: Deploy
                Owner: AWS
                Provider: S3
                Version: 1
              Configuration:
                BucketName: !Ref DeployBucketName
                Extract: true
              InputArtifacts:
                - Name: build
              RunOrder: 1