Skip to content

Commit

Permalink
chore: change bazel example (#9218)
Browse files Browse the repository at this point in the history
* chore: change bazel example

* chore: update bazel example

* chore: remove redundant dirs

* chore: change example project schema version
  • Loading branch information
ericzzzzzzz authored Dec 12, 2023
1 parent c4af00e commit f2323ea
Show file tree
Hide file tree
Showing 24 changed files with 208 additions and 128 deletions.
1 change: 1 addition & 0 deletions examples/bazel/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
common --enable_bzlmod
1 change: 1 addition & 0 deletions examples/bazel/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.1.0
2 changes: 2 additions & 0 deletions examples/bazel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
bazel-*
9 changes: 0 additions & 9 deletions examples/bazel/BUILD

This file was deleted.

53 changes: 53 additions & 0 deletions examples/bazel/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup")
load("@rules_go//go:def.bzl", "go_binary", "go_library")
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_tarball")
load("@rules_pkg//:pkg.bzl", "pkg_tar")

go_library(
name = "app_lib",
srcs = ["main.go"],
importpath = "example.com/custom_registry/app",
visibility = ["//visibility:private"],
)

go_binary(
name = "app",
embed = [":app_lib"],
visibility = ["//visibility:public"],
)

# Put app go_binary into a tar layer.
pkg_tar(
name = "app_layer",
srcs = [":app"],
# If the binary depends on RUNFILES, uncomment the attribute below.
# include_runfiles = True
)

oci_image(
name = "image",
base = "@distroless_base",
entrypoint = ["/app"],
tars = [":app_layer"],
)

# This is the target that should be released to the target platform
platform_transition_filegroup(
name = "transitioned_image",
srcs = [":image"],
target_platform = select({
"@platforms//cpu:arm64": "@rules_go//go/toolchain:linux_arm64",
"@platforms//cpu:x86_64": "@rules_go//go/toolchain:linux_amd64",
}),
)

# $ bazel build :tarball
# $ docker load --input $(bazel cquery --output=files :tarball)
# $ docker run --rm gcr.io/example:latest

oci_tarball(
name = "skaffold-example.tar",
# Use the image built for the exec platform rather than the target platform
image = ":image",
repo_tags = ["gcr.io/example:latest"],
)
28 changes: 28 additions & 0 deletions examples/bazel/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
bazel_dep(name = "aspect_bazel_lib", version = "1.31.1")
bazel_dep(name = "gazelle", version = "0.31.0")
bazel_dep(name = "platforms", version = "0.0.5")
bazel_dep(name = "rules_oci", version = "1.2.0")
bazel_dep(name = "rules_pkg", version = "0.8.1")
bazel_dep(name = "rules_go", version = "0.39.1")

go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")

go_deps.module(
path = "github.com/google/go-cmp",
sum = "h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=",
version = "v0.5.9",
)

oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")

oci.pull(
name = "distroless_base",
digest = "sha256:ccaef5ee2f1850270d453fdf700a5392534f8d1a8ca2acda391fbb6a06b81c86",
image = "gcr.io/distroless/base",
platforms = [
"linux/amd64",
"linux/arm64",
],
)

use_repo(oci, "distroless_base")
4 changes: 2 additions & 2 deletions examples/bazel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ The way you configure it in `skaffold.yaml` is the following build stanza:
```yaml
build:
artifacts:
- image: skaffold-example
- image: skaffold-bazel
context: .
bazel:
target: //:skaffold_example.tar
```
1. make sure the `context` contains the bazel files (`WORKSPACE`, `BUILD`)
2. add `bazel` section to each artifact
3. specify `target` - our builder will use this to load to the image to the Docker daemon
3. specify `target` - our builder will use this to load to the image to the Docker daemon
47 changes: 0 additions & 47 deletions examples/bazel/WORKSPACE

This file was deleted.

1 change: 1 addition & 0 deletions examples/bazel/WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Marker file: this is the root of a Bazel workspace
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ metadata:
name: bazel
spec:
containers:
- name: bazel
image: skaffold-bazel
- name: bazel
image: skaffold-bazel
3 changes: 2 additions & 1 deletion examples/bazel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (

func main() {
for {
fmt.Println("Hello bazel!!!!")
fmt.Println("Hello world!")

time.Sleep(time.Second * 1)
}
}
15 changes: 12 additions & 3 deletions examples/bazel/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
apiVersion: skaffold/v4beta8
kind: Config
metadata:
name: hello
build:
tagPolicy:
sha256: {}
artifacts:
- image: skaffold-bazel
bazel:
target: //:skaffold_example.tar
- image: skaffold-bazel
bazel:
target: //:skaffold-example.tar
deploy:
kubectl: {}
manifests:
rawYaml:
- "deploy.yaml"
1 change: 1 addition & 0 deletions integration/examples/bazel/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
common --enable_bzlmod
1 change: 1 addition & 0 deletions integration/examples/bazel/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.1.0
2 changes: 2 additions & 0 deletions integration/examples/bazel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
bazel-*
9 changes: 0 additions & 9 deletions integration/examples/bazel/BUILD

This file was deleted.

53 changes: 53 additions & 0 deletions integration/examples/bazel/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup")
load("@rules_go//go:def.bzl", "go_binary", "go_library")
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_tarball")
load("@rules_pkg//:pkg.bzl", "pkg_tar")

go_library(
name = "app_lib",
srcs = ["main.go"],
importpath = "example.com/custom_registry/app",
visibility = ["//visibility:private"],
)

go_binary(
name = "app",
embed = [":app_lib"],
visibility = ["//visibility:public"],
)

# Put app go_binary into a tar layer.
pkg_tar(
name = "app_layer",
srcs = [":app"],
# If the binary depends on RUNFILES, uncomment the attribute below.
# include_runfiles = True
)

oci_image(
name = "image",
base = "@distroless_base",
entrypoint = ["/app"],
tars = [":app_layer"],
)

# This is the target that should be released to the target platform
platform_transition_filegroup(
name = "transitioned_image",
srcs = [":image"],
target_platform = select({
"@platforms//cpu:arm64": "@rules_go//go/toolchain:linux_arm64",
"@platforms//cpu:x86_64": "@rules_go//go/toolchain:linux_amd64",
}),
)

# $ bazel build :tarball
# $ docker load --input $(bazel cquery --output=files :tarball)
# $ docker run --rm gcr.io/example:latest

oci_tarball(
name = "skaffold-example.tar",
# Use the image built for the exec platform rather than the target platform
image = ":image",
repo_tags = ["gcr.io/example:latest"],
)
28 changes: 28 additions & 0 deletions integration/examples/bazel/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
bazel_dep(name = "aspect_bazel_lib", version = "1.31.1")
bazel_dep(name = "gazelle", version = "0.31.0")
bazel_dep(name = "platforms", version = "0.0.5")
bazel_dep(name = "rules_oci", version = "1.2.0")
bazel_dep(name = "rules_pkg", version = "0.8.1")
bazel_dep(name = "rules_go", version = "0.39.1")

go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")

go_deps.module(
path = "github.com/google/go-cmp",
sum = "h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=",
version = "v0.5.9",
)

oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")

oci.pull(
name = "distroless_base",
digest = "sha256:ccaef5ee2f1850270d453fdf700a5392534f8d1a8ca2acda391fbb6a06b81c86",
image = "gcr.io/distroless/base",
platforms = [
"linux/amd64",
"linux/arm64",
],
)

use_repo(oci, "distroless_base")
4 changes: 2 additions & 2 deletions integration/examples/bazel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ The way you configure it in `skaffold.yaml` is the following build stanza:
```yaml
build:
artifacts:
- image: skaffold-example
- image: skaffold-bazel
context: .
bazel:
target: //:skaffold_example.tar
```
1. make sure the `context` contains the bazel files (`WORKSPACE`, `BUILD`)
2. add `bazel` section to each artifact
3. specify `target` - our builder will use this to load to the image to the Docker daemon
3. specify `target` - our builder will use this to load to the image to the Docker daemon
47 changes: 0 additions & 47 deletions integration/examples/bazel/WORKSPACE

This file was deleted.

1 change: 1 addition & 0 deletions integration/examples/bazel/WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Marker file: this is the root of a Bazel workspace
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ metadata:
name: bazel
spec:
containers:
- name: bazel
image: skaffold-bazel
- name: bazel
image: skaffold-bazel
3 changes: 2 additions & 1 deletion integration/examples/bazel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (

func main() {
for {
fmt.Println("Hello bazel!!!!")
fmt.Println("Hello world!")

time.Sleep(time.Second * 1)
}
}
Loading

0 comments on commit f2323ea

Please # to comment.