github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/gcb/jib.go (about) 1 /* 2 Copyright 2019 The Skaffold Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package gcb 18 19 import ( 20 "context" 21 "errors" 22 "os" 23 "path/filepath" 24 "strings" 25 26 "google.golang.org/api/cloudbuild/v1" 27 28 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/jib" 29 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/platform" 30 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" 31 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" 32 ) 33 34 func (b *Builder) jibBuildSpec(ctx context.Context, artifact *latest.Artifact, tag string, platforms platform.Matcher) (cloudbuild.Build, error) { 35 t, err := jib.DeterminePluginType(ctx, artifact.Workspace, artifact.JibArtifact) 36 if err != nil { 37 return cloudbuild.Build{}, err 38 } 39 40 switch t { 41 case jib.JibMaven: 42 return cloudbuild.Build{ 43 Steps: []*cloudbuild.BuildStep{{ 44 Name: b.MavenImage, 45 Entrypoint: "sh", 46 Args: fixHome("mvn", jib.GenerateMavenBuildArgs("build", tag, artifact.JibArtifact, platforms, b.skipTests, true, artifact.Dependencies, b.artifactStore, b.cfg.GetInsecureRegistries(), false)), 47 }}, 48 }, nil 49 case jib.JibGradle: 50 return cloudbuild.Build{ 51 Steps: []*cloudbuild.BuildStep{{ 52 Name: b.GradleImage, 53 Entrypoint: "sh", 54 Args: fixHome("gradle", jib.GenerateGradleBuildArgs("jib", tag, artifact.JibArtifact, platforms, b.skipTests, true, artifact.Dependencies, b.artifactStore, b.cfg.GetInsecureRegistries(), false)), 55 }}, 56 }, nil 57 default: 58 return cloudbuild.Build{}, errors.New("skaffold can't determine Jib artifact type for Google Cloud Build") 59 } 60 } 61 62 func fixHome(command string, args []string) []string { 63 return []string{"-c", command + " -Duser.home=$$HOME " + strings.Join(args, " ")} 64 } 65 66 func jibAddWorkspaceToDependencies(workspace string, dependencies []string) ([]string, error) { 67 dependencyMap := make(map[string]bool) 68 for _, d := range dependencies { 69 dependencyMap[d] = true 70 } 71 72 err := filepath.Walk(workspace, 73 func(path string, info os.FileInfo, err error) error { 74 if err != nil { 75 return err 76 } 77 if info.IsDir() { 78 if info.Name() == "target" { 79 if util.IsFile(filepath.Join(filepath.Dir(path), "pom.xml")) { 80 return filepath.SkipDir 81 } 82 } else if info.Name() == "build" { 83 if util.IsFile(filepath.Join(filepath.Dir(path), "build.gradle")) { 84 return filepath.SkipDir 85 } 86 } 87 } 88 if _, ok := dependencyMap[path]; !ok { 89 dependencies = append(dependencies, path) 90 } 91 return nil 92 }) 93 return dependencies, err 94 }