CI with aws and npm

My CI stops to an error about not finding npm command. Here’s my .gitlab-ci.yml:

stages:
  - test
  - deploy

default:
  image: node:18
  before_script:
    - npm ci

lint:
  stage: test
  script:
    - npm run lint

deploy_staging:
  stage: deploy
  image: registry.gitlab.com/gitlab-org/cloud-deploy/aws-base:latest
  services:
    - node:18
  script:
    - echo "Deploying to Staging"
    - npm run build
    - npm i -g @architect/architect
    - arc deploy --staging --prune
  environment:
    name: staging
    url: https://staging.example.com
  only:
    - main
  variables:
    AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
    AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY

The problem that I get is as follows:

$ npm ci
/usr/bin/bash: line 154: npm: command not found

I have changed the image from the standard image to aws-base to get aws command. I have have added node:18 as a service which should provide npm command but evidently that did not do the trick.

Hi,

Any command that you use in script part must be part of the image you are using for your job (in your case registry.gitlab.com/gitlab-org/cloud-deploy/aws-base:latest). node is obviously not installed in that image.
On the other hand, services define containers that run next to your job’s image (normally used for integration testing against databases, etc) - not for your use case.

So, in your case, you need to install node/npm additionally or ideally, find a gitlab aws image that has one installed. But my follow up question would be - why are you running npm build in deploy stage? Normally you would build your package in another (previous) build job (with node:18 image) and then just pass in artifacts to your deploy_staging job, e.g. (didn’t test, use just as inspiration):

stages:
  - test
  - build
  - deploy


build:
  stage: build
  image: node:18
  script:
    - npm install
    - npm run build
    - # whatever else you need to do for build
  artifacts:
    paths:
      - # here you specify path where your artifacts are generated

deploy_staging:
  stage: deploy
  image: registry.gitlab.com/gitlab-org/cloud-deploy/aws-base:latest
  script:
    # whatever you need to do only for deploy, artifacts are already automatically downloaded 
    # from previous stage and are existing in the same folder
    - arc deploy --staging --prune
  environment:
    name: staging
    url: https://staging.example.com
  only:
    - main
  variables:
    AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
    AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
    

Thank you. It just didn’t come to be to use different images for building and deploying. That is of course the correct approach.

1 Like

We are having our next problem which is somewhat related: How to deploy to aws with arc