Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS CodeBuild does not work with Yarn Workspaces

I'm using Yarn Workspaces in my repository and also using AWS CodeBuild to build my packages. When build starts, CodeBuild takes 60 seconds to install all packages and I'd want to avoid this time caching node_modules folder.

When I add:

cache:
  paths:
    - 'node_modules/**/*'

to my buildspec file and enable LOCAL_CUSTOM_CACHE, I receive this error:

error An unexpected error occurred: "EEXIST: file already exists, mkdir '/codebuild/output/src637134264/src/git-codecommit.us-east-2.amazonaws.com/v1/repos/MY_REPOSITORY/node_modules/@packages/configs'".

Is there a way to remove this error configuring AWS CodeBuild or Yarn?

My buildspec file:

version: 0.2
phases:
  install:
    commands:
      - npm install -g yarn
      - git config --global credential.helper '!aws codecommit credential-helper $@'
      - git config --global credential.UseHttpPath true
      - yarn
  pre_build:
    commands:
      - git rev-parse HEAD
      - git pull origin master
  build:
    commands:
      - yarn run build
      - yarn run deploy
  post_build:
    commands:
      - echo 'Finished.'
cache:
  paths:
    - 'node_modules/**/*'

Thank you!

Update 1:

The folder /codebuild/output/src637134264/src/git-codecommit.us-east-2.amazonaws.com/v1/repos/MY_REPOSITORY/node_modules/@packages/configs was being attempted to be created by Yarn, with the command - yarn at install phase. This folder is one of my repository packages, called @packages/config. When I run yarn on my computer, Yarn creates folders linking my packages as described here. An example of how my node_modules structure is on my computer:

node_modules/
|-- ...
|-- @packages/
|   |-- configs/
|   |-- myPackageA/
|   |-- myPackageB/
|-- ...
like image 215
Pedro Arantes Avatar asked Apr 28 '19 12:04

Pedro Arantes


1 Answers

I was having the exact same issue ("EEXIST: file already exists, mkdir"), I ended up using S3 cache and it worked pretty well. Note: for some reason the first upload to S3 took way (10 minutes) too long, the others went fine.

Before:

[5/5] Building fresh packages...
--
Done in 60.28s.

After:

[5/5] Building fresh packages...
--
Done in 6.64s.

If you already have your project configured you can edit the cache accessing the Project -> Edit -> Artifacts -> Additional configuration.

My buildspec.yml is as follows:

version: 0.2

phases:
  install:
    runtime-versions:
       nodejs: 14
  build:
    commands:
      - yarn config set cache-folder /root/.yarn-cache
      - yarn install --frozen-lockfile
      - ...other build commands go here

cache:
  paths:
    - '/root/.yarn-cache/**/*'
    - 'node_modules/**/*'
    # This third entry is only if you're using monorepos (under the packages folder)
    # - 'packages/**/node_modules/**/*'

If you use NPM you'd do something similar, with slightly different commands:

version: 0.2

phases:
  install:
    runtime-versions:
       nodejs: 14
  build:
    commands:
      - npm config -g set prefer-offline true
      - npm config -g set cache /root/.npm
      - npm ci
      - ...other build commands go here

cache:
  paths:
    - '/root/.npm-cache/**/*'
    - 'node_modules/**/*'
    # This third entry is only if you're using monorepos (under the packages folder)
    # - 'packages/**/node_modules/**/*'

Kudos to: https://mechanicalrock.github.io/2019/02/03/monorepos-aws-codebuild.html

like image 157
Edmundo Rodrigues Avatar answered Oct 14 '22 14:10

Edmundo Rodrigues