Jelastic JPS deployment: n8N fails to recognize IP in environment variable

Hey everyone, I’m having trouble with n8n and Jelastic JPS. I can’t get the DB_POSTGRESDB_HOST environment variable to work right. I want to use ${nodes.sqldb[0].address} to set the Postgres node IP, but it’s not happening.

I’ve tried adding the variable manually through the GUI, and that works fine. But I can’t seem to make it work in the JPS file. I even tried putting in a random IP just to see if it would pick it up, but no luck.

Here’s what I’ve attempted:

DB_POSTGRESDB_HOST: ${nodes.sqldb[0].address}
DB_POSTGRESDB_HOST: ${nodes.sqldb.first.address}
DB_POSTGRESDB_HOST: ${nodes.sqldb.master.address}

I think the issue might be that when the variable is set, the Postgres node isn’t created yet, so there’s no IP to grab. Any ideas on how to set this IP later in the process? I’m really stuck here. Thanks for any help!

hey mate, i had a similar headache with JPS. try using the onAfterCloneNodes event to set the variable. it fires after node creation, so the IP should be available. something like this:

onAfterCloneNodes:

  • forEach(nodes.sqldb):
    setGlobals:
    DB_HOST: ${@i.address}

then use ${globals.DB_HOST} in ur config. hope this helps!

I’ve faced a similar issue with Jelastic JPS and environment variables. From my experience, you’re on the right track about the timing of variable assignment. Here’s what worked for me:

Instead of trying to set the DB_POSTGRESDB_HOST directly in the environment variables section, I used a separate ‘actions’ block that runs after all nodes are created. Something like this:

actions:
  setDBHost:
    - cmd[n8n]: |-
        sed -i 's/DB_POSTGRESDB_HOST=.*/DB_POSTGRESDB_HOST=${nodes.sqldb.first.address}/' /path/to/your/.env
    - restartContainers:
      - n8n

This approach ensures the Postgres node is fully set up before we try to grab its IP. The sed command updates the .env file with the correct IP, and then we restart the n8n container to pick up the changes.

It’s a bit of a workaround, but it’s been reliable for me. Hope this helps!

Having dealt with similar JPS deployment issues, I can suggest an alternative approach. Instead of relying on environment variables, consider using a custom script that runs post-deployment to fetch and set the database host dynamically. You could create a simple shell script that retrieves the IP address of your Postgres node and updates the n8n configuration file accordingly. This method ensures the IP is available when it’s needed.

Here’s a basic example of what this script might look like:

#!/bin/bash
DB_IP=$(jelastic api environment.control.GetNodeInfo nodeId=${nodes.sqldb.first.id} | jq -r '.node.address')
sed -i "s/DB_POSTGRESDB_HOST=.*/DB_POSTGRESDB_HOST=$DB_IP/" /path/to/n8n/config

systemctl restart n8n

You can then add this script to your JPS file and set it to execute after all nodes are deployed. This approach has proven more reliable in my experience, as it sidesteps potential timing issues with variable assignment during the initial deployment process.