Skip to content

Instantly share code, notes, and snippets.

@sandfox
Last active December 4, 2022 23:29
Show Gist options
  • Save sandfox/e6154fd1058b7cbb3653aaeb9c4336d4 to your computer and use it in GitHub Desktop.
Save sandfox/e6154fd1058b7cbb3653aaeb9c4336d4 to your computer and use it in GitHub Desktop.
getting nvm working in codebuild
phases:
install:
commands:
- . codebuild_nvm_wrapper.sh
- npm install
- node -v

sh is not considered by nvm to be a shell that can support passing options to to sourced scripts as they are sourced, so we cannot do . nvm.sh --install. nvm's default behaviour upon sourcing is to run nvm use bash would work fine but it's not the default shell, and it feels janky to have to replace sh with bash at the start of this yml file just to support it, it's also not yet worth the effort to maintain our own containers and associated build pipeline and container registry

need to set NVM_DIR for sourcing nvm.sh to work should probably set (and create) NVM_DIR before installing nvm

installing nvm from the repo directory causes it to see our .nvmrc and upon sourcing nvm.sh it's default behaviour is to do the equivalent of nvm use $VERSION which will fail because $VERSION must be installed before it can be used

installing nvm via install.sh causes it to read NODE_VERSION whilst installing and it will then install that version. "pre-installing" nvm.sh and then sourcing it causes failue because sourcing nvm.sh whilst in the same dir as .nvmrc causes it nvm use rather than nvm install

We should consider installing (and sourcing) nvm from a temp directory and then later calling nvm install from the repo directory

improvements:

  • skip fetching and running nvm's install script. can we instead just set $NVM_DIR and then fetch and source/run nvm.sh ourselves (maybe with a checksum check after download for safety). (As a half way house we could fetch nvm from somewhere we control, but meh, thats alot of faff
  • consider bundling nvm.sh inside the repo to save time fetching and remove chance for failures at build time caused by network / gh outages

Actual Goal

  • To have the right version node+npm installed and for that verson to be determined by the repo contents

nvm is just a means to that end and should cause to change to much else to accomodate it!

##
# You must source this file from the buildspec.yml
# for it to work properly
# Assumes you have a copy of nvm.sh in the root folder
##
NVM_DIR="$HOME/.nvm"
mkdir -p $NVM_DIR
cd $(mktemp -d)
. $CODEBUILD_SRC_DIR/nvm.sh
cd $CODEBUILD_SRC_DIR
nvm install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment