---
AWSTemplateFormatVersion: '2010-09-09'
Description: CodeBuild for deploying to a S3 bucket

Parameters:
  ProjectName:
    Type: String
    Description: Codebuild project name
    Default: deploy-myapp
 
  RepoURL:
    Type: String
    Description: The URL of the GitHub repository
    Default: https://github.com/username/repo.git

  BranchName:
    Type: String
    Description: The branch to deploy
    Default: main

  S3Bucket:
    Type: String
    Description: The S3 bucket to deploy to
    Default: myapp-bucket

  OutputFolder:
    Type: String
    Description: The folder to deploy
    Default: _site

Resources:
  CodeBuildRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Effect: Allow
            Principal:
              Service: [codebuild.amazonaws.com]
            Action: ['sts:AssumeRole']
      Policies:
        - PolicyName: S3Policy
          PolicyDocument:
            Statement:
              - Effect: Allow
                Action:
                  - s3:GetObject
                  - s3:PutObject
                  - s3:ListBucket
                Resource:
                  - !Sub 'arn:aws:s3:::${S3Bucket}'
                  - !Sub 'arn:aws:s3:::${S3Bucket}/*'
        - PolicyName: CloudWatchPolicy
          PolicyDocument:
            Statement:
              - Effect: Allow
                Action:
                  - logs:CreateLogGroup
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                Resource:
                  - !Sub 'arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/${ProjectName}:*'

  CodeBuildProject:
    Type: AWS::CodeBuild::Project
    Properties:
      Name: !Ref ProjectName
      Source:
        Type: GITHUB
        Location: !Ref RepoURL
        BuildSpec: |
          version: 0.2
          phases:
            install:
              commands:
                - echo Installing dependencies...
                - npm install
            build:
              commands:
                - echo Building the project...
                - npm run build
                - aws s3 sync $OUTPUTFOLDER s3://$S3BUCKET
                - echo Build completed on `date`
      SourceVersion: !Ref BranchName
      Environment:
        Type: LINUX_CONTAINER
        Image: aws/codebuild/standard:7.0
        ComputeType: BUILD_GENERAL1_SMALL
        EnvironmentVariables:
          - Name: S3BUCKET
            Value: !Ref S3Bucket
          - Name: OUTPUTFOLDER
            Value: !Ref OutputFolder
      Artifacts:
        Type: NO_ARTIFACTS
      ServiceRole: !Ref CodeBuildRole