Skip to main content

Ruby on rails with Nexus

Introduction

In this blog we will use nexus setup for NPM for a project which uses Ruby on rails. In this ruby on rails project we are using YARN and BUN framework. We did setup a nexus NPM proxy and private repo but yarn could not fetch the npm dependencies via the nexus proxy. The rails assets precompile task however uses bun which uses npm. YARN is a wrapper on the NPM hence it does not entirely rely on NPM. BUN is a runtime which performs all the NPM tasks. Our goal in this blog is to understand how to handle this scenario in a ruby on rails project when use NEXUS NPM proxy.

Technical specifications

We have below versions of the tools
  • NEXUS : 3.37.1-01
  • NPM : 9.5.0
  • NODE : v18.15.0
  • NVM : 0.39.1 ( Node version manager )
  • Mac OS : Sonoma 14.2
  • YARN : 3.5.1
  • BUN : 1.0.0

Steps

Below are the steps required
  • Configure NPM proxy in NEXUS
  • Configure NPM private hosted repo
  • Configure NPM group in NEXUS
  • Change local NPM config for registry
  • Change the YARN config for registry
  • Configure BUN configuration for registry

Configure NPM proxy in NEXUS

It is a standard proxy addition in the nexus repository settings tab. Click Create Repository button on the top left corner of the table. In this path below fill the in the details on the UI form

Login -> Settings -> Repositories -> Create repository -> npm (proxy)

FORM Values

  • Name : Enter some name for your proxy repo ( Ex: npm-public )
  • Remote Storage : The value expected here is https://registry.npmjs.org
  • Blob store : Select a blob store you have for your setup. In our case we are having s3 storage
Below are the screenshots showing the mandatory fields mentioned above. You can keep the other fields as default values for now

Create npm proxy - page 1

Create npm proxy - page 2

Create npm proxy - page 3

Configure NPM private hosted repo

NPM private hosted repo is internal to your organisation. It can be used for publishing node modules and libraries to be used within the organisation. The process to create it is similar to NPM hosted proxy. Only difference is that it does not have a remote server url in case of hosted private repo. Goto below flow path

Login -> Settings -> Repositories -> Create repository -> npm (hosted)

FORM Values

  • Name : Enter some name for your proxy repo ( Ex: npm-private )
  • Blob store : Select a blob store you have for your setup. In our case we are having s3 storage
Please refer the screenshots below :

NPM private hosted repo - Page 1

NPM private hosted repo - Page 2

Create NPM group in NEXUS

Now that we have the npm-public and npm-private repos. We don't want to use two links for accessing them or managing the access control for the users within the organisation. Hence we will create a group of these two repos called a repositories group. In the NEXUS to create group follow below flow path :

Login -> Settings -> Repositories -> Create repository -> npm (group)

FORM Values

  • Name : Enter some name for your proxy repo ( Ex: npm-private )
  • Blob store : Select a blob store you have for your setup. In our case we are having s3 storage
Once the form values are filled, select the member repositories i.e. npm-private and npm-public. Refer the screenshot below. 

Create NPM group

Change the local NPM configuration for registry

You local NPM configuration by default will go to NPM registry directly. We will now change it to use the NEXUS npm group so that it first checks if the artefact is available in your internal blob store. If yes then download it from there else fetch it from remote npm registry and store in the blob store for future reuse. The group also handles the scenario that if an artefact is present in the npm-private then fetch it from there else search as per the previous flow. In nutshell we have to configure the npm group url in the npm configuration on the local.

There are two types of configuration

  • global : user scoped config
  • local : project scoped config
To view the configuration resolution run below command. It will show the global config and out of the global config which parameters are overridden by project specific config. 

NOTE : The below command should be run from the root folder of your project which contains the .npmrc. If the project does not contain one then it will not show the project scoped values.

npm config list

The output will contain some kind of format below

; "user" config from <path of the .npmrc file for user>
<registries configured in that file>
; "project" config from <path of your current project .npmrc>
<registries and other params overridden at project npmrc file>

Create a .npmrc file in the project root folder and below content into that.

registry=<complete url path of nexus npm-group>
//<neuxs npm group url without http: >:_auth="<base64 encoded value of username and password>"

  • Nexus group url : You can get from the repositories list table. There is a column named as URL which contains the copy icon. While copying the url do not copy the http: value we only want the part after the colon.
  • Base 64 encoded username and password : If you are using MAC or LINUX use the command mentioned below for generating this value
Base64 encoded creds command

echo -n 'username:password' | base64


Now when you try to do NPM login then you will be prompted with username and password. But you will see they are pre-populated. You need to only confirm. The password will be masked though. Just type enter and proceed. You will now be authenticated using the credentials added to the _auth.

Change the YARN config for registry

Now the NPM configuration is ready we need to change the yarn configuration so that it points to the NEXUS registry. Although I did not find any benefit changing this as the rails assets precompile uses bun for that hence I am documenting this step for consistency. Follow below steps.

Check existing yarn configuration using below command and press enter. It will show the existing yarn configuration as key value pairs.

yarn config

In the output check for the value of the field npmRegistryServer. We will now change it to our nexus npm-public repository. Use below command to change the yarn config npm registry value.

yarn config set npmRegistryServer <complete url path of nexus npm-public repository>

Once the value is set verify the same using yarn config command mentioned above.

Change the BUN configuration for the registry

BUN configuration should be added in the file called bunfig.toml. Create a file in the root folder of the project with this name and add below details to it.


[install]
# set default registry as a string
registry = "https://<nexus user>:<nexus password>@<nexus hostname>/repository/npm-public/"

[install.scopes]
"@mynpm-public" = "https://<nexus user>:<nexus password>@<nexus hostname>/repository/npm-public/"

All set now to use the assets precompile command. Run below command and see now the dependencies are fetched via nexus and cached in nexus.

rails assets:precompile

Summary

In this blog we learnt about how to point my ruby on rails project to my nexus repositories for NPM.

Comments

Popular posts from this blog

Container detached from docker network

Introduction One fine day you find out that one of your docker containers is running but is not currently attached to the container network that you created using docker network command. Ideally a container should be detached from the network when there is some error in it. However, if you set the restart: on-failure in the docker-compose.yml  in that case the container should restart and still be attached to the network. In such a weird situation follow the steps documented in this blog. Possible reasons Listed below are the possible reasons for this issue Storage space issue Memory issue ( We will cover this in next blog ) Storage space issue We need to check the space utilisation at the server level first then drill down to the docker engine. Server level utilisation In most cases the server is ubuntu system hence check the disk usage using the disk free command (i.e. df -h ). It will show you the current storage space utilisation of the server. df -h Docker engine space utilis...

Secure login to MySQL for bash scripts

Introduction In this blog we will connect to a mysql via CLI from a bash script for generating a db dump. A cleaner approach for the same is using mysql config editor. In this blog we are taking an example of a mysql container running inside docker and we have a script for taking a db backup using  mysqldump . We will not use a password as a parameter to  mysqldump  command instead we will use something called as login path. MySQL config editor It is a utility to store authentication credentials in a obfuscated login path file named as  .mylogin.cnf . How to do it ? View existing configurations To view existing configurations if there are any use below command. The command will output existing configurations if there are otherwise it won't print anything. mysql_config_editor print --all Sample output In the output below you can see that the password is obfuscated and is not readable directly. It is handled by the mysql utilities to use this password as per the flags ...

How to install Google font on MacOS

Introduction Google fonts are open source. I was reading through the new angular website  and was astonished by the beautiful font used in the code editor shown in the samples. The font name is DM Mono . I wanted to use the same font on my Visual studio code editor for the Angular project development but I could not find a good blog which will guide through the installation process. Hence I am documenting this blog for the same. Get the font Follow below steps to download a font from Google fonts website . Visit the Google fonts website .  Search for the font you like for example DM Mono Click on the Get font button It will download a zip containing ttf format files Install the font on MacOS Once the zip is downloaded in the default downloads folder on your Mac. Follow below steps Double click to uzip the zip Open the folder Ex: DM_Mono Select all files with extension as .ttf and do not select other files like txt or some other format Keeping all the files selected double clic...