Skip to content

Commit 9fad270

Browse files
committed
Fix release YAML pollution from runtime Go compilation
Fixes #1208 where deploying via git URL buildpack fails with: 'buildpack's release output invalid: yaml: line 4: mapping values are not allowed in this context' Root cause: bin/detect and bin/release wrapper scripts compiled Go binaries at runtime, polluting stdout with build messages before YAML output. Cloud Foundry expects pure YAML on stdout during the release phase. Solution follows the pattern from Java buildpack (commit 11e9bfc5): - Convert bin/detect to pure bash (no runtime compilation) - Simplify bin/release to just 'cat' pre-generated YAML file - Generate release YAML in finalize phase (finalize.go) - Remove obsolete src/php/detect and src/php/release Go CLIs - Add regression test for git URL deployments Changes: - bin/detect: Pure bash detection logic (~40 lines) - bin/release: Cat YAML file from tmp/php-buildpack-release-step.yml - finalize.go: Write release YAML during finalize phase - Deleted: src/php/detect/, src/php/release/ directories - Added integration test for git URL buildpack deployment Net result: -70 lines of code, clean YAML output, no pollution
1 parent 6c10bb2 commit 9fad270

7 files changed

Lines changed: 73 additions & 117 deletions

File tree

bin/detect

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
11
#!/bin/bash
2-
set -euo pipefail
2+
# Cloud Foundry PHP Buildpack - Detect Script
3+
# Pure bash implementation matching Ruby/Java buildpack pattern
4+
5+
set -e
36

47
BUILD_DIR=$1
8+
BUILDPACK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
9+
VERSION=$(cat "$BUILDPACK_DIR/VERSION" 2>/dev/null || echo "unknown")
10+
11+
# 1. Check for composer.json (most common indicator)
12+
[ -f "$BUILD_DIR/composer.json" ] && echo "php $VERSION" && exit 0
13+
14+
# 2. Check for .php files
15+
compgen -G "$BUILD_DIR/*.php" > /dev/null 2>&1 && echo "php $VERSION" && exit 0
16+
17+
# 3. Check for common web directories with PHP files
18+
for webdir in htdocs public web www; do
19+
if [ -d "$BUILD_DIR/$webdir" ]; then
20+
compgen -G "$BUILD_DIR/$webdir/*.php" > /dev/null 2>&1 && echo "php $VERSION" && exit 0
21+
fi
22+
done
523

6-
export BUILDPACK_DIR=`dirname $(readlink -f ${BASH_SOURCE%/*})`
7-
source "$BUILDPACK_DIR/scripts/install_go.sh"
8-
output_dir=$(mktemp -d -t detectXXX)
24+
# 4. Check for index.php in subdirectories (common pattern)
25+
[ -f "$BUILD_DIR/htdocs/index.php" ] && echo "php $VERSION" && exit 0
26+
[ -f "$BUILD_DIR/public/index.php" ] && echo "php $VERSION" && exit 0
27+
[ -f "$BUILD_DIR/web/index.php" ] && echo "php $VERSION" && exit 0
28+
[ -f "$BUILD_DIR/www/index.php" ] && echo "php $VERSION" && exit 0
929

10-
pushd $BUILDPACK_DIR
11-
echo "-----> Running go build detect"
12-
GOROOT=$GoInstallDir $GoInstallDir/bin/go build -mod=vendor -o $output_dir/detect ./src/php/detect/cli
13-
popd
30+
# 5. Check for PHP files recursively (slower, last resort)
31+
if find "$BUILD_DIR" -maxdepth 3 -name "*.php" -type f 2>/dev/null | head -1 | grep -q .; then
32+
echo "php $VERSION"
33+
exit 0
34+
fi
1435

15-
$output_dir/detect "$BUILD_DIR"
36+
# Not a PHP app
37+
exit 1

bin/release

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,4 @@
22
set -euo pipefail
33

44
BUILD_DIR=$1
5-
6-
export BUILDPACK_DIR=`dirname $(readlink -f ${BASH_SOURCE%/*})`
7-
source "$BUILDPACK_DIR/scripts/install_go.sh"
8-
output_dir=$(mktemp -d -t releaseXXX)
9-
10-
pushd $BUILDPACK_DIR
11-
echo "-----> Running go build release"
12-
GOROOT=$GoInstallDir $GoInstallDir/bin/go build -mod=vendor -o $output_dir/release ./src/php/release/cli
13-
popd
14-
15-
$output_dir/release "$BUILD_DIR"
5+
cat $BUILD_DIR/tmp/php-buildpack-release-step.yml

src/php/detect/cli/main.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/php/detect/detect.go

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/php/finalize/finalize.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,21 @@ func Run(f *Finalizer) error {
138138
return err
139139
}
140140

141+
// Write release YAML file for bin/release to consume
142+
tmpDir := filepath.Join(f.Stager.BuildDir(), "tmp")
143+
if err := os.MkdirAll(tmpDir, 0755); err != nil {
144+
return fmt.Errorf("failed to create tmp directory: %w", err)
145+
}
146+
147+
releaseYamlPath := filepath.Join(tmpDir, "php-buildpack-release-step.yml")
148+
yamlContent := `---
149+
default_process_types:
150+
web: $HOME/.bp/bin/start
151+
`
152+
if err := os.WriteFile(releaseYamlPath, []byte(yamlContent), 0644); err != nil {
153+
return fmt.Errorf("failed to write release YAML: %w", err)
154+
}
155+
141156
f.Log.Info("PHP buildpack finalize phase complete")
142157
return nil
143158
}

src/php/integration/default_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,31 @@ func testDefault(platform switchblade.Platform, fixtures string) func(*testing.T
9191
})
9292
})
9393

94+
context("PHP app deployed via git URL buildpack", func() {
95+
it("successfully builds without release YAML pollution", func() {
96+
if settings.Platform == "docker" {
97+
t.Skip("Git URL buildpacks require CF platform - Docker platform cannot clone git repos")
98+
}
99+
100+
deployment, logs, err := platform.Deploy.
101+
WithBuildpacks("https://github.com/cloudfoundry/php-buildpack.git").
102+
WithEnv(map[string]string{
103+
"BP_DEBUG": "1",
104+
}).
105+
Execute(name, filepath.Join(fixtures, "default"))
106+
107+
Expect(err).NotTo(HaveOccurred(), logs.String)
108+
109+
Eventually(logs).Should(SatisfyAll(
110+
ContainLines("Installing PHP"),
111+
ContainLines(MatchRegexp(`PHP [\d\.]+`)),
112+
))
113+
114+
Eventually(deployment).Should(Serve(
115+
ContainSubstring("PHP Version"),
116+
))
117+
})
118+
})
119+
94120
}
95121
}

src/php/release/cli/main.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)