Skip to content

Commit

Permalink
Merge pull request #1478 from dgageot/jib-gcb
Browse files Browse the repository at this point in the history
Support Jib on Google Cloud Build
  • Loading branch information
dgageot authored Jan 17, 2019
2 parents d574e59 + c5c98c5 commit f302cbe
Show file tree
Hide file tree
Showing 28 changed files with 630 additions and 61 deletions.
6 changes: 4 additions & 2 deletions integration/examples/annotated-skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v1beta2
apiVersion: skaffold/v1beta3
kind: Config
build:
# tagPolicy (beta) determines how Skaffold is going to tag your images.
Expand Down Expand Up @@ -100,7 +100,7 @@ build:
# useDockerCLI: false
# useBuildkit: false

# Docker artifacts can be built on Google Cloud Build. The projectId then needs
# Docker and Jib artifacts can be built on Google Cloud Build. The projectId then needs
# to be provided and the currently logged user should be given permissions to trigger
# new builds on Cloud Build.
# If the projectId is not provided, Skaffold will try to guess it from the image name.
Expand All @@ -113,6 +113,8 @@ build:
# machineType: "N1_HIGHCPU_8"|"N1_HIGHCPU_32"
# timeout: 10000s
# dockerImage: gcr.io/cloud-builders/docker
# mavenImage: gcr.io/cloud-builders/mvn
# gradleImage: gcr.io/cloud-builders/gradle

# Docker artifacts can be built on a Kubernetes cluster with Kaniko.
# Exactly one buildContext must be specified to use kaniko
Expand Down
2 changes: 1 addition & 1 deletion integration/examples/bazel/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v1beta2
apiVersion: skaffold/v1beta3
kind: Config
build:
artifacts:
Expand Down
2 changes: 1 addition & 1 deletion integration/examples/getting-started/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v1beta2
apiVersion: skaffold/v1beta3
kind: Config
build:
artifacts:
Expand Down
2 changes: 1 addition & 1 deletion integration/examples/helm-deployment/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v1beta2
apiVersion: skaffold/v1beta3
kind: Config
build:
tagPolicy:
Expand Down
2 changes: 1 addition & 1 deletion integration/examples/hot-reload/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v1beta2
apiVersion: skaffold/v1beta3
kind: Config
build:
artifacts:
Expand Down
8 changes: 7 additions & 1 deletion integration/examples/jib/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
apiVersion: skaffold/v1beta2
apiVersion: skaffold/v1beta3
kind: Config
build:
artifacts:
- image: gcr.io/k8s-skaffold/skaffold-jib
jibMaven: {}

# optional profile to run the jib build on Google Cloud Build
profiles:
- name: gcb
build:
googleCloudBuild: {}
2 changes: 1 addition & 1 deletion integration/examples/kaniko-local/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v1beta2
apiVersion: skaffold/v1beta3
kind: Config
build:
artifacts:
Expand Down
2 changes: 1 addition & 1 deletion integration/examples/kaniko/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v1beta2
apiVersion: skaffold/v1beta3
kind: Config
build:
artifacts:
Expand Down
2 changes: 1 addition & 1 deletion integration/examples/kustomize/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v1beta2
apiVersion: skaffold/v1beta3
kind: Config
deploy:
kustomize: {}
2 changes: 1 addition & 1 deletion integration/examples/microservices/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v1beta2
apiVersion: skaffold/v1beta3
kind: Config
build:
artifacts:
Expand Down
2 changes: 1 addition & 1 deletion integration/examples/nodejs/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v1beta2
apiVersion: skaffold/v1beta3
kind: Config
build:
artifacts:
Expand Down
2 changes: 1 addition & 1 deletion integration/examples/structure-tests/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v1beta2
apiVersion: skaffold/v1beta3
kind: Config
build:
artifacts:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v1beta2
apiVersion: skaffold/v1beta3
kind: Config
build:
artifacts:
Expand Down
2 changes: 1 addition & 1 deletion integration/examples/test-dev-job/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v1beta2
apiVersion: skaffold/v1beta3
kind: Config
build:
artifacts:
Expand Down
4 changes: 2 additions & 2 deletions pkg/skaffold/build/gcb/desc.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ func (b *Builder) buildSteps(artifact *latest.Artifact) ([]*cloudbuild.BuildStep
return nil, errors.New("skaffold can't build a bazel artifact with Google Cloud Build")

case artifact.JibMavenArtifact != nil:
return nil, errors.New("skaffold can't build a jib maven artifact with Google Cloud Build")
return b.jibMavenBuildSteps(artifact.ImageName, artifact.JibMavenArtifact), nil

case artifact.JibGradleArtifact != nil:
return nil, errors.New("skaffold can't build a jib gradle artifact with Google Cloud Build")
return b.jibGradleBuildSteps(artifact.ImageName, artifact.JibGradleArtifact), nil

default:
return nil, fmt.Errorf("undefined artifact type: %+v", artifact.ArtifactType)
Expand Down
30 changes: 0 additions & 30 deletions pkg/skaffold/build/gcb/desc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,6 @@ import (
"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestBuildJibMavenDescriptionFail(t *testing.T) {
artifact := &latest.Artifact{
ArtifactType: latest.ArtifactType{
JibMavenArtifact: &latest.JibMavenArtifact{},
},
}

builder := Builder{
GoogleCloudBuild: &latest.GoogleCloudBuild{},
}
_, err := builder.buildDescription(artifact, "bucket", "object")

testutil.CheckError(t, true, err)
}

func TestBuildJibGradleDescriptionFail(t *testing.T) {
artifact := &latest.Artifact{
ArtifactType: latest.ArtifactType{
JibGradleArtifact: &latest.JibGradleArtifact{},
},
}

builder := Builder{
GoogleCloudBuild: &latest.GoogleCloudBuild{},
}
_, err := builder.buildDescription(artifact, "bucket", "object")

testutil.CheckError(t, true, err)
}

func TestBuildBazelDescriptionFail(t *testing.T) {
artifact := &latest.Artifact{
ArtifactType: latest.ArtifactType{
Expand Down
38 changes: 38 additions & 0 deletions pkg/skaffold/build/gcb/jib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2018 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.
*/

package gcb

import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/jib"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
cloudbuild "google.golang.org/api/cloudbuild/v1"
)

// TODO(dgageot): check that `package` is bound to `jib:build`
func (b *Builder) jibMavenBuildSteps(imageName string, artifact *latest.JibMavenArtifact) []*cloudbuild.BuildStep {
return []*cloudbuild.BuildStep{{
Name: b.MavenImage,
Args: jib.GenerateMavenArgs("dockerBuild", imageName, artifact),
}}
}

func (b *Builder) jibGradleBuildSteps(imageName string, artifact *latest.JibGradleArtifact) []*cloudbuild.BuildStep {
return []*cloudbuild.BuildStep{{
Name: b.GradleImage,
Args: jib.GenerateGradleArgs("jibDockerBuild", imageName, artifact),
}}
}
61 changes: 61 additions & 0 deletions pkg/skaffold/build/gcb/jib_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2018 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.
*/

package gcb

import (
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/testutil"
cloudbuild "google.golang.org/api/cloudbuild/v1"
)

func TestJibMavenBuildSteps(t *testing.T) {
artifact := &latest.JibMavenArtifact{}

builder := Builder{
GoogleCloudBuild: &latest.GoogleCloudBuild{
MavenImage: "maven:3.6.0",
},
}
steps := builder.jibMavenBuildSteps("img", artifact)

expected := []*cloudbuild.BuildStep{{
Name: "maven:3.6.0",
Args: []string{"--non-recursive", "prepare-package", "jib:dockerBuild", "-Dimage=img"},
}}

testutil.CheckDeepEqual(t, expected, steps)
}

func TestJibGradleBuildSteps(t *testing.T) {
artifact := &latest.JibGradleArtifact{}

builder := Builder{
GoogleCloudBuild: &latest.GoogleCloudBuild{
GradleImage: "gradle:5.1.1",
},
}
steps := builder.jibGradleBuildSteps("img", artifact)

expected := []*cloudbuild.BuildStep{{
Name: "gradle:5.1.1",
Args: []string{":jibDockerBuild", "--image=img"},
}}

testutil.CheckDeepEqual(t, expected, steps)
}
2 changes: 2 additions & 0 deletions pkg/skaffold/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ const (
UpdateCheckEnvironmentVariable = "SKAFFOLD_UPDATE_CHECK"

DefaultCloudBuildDockerImage = "gcr.io/cloud-builders/docker"
DefaultCloudBuildMavenImage = "gcr.io/cloud-builders/mvn"
DefaultCloudBuildGradleImage = "gcr.io/cloud-builders/gradle"

// A regex matching valid repository names (https://github.com/docker/distribution/blob/master/reference/reference.go)
RepositoryComponentRegex string = `^[a-z\d]+(?:(?:[_.]|__|-+)[a-z\d]+)*$`
Expand Down
44 changes: 34 additions & 10 deletions pkg/skaffold/schema/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,30 @@ package defaults
import (
"fmt"

homedir "github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
kubectx "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
homedir "github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// Set makes sure default values are set on a SkaffoldPipeline.
func Set(c *latest.SkaffoldPipeline) error {
defaultToLocalBuild(c)
defaultToKubectlDeploy(c)
setDefaultCloudBuildDockerImage(c)
setDefaultTagger(c)
setDefaultKustomizePath(c)
setDefaultKubectlManifests(c)

if err := withCloudBuildConfig(c,
setDefaultCloudBuildDockerImage,
setDefaultCloudBuildMavenImage,
setDefaultCloudBuildGradleImage,
); err != nil {
return err
}

if err := withKanikoConfig(c,
setDefaultKanikoTimeout,
setDefaultKanikoImage,
Expand Down Expand Up @@ -75,13 +81,31 @@ func defaultToKubectlDeploy(c *latest.SkaffoldPipeline) {
c.Deploy.DeployType.KubectlDeploy = &latest.KubectlDeploy{}
}

func setDefaultCloudBuildDockerImage(c *latest.SkaffoldPipeline) {
cloudBuild := c.Build.BuildType.GoogleCloudBuild
if cloudBuild == nil {
return
func withCloudBuildConfig(c *latest.SkaffoldPipeline, operations ...func(kaniko *latest.GoogleCloudBuild) error) error {
if gcb := c.Build.GoogleCloudBuild; gcb != nil {
for _, operation := range operations {
if err := operation(gcb); err != nil {
return err
}
}
}

cloudBuild.DockerImage = valueOrDefault(cloudBuild.DockerImage, constants.DefaultCloudBuildDockerImage)
return nil
}

func setDefaultCloudBuildDockerImage(gcb *latest.GoogleCloudBuild) error {
gcb.DockerImage = valueOrDefault(gcb.DockerImage, constants.DefaultCloudBuildDockerImage)
return nil
}

func setDefaultCloudBuildMavenImage(gcb *latest.GoogleCloudBuild) error {
gcb.MavenImage = valueOrDefault(gcb.MavenImage, constants.DefaultCloudBuildMavenImage)
return nil
}

func setDefaultCloudBuildGradleImage(gcb *latest.GoogleCloudBuild) error {
gcb.GradleImage = valueOrDefault(gcb.GradleImage, constants.DefaultCloudBuildGradleImage)
return nil
}

func setDefaultTagger(c *latest.SkaffoldPipeline) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/skaffold/schema/latest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/util"
)

const Version string = "skaffold/v1beta2"
const Version string = "skaffold/v1beta3"

// NewSkaffoldPipeline creates a SkaffoldPipeline
func NewSkaffoldPipeline() util.VersionedConfig {
Expand Down Expand Up @@ -97,6 +97,8 @@ type GoogleCloudBuild struct {
MachineType string `yaml:"machineType,omitempty"`
Timeout string `yaml:"timeout,omitempty"`
DockerImage string `yaml:"dockerImage,omitempty"`
MavenImage string `yaml:"mavenImage,omitempty"`
GradleImage string `yaml:"gradleImage,omitempty"`
}

// LocalDir represents the local directory kaniko build context
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/schema/v1beta1/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (

"github.com/pkg/errors"

next "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/util"
next "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1beta2"
)

// Upgrade upgrades a configuration to the next version.
Expand Down
4 changes: 2 additions & 2 deletions pkg/skaffold/schema/v1beta1/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package v1beta1
import (
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1beta2"
"github.com/GoogleContainerTools/skaffold/testutil"
yaml "gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -104,7 +104,7 @@ func verityUpgrade(t *testing.T, input, output string) {
upgraded, err := pipeline.Upgrade()
testutil.CheckError(t, false, err)

expected := latest.NewSkaffoldPipeline()
expected := v1beta2.NewSkaffoldPipeline()
err = yaml.UnmarshalStrict([]byte(output), expected)

testutil.CheckErrorAndDeepEqual(t, false, err, expected, upgraded)
Expand Down
Loading

0 comments on commit f302cbe

Please sign in to comment.