Skip to content

Commit

Permalink
Merge pull request #44 from paketo-buildpacks/arm64
Browse files Browse the repository at this point in the history
Adds multi-arch support
  • Loading branch information
dmikusa authored Aug 1, 2024
2 parents 17de984 + 8117849 commit 23260ff
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 10 deletions.
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")
})

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

0 comments on commit 23260ff

Please sign in to comment.