How to refer to buildspec files from CDK code?

0

I am currently building a codepipeline using AWS CDK in which I am making use of multiple codebuild projects for different codebuild actions that are part of several codepipeline stages. I have separate build specification files for each of the codebuild project under the folder - "buildspecs". This "buildspecs" resides in the root of my AWS CDK project. Now I want to refer to individual buildspec files from different pipeline source code files. For example, I want to refer to buildspecs/UnitTestsBuildSpec.yml from lib/infra/cicd/stages/unit-test-stage.ts. How can I do so? I saw different options in the documentations such as BuildSpec.fromSourceFileName, BuildSpec.fromObject, BuildSpec.fromAsset but I am confused which one to use because of lack of proper documentation around it. For now, I have used BuildSpec.fromSourceFileName but not sure whether to use relative path from the file I am in (like ../../../buildspecs/UnitTestsBuildSpec.yml) or to use absolute path from the root of the project (like buildspec/UnitTestsBuildSpec.yml). I see people using other options like fromAsset and fromObject on stackoverflow and github issues pages. Which one should I use and how? Any help would be appreciated. Thanks.

asked 9 months ago590 views
2 Answers
1

You need to use BuildSpec.fromSourceFileName to specify the path of the file in the source repository. The other methods, namely fromObject and fromAsset, are intended for CDK projects where the buildspec is stored locally to the project. To keep it simple, review the following:

fromAsset - loads from a local file path of the CDK project

fromObject - loads from an object variable in memory

fromSourceFileName - specifies the filename and path in the source code repo for the yml file of your buildspec.

Hope this helps, please accept this answer if it does.

profile picture
answered 9 months ago
  • Hey Bryant, this was helpful. But I am a bit confused between fromSourceFileName and fromAsset explanation given above. Both seem the same to me. Please can you clarify it a bit?

1

The difference between fromAsset and fromSourceFileName is that fromAsset uses files locally while fromSourceFileName uses from the source code server (which is not the local git repository).

Let's assume you had a config folder off the root of your project. You could use the FromAsset method to load from this config folder.

If you have want to reference the buildspec from source code repository (on either Github, CodeCommit, or BitBucket) you would utilize the fromSourceFileName method. This source code repository is not your local development environment.

Hope this helps

profile picture
answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions