Deploying Strapi with Fly.io and GitHub Actions: Error while running command build: Could not load js config file /opt/app/config/env/qa/database.js: host is not defined

Hello,

I’m trying to deploy with Fly.io with GitHub Actions. I’ve defined secrets within the repository scope on GitHub.

Guidance provided by: Continuous Deployment with Fly.io and GitHub Actions · Fly Docs

I’m getting an error on npm run build which suggests that there is an issue with reading the DATABASE_URL

#11 [8/8] RUN npm run build
#11 0.643 
#11 0.643 > ***@0.1.0 build
#11 0.643 > strapi build
#11 0.643 
#11 1.799 Error while running command build: Could not load js config file /opt/app/config/env/qa/database.js: host is not defined
#11 ERROR: executor failed running [/bin/sh -c npm run build]: exit code: 1
------
 > [8/8] RUN npm run build:
#11 0.643 
#11 0.643 > ***@0.1.0 build
#11 0.643 > strapi build
#11 0.643 
#11 1.799 Error while running command build: Could not load js config file /opt/app/config/env/qa/database.js: host is not defined
------
Error failed to fetch an image or build from source: error building: executor failed running [/bin/sh -c npm run build]: exit code: 1


Error: Process completed with exit code 1.

config/env/qa/database.js

const parse = require("pg-connection-string").parse;
const { host, port, database, user, password } = parse(process.env.DATABASE_URL);

module.exports = ({ env }) => ({
  defaultConnection: 'default',
  connections: {
    default: {
      connector: 'bookshelf',
      settings: {
        client: 'postgres',
        host,
        port,
        database,
        user,
        password,
        ssl: false,
      },
      debug: false,
      options: {},
    },
  },
});

GitHub Actions Secrets

Related

Thanks for your help!

Whilst not a solution for just having DATABASE_URL and using:

const parse = require("pg-connection-string").parse;
const { host, port, database, user, password } = parse(process.env.DATABASE_URL);

I’ve found that if you define the environment variables separately and then you have something like:

module.exports = ({ env }) => ({
  defaultConnection: 'default',
  connections: {
    default: {
      connector: 'bookshelf',
      settings: {
        client: env("DATABASE_CLIENT", "postgres"),
        host: env("DATABASE_HOST", "127.0.0.1"),
        port: env.int("DATABASE_PORT", 5432),
        database: env("DATABASE_NAME", "strapi"),
        user: env("DATABASE_USERNAME", "strapi"),
        password: env("DATABASE_PASSWORD", "strapi"),
        ssl: false,
      },
      debug: false,
      options: {},
    },
  },
});

this works.

1 Like

Hey @mahalowolf

I was struggling with this today aswell.
What I found was that I had to include the following lines in the Dockerfile

ARG DATABASE_URL
ENV DATABASE_URL=${DATABASE_URL}

After that I added the following to the build step in the Github workflow file.

build-args: |
  DATABASE_URL=${{ secrets.DATABASE_URL }}

This also worked for the secrets which I had to provide for the admin panel during the build.

The database.js file I use is looking like this.

const parse = require("pg-connection-string").parse;
const config = parse(process.env.DATABASE_URL);
module.exports = () => ({
  connection: {
    client: "postgres",
    connection: {
      host: config.host,
      port: config.port,
      database: config.database,
      user: config.user,
      password: config.password,
      ssl: false,
    },
    debug: false,
  },
});

This also works well with Attach or Detach a Fly App · Fly Docs as it spits out a connection string when you attach your app to the Postgres cluster.

Maybe this won’t help you anymore but still for anyone ever stumbling upon this thread :smiley: