Home > database >  Which way is better to put buildspec.yml, in CDK or root directory of the source code
Which way is better to put buildspec.yml, in CDK or root directory of the source code

Time:05-05

Which way is better to add buildspec.yml, in CDK package or source code package?

Context

There are two ways of adding buildspec code right now.

The first option is to add it within the source code package, as mentioned here: "If you include a buildspec as part of the source code, by default, the buildspec file must be named buildspec.yml and placed in the root of your source directory."

However, this option feels off as buildspec really has nothing to do with the service implementation, rather more to do with how to deploy the service. And it is more specific to AWS. It just feels like it should not be part of the source code package.

The second option is to add it in the CDK. But I really don't like have inline buildspec code as it looks so bloated, just an example:

const buildProject = new codebuild.PipelineProject(this, 'project', {
      environment: {// I guess I need to select ubuntu and image 4.0},
      buildSpec: codebuild.BuildSpec.fromObject({
        version: '0.2',
        phases: {
          build: {
            commands:['
              version: 0.2
                phases:
                    install:
                        runtime-versions:
                            docker: 18
                    build:
                        commands:
                            - apt-get install jq -y
                            - ContainerName="tnkDjangoContainer"
                            - ImageURI=$(cat imageDetail.json | jq -r '.ImageURI')
                            - printf '[{"name":"CONTAINER_NAME","imageUri":"IMAGE_URI"}]' > imagedefinitions.json
                            - sed -i -e "s|CONTAINER_NAME|$ContainerName|g" imagedefinitions.json
                            - sed -i -e "s|IMAGE_URI|$ImageURI|g" imagedefinitions.json
                            - cat imagedefinitions.json

                artifacts:
                    files:
                        - imagedefinitions.json
              
              ',         
            ],
          },
        }
      })
    });

And the buildspec code can get more complex and longer.

So instead I'm planning to add :

    const buildspecFile = FS.readFileSync('./config/buildspec.yml', 'utf-8');  
    const buildspecFileYaml = YAML.parse(buildspecFile,{
          prettyErrors:true
        }
    );

    const codeBuildProject = new CodeBuild.PipelineProject(this, "Build", {
      projectName: "Build",
      environment: {
        buildImage: CodeBuild.LinuxBuildImage.AMAZON_LINUX_2_2,
        privileged: true,
      },
      environmentVariables: {
        CLUSTER_NAME: {
          value: `${props.fargateCluster.clusterName}`,
        },
        ECR_REPO_URI: {
          value: `${ecrRepo.repositoryUri}`,
        },
      },
      buildSpec: CodeBuild.BuildSpec.fromObjectToYaml(buildspecFileYaml),
    });

Which way is better? I don't think AWS CodePipeline support read from a local file in CDK package yet? I only see three options now:

  • CodeBuild.BuildSpec.fromObject
  • CodeBuild.BuildSpec.fromObjectToYaml
  • CodeBuild.BuildSpec.fromSourceFilename

CodePudding user response:

Which way is better to add buildspec.yml, in CDK package or source code package?

Pass. It depends on the use case and developer preference.

I don't think AWS CodePipeline support read from a local file in CDK package yet?

True, but you can inline a local yaml buildspec file into the CDK output yourself using language features. Read and parse the file, then pass the result to the fromObject method:

import * as yaml from 'yaml'; // https://www.npmjs.com/package/yaml

const stringified: string = fs.readFileSync(path.join(__dirname, './buildspec.yml'), { encoding: 'utf-8', });
const parsed: any = yaml.parse(stringified);

new codebuild.Project(this, 'MyProject', {
  buildSpec: codebuild.BuildSpec.fromObject(parsed),
});
  • Related