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

Use pack CLI to build on GCB #3503

Merged
merged 2 commits into from
Jan 15, 2020
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
19 changes: 19 additions & 0 deletions deploy/buildpacks/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2019 The Skaffold Authors All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM alpine:3.10

ARG PACK_VERSION
ENV PACK_URL https://github.com/buildpack/pack/releases/download/${PACK_VERSION}/pack-${PACK_VERSION}-linux.tgz
RUN wget -O- "${PACK_URL}" | tar -C /usr/local/bin -xz pack
23 changes: 23 additions & 0 deletions deploy/buildpacks/publish.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

# Copyright 2019 The Skaffold Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

PACK_VERSION=v0.6.0

docker build . --build-arg PACK_VERSION=${PACK_VERSION} -t gcr.io/k8s-skaffold/pack:${PACK_VERSION} -t gcr.io/k8s-skaffold/pack:latest
docker push gcr.io/k8s-skaffold/pack:${PACK_VERSION}
docker push gcr.io/k8s-skaffold/pack:latest
Copy link
Contributor

Choose a reason for hiding this comment

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

Does heroku publish a container image that we can re-use or any plans in future to do so? Or else, skaffold team will have to keep the track of publishing the new image everytime pack is upgraded (which is fine for now)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I haven't seen such plan.

7 changes: 7 additions & 0 deletions docs/content/en/schemas/v2alpha2.json
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,12 @@
"x-intellij-html-description": "image that runs a Maven build. See <a href=\"https://cloud.google.com/cloud-build/docs/cloud-builders\">Cloud Builders</a>.",
"default": "gcr.io/cloud-builders/mvn"
},
"packImage": {
"type": "string",
"description": "image that runs a Cloud Native Buildpacks build. See [Cloud Builders](https://cloud.google.com/cloud-build/docs/cloud-builders).",
"x-intellij-html-description": "image that runs a Cloud Native Buildpacks build. See <a href=\"https://cloud.google.com/cloud-build/docs/cloud-builders\">Cloud Builders</a>.",
"default": "gcr.io/k8s-skaffold/pack"
},
"projectId": {
"type": "string",
"description": "ID of your Cloud Platform Project. If it is not provided, Skaffold will guess it from the image name. For example, given the artifact image name `gcr.io/myproject/image`, Skaffold will use the `myproject` GCP project.",
Expand All @@ -1041,6 +1047,7 @@
"kanikoImage",
"mavenImage",
"gradleImage",
"packImage",
"concurrency"
],
"additionalProperties": false,
Expand Down
59 changes: 13 additions & 46 deletions pkg/skaffold/build/gcb/buildpacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,59 +25,26 @@ import (
)

func (b *Builder) buildpackBuildSpec(artifact *latest.BuildpackArtifact, tag string) (cloudbuild.Build, error) {
args := []string{"pack", "build", tag, "--builder", artifact.Builder}

if artifact.RunImage != "" {
args = append(args, "--run-image", artifact.RunImage)
}

env, err := misc.EvaluateEnv(artifact.Env)
if err != nil {
return cloudbuild.Build{}, errors.Wrap(err, "unable to evaluate env variables")
}

steps := []*cloudbuild.BuildStep{
{
Name: artifact.Builder,
Args: []string{"sh", "-c", "chown -R $$CNB_USER_ID:$$CNB_GROUP_ID /workspace /layers $$HOME"},
},
{
Name: artifact.Builder,
Entrypoint: "/lifecycle/detector",
Env: env,
},
{
Name: artifact.Builder,
Entrypoint: "/lifecycle/analyzer",
Args: []string{tag},
},
{
Name: artifact.Builder,
Entrypoint: "/lifecycle/builder",
Env: env,
},
}

if artifact.RunImage == "" {
steps = append(steps,
&cloudbuild.BuildStep{
Name: artifact.Builder,
Entrypoint: "/lifecycle/exporter",
Args: []string{tag},
},
)
} else {
steps = append(steps,
&cloudbuild.BuildStep{
Name: b.DockerImage,
Args: []string{"pull", artifact.RunImage},
},
&cloudbuild.BuildStep{
Name: artifact.Builder,
Entrypoint: "/lifecycle/exporter",
Args: []string{"-image", artifact.RunImage, tag},
},
)
for _, kv := range env {
args = append(args, "--env", kv)
}

return cloudbuild.Build{
Options: &cloudbuild.BuildOptions{
Volumes: []*cloudbuild.Volume{{Name: "layers", Path: "/layers"}},
},
Steps: steps,
Steps: []*cloudbuild.BuildStep{{
Name: b.PackImage,
Args: args,
}},
Images: []string{tag},
}, nil
}
88 changes: 25 additions & 63 deletions pkg/skaffold/build/gcb/buildpacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,29 @@ func TestBuildpackBuildSpec(t *testing.T) {
}{
{
description: "default run image",
artifact: &latest.BuildpackArtifact{
Builder: "builder",
},
expected: cloudbuild.Build{
Steps: []*cloudbuild.BuildStep{{
Name: "pack/image",
Args: []string{"pack", "build", "img", "--builder", "builder"},
}},
Images: []string{"img"},
},
},
{
description: "env variables",
artifact: &latest.BuildpackArtifact{
Builder: "builder",
Env: []string{"KEY=VALUE", "FOO={{.BAR}}"},
},
expected: cloudbuild.Build{
Options: &cloudbuild.BuildOptions{
Volumes: []*cloudbuild.Volume{{Name: "layers", Path: "/layers"}},
},
Steps: []*cloudbuild.BuildStep{
{
Name: "builder",
Args: []string{"sh", "-c", "chown -R $$CNB_USER_ID:$$CNB_GROUP_ID /workspace /layers $$HOME"},
},
{
Name: "builder",
Entrypoint: "/lifecycle/detector",
Env: []string{"KEY=VALUE", "FOO=bar"},
},
{
Name: "builder",
Entrypoint: "/lifecycle/analyzer",
Args: []string{"img"},
},
{
Name: "builder",
Entrypoint: "/lifecycle/builder",
Env: []string{"KEY=VALUE", "FOO=bar"},
},
{
Name: "builder",
Entrypoint: "/lifecycle/exporter",
Args: []string{"img"},
},
},
Steps: []*cloudbuild.BuildStep{{
Name: "pack/image",
Args: []string{"pack", "build", "img", "--builder", "builder", "--env", "KEY=VALUE", "--env", "FOO=bar"},
}},
Images: []string{"img"},
},
},
{
Expand All @@ -78,37 +67,11 @@ func TestBuildpackBuildSpec(t *testing.T) {
RunImage: "run/image",
},
expected: cloudbuild.Build{
Options: &cloudbuild.BuildOptions{
Volumes: []*cloudbuild.Volume{{Name: "layers", Path: "/layers"}},
},
Steps: []*cloudbuild.BuildStep{
{
Name: "otherbuilder",
Args: []string{"sh", "-c", "chown -R $$CNB_USER_ID:$$CNB_GROUP_ID /workspace /layers $$HOME"},
},
{
Name: "otherbuilder",
Entrypoint: "/lifecycle/detector",
},
{
Name: "otherbuilder",
Entrypoint: "/lifecycle/analyzer",
Args: []string{"img"},
},
{
Name: "otherbuilder",
Entrypoint: "/lifecycle/builder",
},
{
Name: "docker/docker",
Args: []string{"pull", "run/image"},
},
{
Name: "otherbuilder",
Entrypoint: "/lifecycle/exporter",
Args: []string{"-image", "run/image", "img"},
},
},
Steps: []*cloudbuild.BuildStep{{
Name: "pack/image",
Args: []string{"pack", "build", "img", "--builder", "otherbuilder", "--run-image", "run/image"},
}},
Images: []string{"img"},
},
},
{
Expand All @@ -131,15 +94,14 @@ func TestBuildpackBuildSpec(t *testing.T) {
}

builder := newBuilder(latest.GoogleCloudBuild{
DockerImage: "docker/docker",
PackImage: "pack/image",
})
buildSpec, err := builder.buildSpec(artifact, "img", "bucket", "object")
t.CheckError(test.shouldErr, err)

if !test.shouldErr {
t.CheckDeepEqual(test.expected.Steps, buildSpec.Steps)
t.CheckDeepEqual(test.expected.Options.Volumes, buildSpec.Options.Volumes)
t.CheckEmpty(buildSpec.Images)
t.CheckDeepEqual(test.expected.Images, buildSpec.Images)
}
})
}
Expand Down
1 change: 1 addition & 0 deletions pkg/skaffold/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const (
DefaultCloudBuildMavenImage = "gcr.io/cloud-builders/mvn"
DefaultCloudBuildGradleImage = "gcr.io/cloud-builders/gradle"
DefaultCloudBuildKanikoImage = "gcr.io/kaniko-project/executor"
DefaultCloudBuildPackImage = "gcr.io/k8s-skaffold/pack"
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: can we move this gcb/buildpacks.go as a local constant if there are cyclic dependencies issues?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll move all those default image names next to where they are used in another PR


DefaultSkaffoldDir = ".skaffold"
DefaultCacheFile = "cache"
Expand Down
5 changes: 5 additions & 0 deletions pkg/skaffold/schema/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func Set(c *latest.SkaffoldConfig) error {
setDefaultCloudBuildMavenImage,
setDefaultCloudBuildGradleImage,
setDefaultCloudBuildKanikoImage,
setDefaultCloudBuildPackImage,
)

if err := withClusterConfig(c,
Expand Down Expand Up @@ -145,6 +146,10 @@ func setDefaultCloudBuildKanikoImage(gcb *latest.GoogleCloudBuild) {
gcb.KanikoImage = valueOrDefault(gcb.KanikoImage, constants.DefaultCloudBuildKanikoImage)
}

func setDefaultCloudBuildPackImage(gcb *latest.GoogleCloudBuild) {
gcb.PackImage = valueOrDefault(gcb.PackImage, constants.DefaultCloudBuildPackImage)
}

func setDefaultTagger(c *latest.SkaffoldConfig) {
if c.Build.TagPolicy != (latest.TagPolicy{}) {
return
Expand Down
1 change: 1 addition & 0 deletions pkg/skaffold/schema/defaults/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ func TestSetDefaultsOnCloudBuild(t *testing.T) {
testutil.CheckDeepEqual(t, constants.DefaultCloudBuildDockerImage, cfg.Build.GoogleCloudBuild.DockerImage)
testutil.CheckDeepEqual(t, constants.DefaultCloudBuildMavenImage, cfg.Build.GoogleCloudBuild.MavenImage)
testutil.CheckDeepEqual(t, constants.DefaultCloudBuildGradleImage, cfg.Build.GoogleCloudBuild.GradleImage)
testutil.CheckDeepEqual(t, constants.DefaultCloudBuildPackImage, cfg.Build.GoogleCloudBuild.PackImage)
}

func TestSetDefaultsOnLocalBuild(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions pkg/skaffold/schema/latest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ type GoogleCloudBuild struct {
// Defaults to `gcr.io/cloud-builders/gradle`.
GradleImage string `yaml:"gradleImage,omitempty"`

// PackImage is the image that runs a Cloud Native Buildpacks build.
// See [Cloud Builders](https://cloud.google.com/cloud-build/docs/cloud-builders).
// Defaults to `gcr.io/k8s-skaffold/pack`.
PackImage string `yaml:"packImage,omitempty"`

// Concurrency is how many artifacts can be built concurrently. 0 means "no-limit"
// Defaults to 0.
Concurrency int `yaml:"concurrency,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions pkg/skaffold/schema/profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func TestApplyProfiles(t *testing.T) {
MavenImage: "gcr.io/cloud-builders/mvn",
GradleImage: "gcr.io/cloud-builders/gradle",
KanikoImage: "gcr.io/kaniko-project/executor",
PackImage: "gcr.io/k8s-skaffold/pack",
},
},
},
Expand Down
1 change: 1 addition & 0 deletions pkg/skaffold/schema/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ func withGoogleCloudBuild(id string, ops ...func(*latest.BuildConfig)) func(*lat
MavenImage: "gcr.io/cloud-builders/mvn",
GradleImage: "gcr.io/cloud-builders/gradle",
KanikoImage: "gcr.io/kaniko-project/executor",
PackImage: "gcr.io/k8s-skaffold/pack",
}}}
for _, op := range ops {
op(&b)
Expand Down