2

I am new to create a pipeline in aws. I want to create a ci-cd pipeline for my nuxt project. I create a yml file in which I want to install nvm and then install node version 12.18.3

The problem is I am getting the nvm not found error.

Can you please check and let me know if there is any error in my yml file:

version: 0.2
phases:
  install:
   commands:
     - echo Installing nvm...
     - curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
     - export NVM_DIR="$HOME/.nvm"
     - '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"'
     - '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"'
  pre_build:
    commands:
      #install dependencies
      - echo Installing node...
      - nvm install 12.18.3
      - echo Installing npm...
      - npm install
  build:
    commands:
      #build
      - echo building...
      - npm run generate
artifacts:
  files:
    - '**/*'
 base-directory: dist
cache:
  paths:
    - node_modules/**/*

Thank you.

Ragu
  • 155
  • 2
  • 12
  • are you sure the $HOME variable is set?. you can try to echo it and see that it is set to the correct value – Arun Kamalanathan Jun 21 '21 at 00:05
  • @ArunK Hi, $HOME returned "/root". Is this correct? – Ragu Jun 21 '21 at 07:43
  • yes because the docker is running as root. I didn't get a chance to try this. Can you try these two ideas for me. 1. change the line to include an echo `'[ -s "$NVM_DIR/nvm.sh" ] && echo "nvm folder exists" && \. "$NVM_DIR/nvm.sh"'` . see whether it's printing "nvm folder exists" in the logs. 2. I want you to try the first one first. then can you change the line to `'[ -s "$NVM_DIR/nvm.sh" ] && source "$NVM_DIR/nvm.sh"'` . and see if it solves the problem – Arun Kamalanathan Jun 21 '21 at 08:36
  • I applied 1st idea and it returns "nvm folder exists", then I change the line and now my lines look like this: ` - echo $HOME \n - export NVM_DIR="$HOME/.nvm" \n - '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"' \n - '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"' \n - '[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"'` I changed the source to '.' because it was giving error source not found. It enters pre-build phase, but same nvm not found error: `/codebuild/output/tmp/script.sh: 4: /codebuild/output/tmp/script.sh: nvm: not found` – Ragu Jun 21 '21 at 09:24
  • whats the image you are using for codebuild – Arun Kamalanathan Jun 21 '21 at 09:47
  • I do not have much idea about the image but I think its "aws/codebuild/standard:4.0" as I searched it here "https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-available.html" – Ragu Jun 21 '21 at 09:58
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/234004/discussion-between-ragu-and-arun-k). – Ragu Jun 21 '21 at 09:59

2 Answers2

4

Working Solution

For some reason, It doesn't recognize nvm in the next line. I did not get a chance to investigate it further. The following configuration works. The idea is to set the nvm configurations and install node in the same line.

version: 0.2
phases:
  install:
    commands:
      - echo Installing nvm...
      - curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
      - export NVM_DIR="$HOME/.nvm"
      - '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"'
      - '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"'
  pre_build:
    commands:
      #install dependencies
      - . "$NVM_DIR/nvm.sh" && nvm install 12.18.3 && echo "node installed by arun"
      - echo Installing node...
      #- nvm install 12.18.3
      - echo Installing npm...
      - npm install
  build:
    commands:
      #build
      - echo building...
      - npm run generate
cache:
  paths:
    - node_modules/**/*

Troubleshoot in local environment.

In order to troubleshoot the buildspec.yaml, you can run the build locally. here is how to run the buildspec locally.

Arun Kamalanathan
  • 8,107
  • 4
  • 23
  • 39
  • In my case this was failing on `'[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"'` step because of `.nvmrc` file in the root dir. Fixed it by navigating first to `/tmp` and then back to `$CODEBUILD_SRC_DIR` – stillwaiting Mar 19 '22 at 11:25
4

You don't actually need to use nvm to install specific node.js version on AWS CodeBuild.

You can use runtime-versions option which would install some version, but you don't have much control over this.

phases:
  install:
    runtime-versions:
      nodejs: 12.x

But AWS standard 5 image comes with n preinstalled (haven't checked 4 but it should be there as well), so you can use it like:

phases:
  install:
    commands:
      - n 12.18.3

and it would install that version same as nvm.

Karolis Mazukna
  • 377
  • 1
  • 6
  • 12
  • using n command just works thanks :) – Hady Rashwan May 25 '22 at 10:04
  • I also see that there's an "auto" label: There is an auto label to read the target version from a file in the current directory, or any parent directory. n looks for in order https://github.com/tj/n#specifying-nodejs-versions – Jonathan Ramos Jul 04 '23 at 06:58