Skip to content

Commit

Permalink
feat: Add global build pre- and post-hooks (#9047)
Browse files Browse the repository at this point in the history
* feat(pkg/skaffold/schema): add build.hooks to latest config version

* feat(pkgs/skaffold/runner): wrap pipeline builders to run build pre/post hooks

* fix(pkg/skaffold/runner): use pointer semantics for pipelineBuilderWithHooks to make it hash-able

* test(pkg/skaffold/runner): add builder test for pipelineBuilderWithHooks

* doc(integration/examples): add the new build.hooks example in 'lifecycle-hooks'

* fix(pkg/skaffold/runner): add copyright to builder_test.go
  • Loading branch information
ar3s3ru committed Sep 8, 2023
1 parent 09f4049 commit 38b4e03
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 14 deletions.
24 changes: 24 additions & 0 deletions docs-v2/content/en/schemas/v4beta7.json
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,11 @@
"description": "the images you're going to be building.",
"x-intellij-html-description": "the images you're going to be building."
},
"hooks": {
"$ref": "#/definitions/BuildHooks",
"description": "describes a set of lifecycle hooks that are executed before and after the build phase of the Pipeline, where the artifacts are built.",
"x-intellij-html-description": "describes a set of lifecycle hooks that are executed before and after the build phase of the Pipeline, where the artifacts are built."
},
"insecureRegistries": {
"items": {
"type": "string"
Expand All @@ -750,6 +755,7 @@
}
},
"preferredOrder": [
"hooks",
"artifacts",
"insecureRegistries",
"tagPolicy",
Expand All @@ -767,6 +773,11 @@
"description": "the images you're going to be building.",
"x-intellij-html-description": "the images you're going to be building."
},
"hooks": {
"$ref": "#/definitions/BuildHooks",
"description": "describes a set of lifecycle hooks that are executed before and after the build phase of the Pipeline, where the artifacts are built.",
"x-intellij-html-description": "describes a set of lifecycle hooks that are executed before and after the build phase of the Pipeline, where the artifacts are built."
},
"insecureRegistries": {
"items": {
"type": "string"
Expand Down Expand Up @@ -797,6 +808,7 @@
}
},
"preferredOrder": [
"hooks",
"artifacts",
"insecureRegistries",
"tagPolicy",
Expand All @@ -820,6 +832,11 @@
"description": "*beta* describes how to do a remote build on [Google Cloud Build](https://cloud.google.com/cloud-build/).",
"x-intellij-html-description": "<em>beta</em> describes how to do a remote build on <a href=\"https://cloud.google.com/cloud-build/\">Google Cloud Build</a>."
},
"hooks": {
"$ref": "#/definitions/BuildHooks",
"description": "describes a set of lifecycle hooks that are executed before and after the build phase of the Pipeline, where the artifacts are built.",
"x-intellij-html-description": "describes a set of lifecycle hooks that are executed before and after the build phase of the Pipeline, where the artifacts are built."
},
"insecureRegistries": {
"items": {
"type": "string"
Expand All @@ -845,6 +862,7 @@
}
},
"preferredOrder": [
"hooks",
"artifacts",
"insecureRegistries",
"tagPolicy",
Expand All @@ -868,6 +886,11 @@
"description": "*beta* describes how to do an on-cluster build.",
"x-intellij-html-description": "<em>beta</em> describes how to do an on-cluster build."
},
"hooks": {
"$ref": "#/definitions/BuildHooks",
"description": "describes a set of lifecycle hooks that are executed before and after the build phase of the Pipeline, where the artifacts are built.",
"x-intellij-html-description": "describes a set of lifecycle hooks that are executed before and after the build phase of the Pipeline, where the artifacts are built."
},
"insecureRegistries": {
"items": {
"type": "string"
Expand All @@ -893,6 +916,7 @@
}
},
"preferredOrder": [
"hooks",
"artifacts",
"insecureRegistries",
"tagPolicy",
Expand Down
9 changes: 7 additions & 2 deletions integration/examples/lifecycle-hooks/skaffold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ manifests:
- host:
command: ["sh", "-c", "echo post-render host hook running on $(hostname)!"]
build:
hooks:
before:
- command: ["sh", "-c", "echo pre-build hook running"]
after:
- command: ["sh", "-c", "echo post-build hook running"]
artifacts:
- image: hooks-example
hooks:
Expand All @@ -26,7 +31,7 @@ build:
os: [darwin, linux]
- command: ["cmd.exe", "/C", "docker images %SKAFFOLD_IMAGE% --digests"]
os: [windows]
sync:
sync:
manual:
- src: 'hello.txt'
dest: .
Expand All @@ -49,7 +54,7 @@ deploy:
command: ["sh", "-c", "echo pre-deploy host hook running on $(hostname)!"]
os: [darwin, linux]
- container:
# this will only run when there's a matching container from a previous deploy iteration like in `skaffold dev`
# this will only run when there's a matching container from a previous deploy iteration like in `skaffold dev`
command: ["sh", "-c", "echo pre-deploy container hook running on $(hostname)!"]
containerName: hooks-example*
podName: hooks-example-deployment*
Expand Down
42 changes: 42 additions & 0 deletions pkg/skaffold/runner/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ package runner
import (
"context"
"fmt"
"io"

"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/build/cluster"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/build/gcb"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/build/local"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/graph"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/hooks"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/output/log"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/runner/runcontext"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/schema/latest"
Expand Down Expand Up @@ -74,3 +76,43 @@ func GetBuilder(ctx context.Context, r *runcontext.RunContext, s build.ArtifactS
return nil, fmt.Errorf("unknown builder for config %+v", p.Build)
}
}

type pipelineBuilderWithHooks struct {
build.PipelineBuilder
hooksRunner hooks.Runner
}

// PreBuild executes any one-time setup required prior to starting any build on this builder,
// followed by any Build pre-hooks set for the pipeline.
func (b *pipelineBuilderWithHooks) PreBuild(ctx context.Context, out io.Writer) error {
if err := b.PipelineBuilder.PreBuild(ctx, out); err != nil {
return err
}

if err := b.hooksRunner.RunPreHooks(ctx, out); err != nil {
return fmt.Errorf("running builder pre-hooks: %w", err)
}

return nil
}

// PostBuild executes any one-time teardown required after all builds on this builder are complete,
// followed by any Build post-hooks set for the pipeline.
func (b *pipelineBuilderWithHooks) PostBuild(ctx context.Context, out io.Writer) error {
if err := b.PipelineBuilder.PostBuild(ctx, out); err != nil {
return err
}

if err := b.hooksRunner.RunPostHooks(ctx, out); err != nil {
return fmt.Errorf("running builder post-hooks: %w", err)
}

return nil
}

func withPipelineBuildHooks(pb build.PipelineBuilder, buildHooks latest.BuildHooks) build.PipelineBuilder {
return &pipelineBuilderWithHooks{
PipelineBuilder: pb,
hooksRunner: hooks.BuildRunner(buildHooks, hooks.BuildEnvOpts{}),
}
}
193 changes: 193 additions & 0 deletions pkg/skaffold/runner/builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/*
Copyright 2023 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 runner

import (
"bytes"
"context"
"io"
"testing"

"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/platform"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/v2/testutil"
)

func TestPipelineBuilderWithHooks(t *testing.T) {
type testcase struct {
name string
hooks latest.BuildHooks
expected []byte
wantPreHooksErr bool
wantPostHooksErr bool
}

testcases := []testcase{
{
name: "no hooks to execute",
hooks: latest.BuildHooks{},
expected: nil,
wantPreHooksErr: false,
wantPostHooksErr: false,
},
{
name: "execute pre-hooks in order",
hooks: latest.BuildHooks{
PreHooks: []latest.HostHook{
{
Command: []string{
"sh", "-c", "echo hello world 1",
},
},
{
Command: []string{
"sh", "-c", "echo hello world 2",
},
},
},
PostHooks: nil,
},
expected: []byte(
"Starting pre-build hooks...\n" +
"hello world 1\n" +
"hello world 2\n" +
"Completed pre-build hooks\n",
),
},
{
name: "execute post-hooks in order",
hooks: latest.BuildHooks{
PreHooks: nil,
PostHooks: []latest.HostHook{
{
Command: []string{
"sh", "-c", "echo hello world 1",
},
},
{
Command: []string{
"sh", "-c", "echo hello world 2",
},
},
},
},
expected: []byte(
"Starting post-build hooks...\n" +
"hello world 1\n" +
"hello world 2\n" +
"Completed post-build hooks\n",
),
},
{
name: "execute pre-hooks before post-hooks in order",
hooks: latest.BuildHooks{
PreHooks: []latest.HostHook{
{
Command: []string{
"sh", "-c", "echo hello world 1",
},
},
},
PostHooks: []latest.HostHook{
{
Command: []string{
"sh", "-c", "echo hello world 2",
},
},
},
},
expected: []byte(
"Starting pre-build hooks...\n" +
"hello world 1\n" +
"Completed pre-build hooks\n" +
"Starting post-build hooks...\n" +
"hello world 2\n" +
"Completed post-build hooks\n",
),
},
{
name: "executing pre-hooks returns an error if one of the commands fail",
hooks: latest.BuildHooks{
PreHooks: []latest.HostHook{
{
Command: []string{
"sh", "-c", "exit 1",
},
},
},
PostHooks: nil,
},
wantPreHooksErr: true,
},
{
name: "executing post-hooks returns an error if one of the commands fail",
hooks: latest.BuildHooks{
PreHooks: nil,
PostHooks: []latest.HostHook{
{
Command: []string{
"sh", "-c", "exit 1",
},
},
},
},
wantPostHooksErr: true,
},
}

pipelineBuilder := &mockPipelineBuilder{}

for _, tc := range testcases {
tc := tc

testutil.Run(t, tc.name, func(t *testutil.T) {
ctx := context.Background()
buf := new(bytes.Buffer)

pb := withPipelineBuildHooks(pipelineBuilder, tc.hooks)

err := pb.PreBuild(ctx, buf)
t.CheckError(tc.wantPreHooksErr, err)

err = pb.PostBuild(ctx, buf)
t.CheckError(tc.wantPostHooksErr, err)

if !tc.wantPreHooksErr && !tc.wantPostHooksErr {
t.CheckDeepEqual(tc.expected, buf.Bytes())
}
})
}
}

type mockPipelineBuilder struct{}

func (m *mockPipelineBuilder) PreBuild(ctx context.Context, out io.Writer) error { return nil }

func (m *mockPipelineBuilder) Build(ctx context.Context, out io.Writer, artifact *latest.Artifact) build.ArtifactBuilder {
return nil
}

func (m *mockPipelineBuilder) PostBuild(ctx context.Context, out io.Writer) error { return nil }

func (m *mockPipelineBuilder) Concurrency() *int { return nil }

func (m *mockPipelineBuilder) Prune(context.Context, io.Writer) error { return nil }

func (m *mockPipelineBuilder) PushImages() bool { return false }

func (m *mockPipelineBuilder) SupportedPlatforms() platform.Matcher { return platform.All }
7 changes: 5 additions & 2 deletions pkg/skaffold/runner/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func NewForConfig(ctx context.Context, runCtx *runcontext.RunContext) (*Skaffold
var deployer deploy.Deployer

hydrationDir, err := util.GetHydrationDir(runCtx.Opts, runCtx.WorkingDir, true, isKptRendererOrDeployerUsed(runCtx.Pipelines))

if err != nil {
return nil, fmt.Errorf("getting render output path: %w", err)
}
Expand Down Expand Up @@ -145,7 +144,11 @@ func NewForConfig(ctx context.Context, runCtx *runcontext.RunContext) (*Skaffold
// the Cluster object on the RunContext, which in turn influences whether or not we will push images.
var builder build.Builder
builder, err = build.NewBuilderMux(runCtx, store, artifactCache, func(p latest.Pipeline) (build.PipelineBuilder, error) {
return GetBuilder(ctx, runCtx, store, sourceDependencies, p)
pb, err := GetBuilder(ctx, runCtx, store, sourceDependencies, p)
if err != nil {
return nil, err
}
return withPipelineBuildHooks(pb, p.Build.Hooks), nil
})
if err != nil {
endTrace(instrumentation.TraceEndError(err))
Expand Down

0 comments on commit 38b4e03

Please sign in to comment.