Home > Blockchain >  CDK: Codebuild project not able to find buidspec.yml file
CDK: Codebuild project not able to find buidspec.yml file

Time:12-14

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

CodePudding user response:

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   ...",

CodePudding user response:

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,
    });
  • Related