whitebear
whitebear

Reputation: 12433

How to writing the buildspec yaml code in cdk

I making the codebuild with cdk

It can accept the buildspec as yaml, however ,how can I do the same thing in cdk?

What I want to do is like this, of course it doesn't work though,

I forcely put the yaml code in commands.

    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 also I guess I need to choose the image to do the buildspec such as Ubuntu

Where can I set these?

Upvotes: 3

Views: 4622

Answers (1)

fedonev
fedonev

Reputation: 25669

CDK does not expose a method to inline a YAML buildspec at synth-time. You could do this yourself by parsing existing YAML into a JS object and passing the result to BuildSpec.fromObject.

CDK's codebuild.Project gives you several other ways to provide a buildSpec:

  • BuildSpec.fromObject inlines a buildspec from key-value pairs at synth-time. It should follow the CodeBuild buildspec format. CDK will output a stringified JSON buildspec in the CloudFormation template. If you want CDK to output YAML instead, use fromObjectToYaml. Both methods take key-value pairs (type: [key: string]: any;) as input, so TS can't offer much typechecking help.
  • BuildSpec.fromSourceFilename tells CodeBuild to use a buildspec file in your source at run-time. The filename is passed in the CloudFormation template.

Here's an example of parsing a YAML string into inlined YAML output, using the yaml package. Note that the environment is defined outside the buildspec:

import * as yaml from 'yaml';

const fromYaml = yaml.parse(`
  version: '0.2'
  phases:
    build:
      commands:
        - npm run build
`);

new codebuild.Project(this, 'YamlInYamlOutProject', {
  environment: {
    buildImage: codebuild.LinuxBuildImage.STANDARD_5_0, // Ubuntu Standard 5
  },
  buildSpec: codebuild.BuildSpec.fromObjectToYaml(fromYaml),
});

Upvotes: 4

Related Questions