skip to Main Content

I am trying to load plan-buildspec.yml file in my codebuild Project, however its not able to find my builspec.yml no matter whichever path I give.

I did refer to this https://github.com/aws/aws-cdk/issues/7329, but not luck

YAML_FILE_ERROR Message: stat /codebuild/output/srcXXXXXXX/buildspec/plan-buildspec.yml: no such file or directory

here is my CodeBuild pipeline definition

 const TFplan = new codebuild.PipelineProject(this, 'tf-plan', {
      projectName: 'tf-cicd-plan',
      description: 'Plan stage for terraform',
      environment: {
        computeType: ComputeType.SMALL,
        buildImage: LinuxBuildImage.AMAZON_LINUX_2_4
      },
      buildSpec: codebuild.BuildSpec.fromSourceFilename('../buildspec/plan-buildspec.yml')
    })



const TFplanbBuildAction = new codepipeline_actions.CodeBuildAction({
      actionName: 'Build',
      project: TFplan,
      input: sourceOutput,
    });

my tree structure, buildspec files are present in buildspec directory.

.
├── README.md
├── bin
├── buildspec
├── cdk.json
├── cdk.out
├── jest.config.js
├── lib
├── node_modules
├── package-lock.json
├── package.json
├── test
└── tsconfig.json

2

Answers


  1. Chosen as BEST ANSWER

    as @fedonev mentioned, when fromSourcefilename is trying to find the file only at runtime not at the synth time.

    I fixed it with this

    import * as fs from 'fs';
    import * as yaml from 'yaml';
    
    const TFplan = new codebuild.PipelineProject(this, 'tf-plan', {
          projectName: 'tf-cicd-plan',
          description: 'Plan stage for terraform',
          environment: {
            computeType: ComputeType.SMALL,
            buildImage: LinuxBuildImage.AMAZON_LINUX_2_4
          },
          buildSpec: codebuild.BuildSpec.fromObject(yaml.parse(fs.readFileSync('buildspec/plan-buildspec.yml', 'utf8')))
          //buildSpec: codebuild.BuildSpec.fromSourceFilename('buildspec/plan-buildspec.yml')
        })
    
        const TFplanbBuildAction = new codepipeline_actions.CodeBuildAction({
          actionName: 'Build',
          project: TFplan,
          input: sourceOutput,
        });
    

    Update:

    There is also a much simpler way:

    To use: fromAsset uploads the yaml file to S3 b bucket created by cdk bootstrap and referenced from the codebuild project.

     buildspec: codebuild.BuildSpec.fromAsset("codebuild-buildspec/apply.yml"),
    

  2. buildspec/plan-buildspec.yml

    The path is relative to the project root.


    Note: When you use fromSourceFilename, CodeBuild looks for your buildspec file only at runtime. It expects a file in the pipeline artefact (= *from a file in the source* = your repo). The template the CDK creates has the file name only:

    "Type": "AWS::CodeBuild::Project",
    "Properties": {
        "Source": {
            "BuildSpec": "buildspec/plan-buildspec.yml",
    

    If instead you want the CDK to embed the buildspec itself with the pipeline definition, you must use Buildspec.fromObject, passing key-value pairs. CDK puts the buildspec in the template at synth-time:

    "Type": "AWS::CodeBuild::Project",
    "Properties": {
        "Source": {
            "BuildSpec": "{n  "version": "0.2",n  "phases": {n    "build": {n   ...",
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search