Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

fix(railpack): interpolate build-time env variables by sourcing build-time .env

Aditya Tripathi623bf89543bf
diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php
index 20eae036bd..6b72fb3398 100644
--- a/app/Jobs/ApplicationDeploymentJob.php
+++ b/app/Jobs/ApplicationDeploymentJob.php
@@ -2667,12 +2667,22 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
$cacheArgs .= ' --build-arg secrets-hash='.$this->generate_secrets_hash($variables);
}
- $environmentPrefix = $this->railpack_build_environment_prefix($variables);
+ // Build-time variables reach the build through the sourced build-time .env file
+ // (written by save_buildtime_environment_variables), which interpolates shell-style
+ // references such as BETTER_AUTH_URL=$COOLIFY_URL. Passing them inline via `env`
+ // would forward the literal `$COOLIFY_URL` because each value is single-quoted and
+ // `env` does not interpolate its own assignments. Only buildpack control variables
+ // (NIXPACKS_/RAILPACK_) — which are excluded from the build-time .env file and never
+ // need interpolation — are still passed inline.
+ $controlVariables = $variables->filter(
+ fn ($value, $key) => str($key)->startsWith(EnvironmentVariable::BUILDPACK_CONTROL_VARIABLE_PREFIXES)
+ );
+
+ $environmentPrefix = $this->railpack_build_environment_prefix($controlVariables);
$secretFlags = $this->railpack_build_secret_flags($variables);
$frontendImage = 'ghcr.io/railwayapp/railpack-frontend:v'.config('constants.coolify.railpack_version');
- return 'docker buildx create --name coolify-railpack --driver docker-container 2>/dev/null || true'
- ." && {$environmentPrefix}docker buildx build --builder coolify-railpack"
+ $buildxBuildCommand = "{$environmentPrefix}docker buildx build --builder coolify-railpack"
." {$this->addHosts} --network host"
." --build-arg BUILDKIT_SYNTAX=\"{$frontendImage}\""
." {$cacheArgs}"
@@ -2682,6 +2692,9 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
.' --load'
." -t {$imageName}"
." {$this->workdir}";
+
+ return 'docker buildx create --name coolify-railpack --driver docker-container 2>/dev/null || true'
+ .' && '.$this->wrap_build_command_with_env_export($buildxBuildCommand);
}
private function decode_railpack_config(string $config, string $source): array
diff --git a/tests/Unit/ApplicationDeploymentRailpackConfigTest.php b/tests/Unit/ApplicationDeploymentRailpackConfigTest.php
index e1449a59ef..77815a5237 100644
--- a/tests/Unit/ApplicationDeploymentRailpackConfigTest.php
+++ b/tests/Unit/ApplicationDeploymentRailpackConfigTest.php
@@ -236,10 +236,15 @@ it('builds railpack docker command with matching env and secret flags for all ra
],
);
+ // Build-time variables are interpolated by sourcing the build-time .env file before
+ // the build, so user/Coolify variables must NOT be forwarded inline as literals.
+ expect($command)->toContain('set -a && source /artifacts/build-time.env && set +a');
expect($command)->toContain("env 'RAILPACK_NODE_VERSION=22'");
expect($command)->toContain("'RAILPACK_INSTALL_CMD=npm ci && npm run postinstall'");
expect($command)->toContain("'RAILPACK_DEPLOY_APT_PACKAGES=curl wget'");
- expect($command)->toContain("'SECRET_JSON={\"token\":\"abc\"}'");
+ // SECRET_JSON is not a buildpack control variable, so it is provided via the sourced
+ // build-time .env file (which supports $VAR interpolation) rather than inline `env`.
+ expect($command)->not->toContain("'SECRET_JSON={\"token\":\"abc\"}'");
expect($command)->toContain("--secret 'id=RAILPACK_NODE_VERSION,env=RAILPACK_NODE_VERSION'");
expect($command)->toContain("--secret 'id=RAILPACK_INSTALL_CMD,env=RAILPACK_INSTALL_CMD'");
expect($command)->toContain("--secret 'id=RAILPACK_DEPLOY_APT_PACKAGES,env=RAILPACK_DEPLOY_APT_PACKAGES'");
@@ -247,3 +252,31 @@ it('builds railpack docker command with matching env and secret flags for all ra
expect($command)->toContain(' --build-arg secrets-hash=');
expect($command)->toContain('--build-arg BUILDKIT_SYNTAX="ghcr.io/railwayapp/railpack-frontend:v'.config('constants.coolify.railpack_version').'"');
});
+
+it('interpolates build-time variable references for railpack by sourcing the build-time env file', function () {
+ [$job, $reflection] = makeRailpackDeploymentJob([
+ 'uuid' => 'application-uuid',
+ ]);
+
+ // Mirrors the issue: BETTER_AUTH_URL=$COOLIFY_URL must be interpolated at build time.
+ $command = invokeRailpackMethod(
+ $job,
+ $reflection,
+ 'railpack_build_command',
+ [
+ 'coollabsio/coolify:test',
+ collect([
+ 'BETTER_AUTH_URL' => '$COOLIFY_URL',
+ 'COOLIFY_URL' => 'https://sapere-10.bobman.dev',
+ ]),
+ ],
+ );
+
+ // The literal `$COOLIFY_URL` must NOT be forwarded inline; it is resolved by the shell
+ // after sourcing the build-time .env file, then read through the build secret.
+ expect($command)->toContain('set -a && source /artifacts/build-time.env && set +a');
+ expect($command)->not->toContain("'BETTER_AUTH_URL=\$COOLIFY_URL'");
+ expect($command)->not->toContain("env 'BETTER_AUTH_URL");
+ expect($command)->toContain("--secret 'id=BETTER_AUTH_URL,env=BETTER_AUTH_URL'");
+ expect($command)->toContain("--secret 'id=COOLIFY_URL,env=COOLIFY_URL'");
+});