Skip to content

Commit

Permalink
Forward-port v1 changes to v2 (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Mikusa committed May 24, 2022
1 parent 5bb9c7b commit 5e36043
Show file tree
Hide file tree
Showing 16 changed files with 1,208 additions and 362 deletions.
2 changes: 1 addition & 1 deletion .github/pipeline-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.19.0
1.21.2
1 change: 0 additions & 1 deletion .github/pipelines-version

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/synchronize-labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on:
- ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: micnncim/action-label-syncer@v1
env:
GITHUB_TOKEN: ${{ secrets.IMPLEMENTATION_GITHUB_TOKEN }}
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ jobs:
runs-on:
- ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
- uses: actions/checkout@v3
- uses: actions/cache@v3
with:
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
path: ${{ env.HOME }}/go/pkg/mod
restore-keys: ${{ runner.os }}-go-
- uses: actions/setup-go@v2
- uses: actions/setup-go@v3
with:
go-version: "1.16"
go-version: "1.17"
- name: Install richgo
run: |
#!/usr/bin/env bash
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/update-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ jobs:
runs-on:
- ubuntu-latest
steps:
- uses: actions/setup-go@v2
- uses: actions/setup-go@v3
with:
go-version: "1.16"
go-version: "1.17"
- name: Install octo
run: |
#!/usr/bin/env bash
set -euo pipefail
GO111MODULE=on go get -u -ldflags="-s -w" github.com/paketo-buildpacks/pipeline-builder/cmd/octo
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Update Pipeline
id: pipeline
run: |
Expand Down Expand Up @@ -62,7 +62,7 @@ jobs:
env:
DESCRIPTOR: .github/pipeline-descriptor.yml
GITHUB_TOKEN: ${{ secrets.IMPLEMENTATION_GITHUB_TOKEN }}
- uses: peter-evans/create-pull-request@v3
- uses: peter-evans/create-pull-request@v4
with:
author: ${{ secrets.IMPLEMENTATION_GITHUB_USERNAME }} <${{ secrets.IMPLEMENTATION_GITHUB_USERNAME }}@users.noreply.github.com>
body: |-
Expand Down
78 changes: 62 additions & 16 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"strings"

"github.com/BurntSushi/toml"
"github.com/Masterminds/semver"

"github.com/buildpacks/libcnb/internal"
"github.com/buildpacks/libcnb/log"
Expand Down Expand Up @@ -77,6 +78,15 @@ type BuildResult struct {
Unmet []UnmetPlanEntry
}

// Constants to track minimum and maximum supported Buildpack API versions
const (
// MinSupportedBPVersion indicates the minium supported version of the Buildpacks API
MinSupportedBPVersion = "0.5"

// MaxSupportedBPVersion indicates the maximum supported version of the Buildpacks API
MaxSupportedBPVersion = "0.8"
)

// NewBuildResult creates a new BuildResult instance, initializing empty fields.
func NewBuildResult() BuildResult {
return BuildResult{
Expand Down Expand Up @@ -113,11 +123,6 @@ func Build(build BuildFunc, options ...Option) {
config = option(config)
}

if len(config.arguments) != 4 {
config.exitHandler.Error(fmt.Errorf("expected 3 arguments and received %d", len(config.arguments)-1))
return
}

var (
err error
file string
Expand Down Expand Up @@ -150,16 +155,49 @@ func Build(build BuildFunc, options ...Option) {
}
config.logger.Debugf("Buildpack: %+v", ctx.Buildpack)

API := strings.TrimSpace(ctx.Buildpack.API)
if API != "0.5" && API != "0.6" && API != "0.7" {
config.exitHandler.Error(errors.New("this version of libcnb is only compatible with buildpack APIs 0.5, 0.6, and 0.7"))
API, err := semver.NewVersion(ctx.Buildpack.API)
if err != nil {
config.exitHandler.Error(errors.New("version cannot be parsed"))
return
}

ctx.Layers = Layers{config.arguments[1]}
compatVersionCheck, _ := semver.NewConstraint(fmt.Sprintf(">= %s, <= %s", MinSupportedBPVersion, MaxSupportedBPVersion))
if !compatVersionCheck.Check(API) {
config.exitHandler.Error(fmt.Errorf("this version of libcnb is only compatible with buildpack APIs >= %s, <= %s", MinSupportedBPVersion, MaxSupportedBPVersion))
return
}

var buildpackPlanPath string

if API.LessThan(semver.MustParse("0.8")) {
if len(config.arguments) != 4 {
config.exitHandler.Error(fmt.Errorf("expected 3 arguments and received %d", len(config.arguments)-1))
return
}
ctx.Layers = Layers{config.arguments[1]}
ctx.Platform.Path = config.arguments[2]
buildpackPlanPath = config.arguments[3]
} else {
layersDir, ok := os.LookupEnv("CNB_LAYERS_DIR")
if !ok {
config.exitHandler.Error(fmt.Errorf("expected CNB_LAYERS_DIR to be set"))
return
}
ctx.Layers = Layers{layersDir}
ctx.Platform.Path, ok = os.LookupEnv("CNB_PLATFORM_DIR")
if !ok {
config.exitHandler.Error(fmt.Errorf("expected CNB_PLATFORM_DIR to be set"))
return
}
buildpackPlanPath, ok = os.LookupEnv("CNB_BP_PLAN_PATH")
if !ok {
config.exitHandler.Error(fmt.Errorf("expected CNB_BP_PLAN_PATH to be set"))
return
}
}

config.logger.Debugf("Layers: %+v", ctx.Layers)

ctx.Platform.Path = config.arguments[2]
if config.logger.IsDebugEnabled() {
config.logger.Debug(PlatformFormatter(ctx.Platform))
}
Expand All @@ -186,9 +224,8 @@ func Build(build BuildFunc, options ...Option) {
ctx.PersistentMetadata = store.Metadata
config.logger.Debugf("Persistent Metadata: %+v", ctx.PersistentMetadata)

file = config.arguments[3]
if _, err = toml.DecodeFile(file, &ctx.Plan); err != nil && !os.IsNotExist(err) {
config.exitHandler.Error(fmt.Errorf("unable to decode buildpack plan %s\n%w", file, err))
if _, err = toml.DecodeFile(buildpackPlanPath, &ctx.Plan); err != nil && !os.IsNotExist(err) {
config.exitHandler.Error(fmt.Errorf("unable to decode buildpack plan %s\n%w", buildpackPlanPath, err))
return
}
config.logger.Debugf("Buildpack Plan: %+v", ctx.Plan)
Expand Down Expand Up @@ -246,7 +283,7 @@ func Build(build BuildFunc, options ...Option) {
file = filepath.Join(ctx.Layers.Path, fmt.Sprintf("%s.toml", layer.Name))
config.logger.Debugf("Writing layer metadata: %s <= %+v", file, layer)
var toWrite interface{} = layer
if API == "0.5" {
if API.Equal(semver.MustParse("0.5")) {
toWrite = internal.LayerAPI5{
Build: layer.LayerTypes.Build,
Cache: layer.LayerTypes.Cache,
Expand Down Expand Up @@ -274,7 +311,7 @@ func Build(build BuildFunc, options ...Option) {
}
}

if API != "0.5" && API != "0.6" {
if API.GreaterThan(semver.MustParse("0.7")) || API.Equal(semver.MustParse("0.7")) {
if err := validateSBOMFormats(ctx.Layers.Path, ctx.Buildpack.Info.SBOMFormats); err != nil {
config.exitHandler.Error(fmt.Errorf("unable to validate SBOM\n%w", err))
return
Expand All @@ -291,14 +328,23 @@ func Build(build BuildFunc, options ...Option) {
file = filepath.Join(ctx.Layers.Path, "launch.toml")
config.logger.Debugf("Writing application metadata: %s <= %+v", file, launch)

if API == "0.5" {
if API.LessThan(semver.MustParse("0.6")) {
for _, process := range launch.Processes {
if process.Default {
config.exitHandler.Error(fmt.Errorf("unable to set default=true as that is not supported until API version 0.6"))
}
}
}

if API.LessThan(semver.MustParse("0.8")) {
for i, process := range launch.Processes {
if process.WorkingDirectory != "" {
config.exitHandler.Error(fmt.Errorf("unable to set working-directory=%s because that is not supported until API version 0.8", process.WorkingDirectory))
launch.Processes[i].WorkingDirectory = ""
}
}
}

if err = config.tomlWriter.Write(file, launch); err != nil {
config.exitHandler.Error(fmt.Errorf("unable to write application metadata %s\n%w", file, err))
return
Expand Down
Loading

0 comments on commit 5e36043

Please sign in to comment.