Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds multi-arch support #44

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions commands/package_buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func PackageBundleCommand() *cobra.Command {
p.InferBuildpackVersion()
}

if p.RegistryName == "" {
p.RegistryName = p.BuildpackID
}

err := p.Execute()
if err != nil {
log.Fatal(err)
Expand All @@ -61,6 +65,8 @@ func PackageBundleCommand() *cobra.Command {
packageBuildpackCmd.Flags().BoolVar(&p.IncludeDependencies, "include-dependencies", false, "whether to include dependencies (default: false)")
packageBuildpackCmd.Flags().StringArrayVar(&p.DependencyFilters, "dependency-filter", []string{}, "one or more filters that are applied to exclude dependencies")
packageBuildpackCmd.Flags().BoolVar(&p.StrictDependencyFilters, "strict-filters", false, "require filter to match all data or just some data (default: false)")
packageBuildpackCmd.Flags().StringVar(&p.RegistryName, "registry-name", "", "prefix for the registry to publish to (default: your buildpack id)")
packageBuildpackCmd.Flags().BoolVar(&p.Publish, "publish", false, "publish the buildpack to a buildpack registry (default: false)")

return packageBuildpackCmd
}
47 changes: 38 additions & 9 deletions packager/buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/buildpacks/libcnb/v2"
Expand Down Expand Up @@ -51,6 +52,12 @@ type BundleBuildpack struct {
// IncludeDependencies indicates whether to include dependencies in build package.
IncludeDependencies bool

// RegistryName is the prefix to use when publishing the buildpack
RegistryName string

// Publish indicates whether to publish the buildpack to the registry
Publish bool

executor effect.Executor
exitHandler libcnb.ExitHandler
}
Expand Down Expand Up @@ -173,17 +180,30 @@ func (p *BundleBuildpack) ExecutePackage(tmpDir string) error {
pullPolicy = "if-not-present"
}

imageName := p.BuildpackID
if p.RegistryName != "" {
imageName = p.RegistryName
}

args := []string{
"buildpack",
"package",
imageName,
"--pull-policy", pullPolicy,
}

if p.Publish {
args = append(args, "--publish")
} else {
args = append(args, "--target", archFromSystem())
}

err := p.executor.Execute(effect.Execution{
Command: "pack",
Args: []string{
"buildpack",
"package",
p.BuildpackID,
"--pull-policy", pullPolicy,
},
Stdout: os.Stdout,
Stderr: os.Stderr,
Dir: tmpDir,
Args: args,
Stdout: os.Stdout,
Stderr: os.Stderr,
Dir: tmpDir,
})
if err != nil {
return fmt.Errorf("unable to execute `pack buildpack package` command\n%w", err)
Expand Down Expand Up @@ -239,3 +259,12 @@ func (p *BundleBuildpack) Execute() error {

return nil
}

func archFromSystem() string {
archFromEnv, ok := os.LookupEnv("BP_ARCH")
if !ok {
archFromEnv = runtime.GOARCH
}

return "linux/" + archFromEnv
}
32 changes: 31 additions & 1 deletion packager/buildpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ func testBuildpack(t *testing.T, context spec.G, it spec.S) {

it.Before(func() {
mockExecutor = &mocks.Executor{}

t.Setenv("BP_ARCH", "amd64")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, you want to make sure the test runs whichever the platform it runs on

})

it("defaults pull policy to if-not-present", func() {
Expand All @@ -256,6 +258,8 @@ func testBuildpack(t *testing.T, context spec.G, it spec.S) {
e.Args[2] == "some-id" &&
e.Args[3] == "--pull-policy" &&
e.Args[4] == "if-not-present" &&
e.Args[5] == "--target" &&
e.Args[6] == "linux/amd64" &&
e.Dir == "/some/path"
})).Return(nil)

Expand All @@ -265,6 +269,26 @@ func testBuildpack(t *testing.T, context spec.G, it spec.S) {
Expect(p.ExecutePackage("/some/path")).To(Succeed())
})

it("include registry prefix if set", func() {
mockExecutor.On("Execute", mock.MatchedBy(func(e effect.Execution) bool {
return e.Command == "pack" &&
e.Args[0] == "buildpack" &&
e.Args[1] == "package" &&
e.Args[2] == "docker.io/some-other-id/image" &&
e.Args[3] == "--pull-policy" &&
e.Args[4] == "if-not-present" &&
e.Args[5] == "--publish" &&
e.Dir == "/some/path"
})).Return(nil)

p := packager.NewBundleBuildpackForTests(mockExecutor, nil)
p.BuildpackID = "some-id"
p.RegistryName = "docker.io/some-other-id/image"
p.Publish = true

Expect(p.ExecutePackage("/some/path")).To(Succeed())
})

context("BP_PULL_POLICY is set", func() {
it.Before(func() {
t.Setenv("BP_PULL_POLICY", "always")
Expand All @@ -278,6 +302,8 @@ func testBuildpack(t *testing.T, context spec.G, it spec.S) {
e.Args[2] == "some-id" &&
e.Args[3] == "--pull-policy" &&
e.Args[4] == "always" &&
e.Args[5] == "--target" &&
e.Args[6] == "linux/amd64" &&
e.Dir == "/some/path"
})).Return(nil)

Expand All @@ -298,16 +324,20 @@ func testBuildpack(t *testing.T, context spec.G, it spec.S) {
it.Before(func() {
mockExecutor = &mocks.Executor{}
mockExitHandler = exMocks.ExitHandler{}

t.Setenv("BP_ARCH", "amd64")
})

it("", func() {
it("compiles the package", func() {
mockExecutor.On("Execute", mock.MatchedBy(func(e effect.Execution) bool {
return e.Command == "pack" &&
e.Args[0] == "buildpack" &&
e.Args[1] == "package" &&
e.Args[2] == "some-id" &&
e.Args[3] == "--pull-policy" &&
e.Args[4] == "if-not-present" &&
e.Args[5] == "--target" &&
e.Args[6] == "linux/amd64" &&
e.Dir == "/some/path"
})).Return(nil)

Expand Down