Search code examples
amazon-web-servicesaws-codepipelineaws-codebuild

CodePipeline buildspec and multiple build actions


A simple buildspec like:

version: 0.2

phases:
  install:
    commands:
      - (cd lambda/src; npm install)
      - aws cloudformation package --template-file lambda/sam.yml --s3-bucket skynet-lambda --output-template-file SkynetLambdaPackaged.yml

artifacts:
  type: zip
  files:
    - SkynetLambdaPackaged.yml

Works fine when I have one action in my build stage. But what if I want to have more build actions for example: I want to build my api server and frontend files in parallel. How do I model this?

UPDATE

In CodePipeline I can create actions that run in parallel like below, how is this modeled in buildspec? Or isit impossible?

enter image description here


Solution

  • You can use two different CodeBuild projects from the same source as two separate parallel actions in your CodePipeline.

    For this to happen, you can use two buildspec files in your source.

    e.g.

    buildspec-frontend.yml

    phases:
      install:
        commands:
          - (cd frontend/src; npm run build)
          - aws s3 sync frontend/dist s3://<insert s3 bucket url here>/ --delete 
    

    buildspec-backend.yml

    phases:
      install:
        commands:
          - (cd lambda/src; npm install)
          - aws cloudformation package --template-file lambda/sam.yml --s3-bucket skynet-lambda --output-template-file SkynetLambdaPackaged.yml
    

    Then, create a frontend CodeBuild project that uses the frontend buildspec. Repeat for the backend.

    Then, when you go to your Build stage in your CodePipeline, use the two CodeBuild projects as parallel actions.


    Update: The information below is now irrelevant since I misunderstood the question.

    If your frontend can be deployed to s3, just add its deployment commands where you put your api deployment commands.

    e.g.

    phases:
      install:
        commands:
          - (cd lambda/src; npm install)
          - aws cloudformation package --template-file lambda/sam.yml --s3-bucket skynet-lambda --output-template-file SkynetLambdaPackaged.yml
          - (cd frontend/src; npm run build)
          - aws s3 sync frontend/dist s3://<insert s3 bucket url here>/ --delete
    

    If your frontend is not on s3, just replace those lines with your own frontend deployment commands.

    CodeBuild executes those commands in sequence. If you really need to run them in parallel, there are many ways to do it.

    My preference is to put the commands in a Makefile and call them from your buildspec.yml (e.g. make --jobs 2 backend frontend).