Skip to main content

node

In the node preparation stage, it is essential to manage dependencies efficiently before moving on to the later phases of the CI/CD cycle. Key preparations will be made here:

Installation of all dependencies (development and production): At this stage, a preparation will be executed to install all the dependencies required for the project. This will include both production dependencies, which are essential for the operation of the application in a production environment, and development dependencies, which are useful during the development and testing process. The installation of all dependencies ensures that the project is fully functional in this construction environment.

Integration with Gitlab

As we have just mentioned, we are going to set up a job.

All dependencies (node)

For the preparation of the necessary dependencies in the testing stage, we will create a stage that we will share with the rest of the stages through the GitLab Runner cache, which will significantly improve the efficiency of our pipeline. In this step, we will not generate artifacts since we will not require their discharge later. Optimizing the preparation stage is essential to ensure that all subsequent tests run efficiently and without missing dependency issues. By using the GitLab Runner cache, we avoid the need to re-download the same dependencies at each stage of the pipeline, which significantly speeds up the process of building and testing our application.

This stage named .dependencies_cache is responsible for managing the dependencies cache for the current project in GitLab CI/CD.

.gitlab-ci.yml
.dependencies_cache:
cache:
key: $CI_PROJECT_NAME-osdo
paths:
- .npm/
- node_modules/
- public/build/

The dot in front of a stage name in GitLab CI/CD, such as .dependencies_cache, is a convention used to indicate that it is a hidden or internal stage that does not run independently, but is used as part of other stages or to perform specific setup, preparation or cleanup tasks in the pipeline, but is not started by itself.

In this case, the cache is being configured with the key defined as $CI_PROJECT_NAME-osdo. The key is unique for each project and is used to identify and retrieve the cache associated with that particular project.

The items being cached are specified in the list under paths. In this example, the following directories and their contents are being included:

  • .npm/: This directory is used by npm, the Node.js package manager, to store the JavaScript and Node.js dependencies used in the project.

  • node_modules/: Here are the Node.js dependencies and modules required by the application.

  • public/build/: Contains files generated in the process of building the application, such as static resources.

Once the cache is configured we can use it in our preparation stage

.gitlab-ci.yml
npm:
image: node@sha256:f4c96a28c0b2d8981664e03f461c2677152cd9a756012ffa8e2c6727427c2bda
stage: preparation
needs:
- composer
extends: .dependencies_cache
before_script:
- npm ci --cache .npm --prefer-offline
script:
- npm run build

This stage called npm takes care of installing node dependencies using npm during the preparation stage in a GitLab CI/CD pipeline.

  • image: Specifies the Docker image to be used to run this stage. In this case, the image of node@sha256:f4c96a28c0b2d8981664e03f461c2677152cd9a756012ffa8e2c6727427c2bda is used with a specific tag (sha256:f4c96a28c0b2d898161664e03f461c2677152cd9a756012ffa8e2c672727427c2bda) that provides a configured environment for Node and the necessary tools.

  • stage: Defines the stage to which this stage belongs. In this case, it is in the preparation stage, which means that it will be executed before other stages in the pipeline.

  • needs: It tells us that it depends on the composer stage, this can be given for example in projects that are Laravel + InertiaJS + Vue as the OSDO APP

  • before_script: Here you specify the commands to be executed before the stage scripts. In this case, the command npm ci --cache .npm --prefer-offline is used to install the node dependencies. The different parameters following npm install have the following meanings:

    • ci: Avoid modifying the package.json file or the package-lock.json or yarn.lock dependency lock files.
    • --cache .npm: Use the local cache to store the downloaded packages.
    • --prefer-offlinen: Try to install packages from the local cache even if they are not up to date.
  • script: Here you specify the commands to be executed in this stage. In this case, the npm run build command is used to build the application, this command is defined in the package.json.

  • extends: Uses the extends directive to inherit the configuration from another stage called .dependencies_cache. This is commonly used to share configurations between different stages. In this case, .dependencies_cache configures the use of the GitLab Runner cache to store and retrieve previously installed dependencies, which significantly improves pipeline performance by avoiding repetitive download and installation of identical dependencies.