diff --git a/examples/bazel/.bazelrc b/examples/bazel/.bazelrc new file mode 100644 index 00000000000..3ce91d27219 --- /dev/null +++ b/examples/bazel/.bazelrc @@ -0,0 +1 @@ +common --enable_bzlmod diff --git a/examples/bazel/.bazelversion b/examples/bazel/.bazelversion new file mode 100644 index 00000000000..dfda3e0b4f0 --- /dev/null +++ b/examples/bazel/.bazelversion @@ -0,0 +1 @@ +6.1.0 diff --git a/examples/bazel/.gitignore b/examples/bazel/.gitignore new file mode 100644 index 00000000000..0684e4bfe51 --- /dev/null +++ b/examples/bazel/.gitignore @@ -0,0 +1,2 @@ +node_modules +bazel-* diff --git a/examples/bazel/BUILD b/examples/bazel/BUILD deleted file mode 100644 index ba00afe13d3..00000000000 --- a/examples/bazel/BUILD +++ /dev/null @@ -1,9 +0,0 @@ -load("@io_bazel_rules_docker//go:image.bzl", "go_image") - -go_image( - name = "skaffold_example", - srcs = ["main.go"], - goos = "linux", - goarch = "amd64", - static = "on", -) diff --git a/examples/bazel/BUILD.bazel b/examples/bazel/BUILD.bazel new file mode 100644 index 00000000000..4149f2e2c2d --- /dev/null +++ b/examples/bazel/BUILD.bazel @@ -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"], +) diff --git a/examples/bazel/MODULE.bazel b/examples/bazel/MODULE.bazel new file mode 100644 index 00000000000..135d0b812ff --- /dev/null +++ b/examples/bazel/MODULE.bazel @@ -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") diff --git a/examples/bazel/README.md b/examples/bazel/README.md index 9a3592c8163..224230d966e 100644 --- a/examples/bazel/README.md +++ b/examples/bazel/README.md @@ -9,7 +9,7 @@ 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 @@ -17,4 +17,4 @@ build: 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 \ No newline at end of file diff --git a/examples/bazel/WORKSPACE b/examples/bazel/WORKSPACE deleted file mode 100644 index 3b5a76e1117..00000000000 --- a/examples/bazel/WORKSPACE +++ /dev/null @@ -1,47 +0,0 @@ -workspace(name = "skaffold") - -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - -http_archive( - name = "io_bazel_rules_docker", - sha256 = "b1e80761a8a8243d03ebca8845e9cc1ba6c82ce7c5179ce2b295cd36f7e394bf", - urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.25.0/rules_docker-v0.25.0.tar.gz"], -) - -http_archive( - name = "io_bazel_rules_go", - sha256 = "a8d6b1b354d371a646d2f7927319974e0f9e52f73a2452d2b3877118169eb6bb", - urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.23.3/rules_go-v0.23.3.tar.gz", - "https://github.com/bazelbuild/rules_go/releases/download/v0.23.3/rules_go-v0.23.3.tar.gz", - ], -) - -load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") - -go_rules_dependencies() - -go_register_toolchains( - go_version = "1.14.4", -) - -load( - "@io_bazel_rules_docker//repositories:repositories.bzl", - container_repositories = "repositories", -) - -container_repositories() - -load( - "@io_bazel_rules_docker//repositories:deps.bzl", - container_deps = "deps", -) - -container_deps() - -load( - "@io_bazel_rules_docker//go:image.bzl", - _go_image_repos = "repositories", -) - -_go_image_repos() diff --git a/examples/bazel/WORKSPACE.bazel b/examples/bazel/WORKSPACE.bazel new file mode 100644 index 00000000000..64d7fc061a1 --- /dev/null +++ b/examples/bazel/WORKSPACE.bazel @@ -0,0 +1 @@ +# Marker file: this is the root of a Bazel workspace diff --git a/integration/examples/bazel/k8s/k8s-pod.yaml b/examples/bazel/deploy.yaml similarity index 60% rename from integration/examples/bazel/k8s/k8s-pod.yaml rename to examples/bazel/deploy.yaml index 84a5318a0e1..aa19e600205 100644 --- a/integration/examples/bazel/k8s/k8s-pod.yaml +++ b/examples/bazel/deploy.yaml @@ -4,5 +4,5 @@ metadata: name: bazel spec: containers: - - name: bazel - image: skaffold-bazel + - name: bazel + image: skaffold-bazel \ No newline at end of file diff --git a/examples/bazel/main.go b/examples/bazel/main.go index 8f98567919a..593721cfe2e 100644 --- a/examples/bazel/main.go +++ b/examples/bazel/main.go @@ -7,7 +7,8 @@ import ( func main() { for { - fmt.Println("Hello bazel!!!!") + fmt.Println("Hello world!") + time.Sleep(time.Second * 1) } } diff --git a/examples/bazel/skaffold.yaml b/examples/bazel/skaffold.yaml index 5fba81c18fc..a6e5c9d090f 100644 --- a/examples/bazel/skaffold.yaml +++ b/examples/bazel/skaffold.yaml @@ -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" diff --git a/integration/examples/bazel/.bazelrc b/integration/examples/bazel/.bazelrc new file mode 100644 index 00000000000..3ce91d27219 --- /dev/null +++ b/integration/examples/bazel/.bazelrc @@ -0,0 +1 @@ +common --enable_bzlmod diff --git a/integration/examples/bazel/.bazelversion b/integration/examples/bazel/.bazelversion new file mode 100644 index 00000000000..dfda3e0b4f0 --- /dev/null +++ b/integration/examples/bazel/.bazelversion @@ -0,0 +1 @@ +6.1.0 diff --git a/integration/examples/bazel/.gitignore b/integration/examples/bazel/.gitignore new file mode 100644 index 00000000000..0684e4bfe51 --- /dev/null +++ b/integration/examples/bazel/.gitignore @@ -0,0 +1,2 @@ +node_modules +bazel-* diff --git a/integration/examples/bazel/BUILD b/integration/examples/bazel/BUILD deleted file mode 100644 index ba00afe13d3..00000000000 --- a/integration/examples/bazel/BUILD +++ /dev/null @@ -1,9 +0,0 @@ -load("@io_bazel_rules_docker//go:image.bzl", "go_image") - -go_image( - name = "skaffold_example", - srcs = ["main.go"], - goos = "linux", - goarch = "amd64", - static = "on", -) diff --git a/integration/examples/bazel/BUILD.bazel b/integration/examples/bazel/BUILD.bazel new file mode 100644 index 00000000000..4149f2e2c2d --- /dev/null +++ b/integration/examples/bazel/BUILD.bazel @@ -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"], +) diff --git a/integration/examples/bazel/MODULE.bazel b/integration/examples/bazel/MODULE.bazel new file mode 100644 index 00000000000..135d0b812ff --- /dev/null +++ b/integration/examples/bazel/MODULE.bazel @@ -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") diff --git a/integration/examples/bazel/README.md b/integration/examples/bazel/README.md index 9a3592c8163..224230d966e 100644 --- a/integration/examples/bazel/README.md +++ b/integration/examples/bazel/README.md @@ -9,7 +9,7 @@ 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 @@ -17,4 +17,4 @@ build: 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 \ No newline at end of file diff --git a/integration/examples/bazel/WORKSPACE b/integration/examples/bazel/WORKSPACE deleted file mode 100644 index 3b5a76e1117..00000000000 --- a/integration/examples/bazel/WORKSPACE +++ /dev/null @@ -1,47 +0,0 @@ -workspace(name = "skaffold") - -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - -http_archive( - name = "io_bazel_rules_docker", - sha256 = "b1e80761a8a8243d03ebca8845e9cc1ba6c82ce7c5179ce2b295cd36f7e394bf", - urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.25.0/rules_docker-v0.25.0.tar.gz"], -) - -http_archive( - name = "io_bazel_rules_go", - sha256 = "a8d6b1b354d371a646d2f7927319974e0f9e52f73a2452d2b3877118169eb6bb", - urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.23.3/rules_go-v0.23.3.tar.gz", - "https://github.com/bazelbuild/rules_go/releases/download/v0.23.3/rules_go-v0.23.3.tar.gz", - ], -) - -load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") - -go_rules_dependencies() - -go_register_toolchains( - go_version = "1.14.4", -) - -load( - "@io_bazel_rules_docker//repositories:repositories.bzl", - container_repositories = "repositories", -) - -container_repositories() - -load( - "@io_bazel_rules_docker//repositories:deps.bzl", - container_deps = "deps", -) - -container_deps() - -load( - "@io_bazel_rules_docker//go:image.bzl", - _go_image_repos = "repositories", -) - -_go_image_repos() diff --git a/integration/examples/bazel/WORKSPACE.bazel b/integration/examples/bazel/WORKSPACE.bazel new file mode 100644 index 00000000000..64d7fc061a1 --- /dev/null +++ b/integration/examples/bazel/WORKSPACE.bazel @@ -0,0 +1 @@ +# Marker file: this is the root of a Bazel workspace diff --git a/examples/bazel/k8s/k8s-pod.yaml b/integration/examples/bazel/deploy.yaml similarity index 60% rename from examples/bazel/k8s/k8s-pod.yaml rename to integration/examples/bazel/deploy.yaml index 84a5318a0e1..aa19e600205 100644 --- a/examples/bazel/k8s/k8s-pod.yaml +++ b/integration/examples/bazel/deploy.yaml @@ -4,5 +4,5 @@ metadata: name: bazel spec: containers: - - name: bazel - image: skaffold-bazel + - name: bazel + image: skaffold-bazel \ No newline at end of file diff --git a/integration/examples/bazel/main.go b/integration/examples/bazel/main.go index 8f98567919a..593721cfe2e 100644 --- a/integration/examples/bazel/main.go +++ b/integration/examples/bazel/main.go @@ -7,7 +7,8 @@ import ( func main() { for { - fmt.Println("Hello bazel!!!!") + fmt.Println("Hello world!") + time.Sleep(time.Second * 1) } } diff --git a/integration/examples/bazel/skaffold.yaml b/integration/examples/bazel/skaffold.yaml index 727bca1428d..90a0e26b993 100644 --- a/integration/examples/bazel/skaffold.yaml +++ b/integration/examples/bazel/skaffold.yaml @@ -1,7 +1,16 @@ apiVersion: skaffold/v4beta9 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"