Skip to content

Commit

Permalink
Distribute rules via http_archive
Browse files Browse the repository at this point in the history
  • Loading branch information
bcmyers committed Jan 7, 2025
1 parent 8c5b324 commit 1069888
Show file tree
Hide file tree
Showing 42 changed files with 1,990 additions and 354 deletions.
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
examples
node_modules
8 changes: 6 additions & 2 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ on:
branches: [main]
pull_request:
workflow_dispatch:
env:
CI: 1
jobs:
ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: bazelbuild/setup-bazelisk@v1
- run: bazel test --config=ci //...
- run: bazel run //:gazelle -- -mode diff || exit 1
- uses: extractions/setup-just@6e1de3cc407de738551abd6c0923bd5ed5608042
with:
just-version: "1.38.0"
- run: just test
14 changes: 13 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@gazelle//:def.bzl", "DEFAULT_LANGUAGES", "gazelle", "gazelle_binary")
load("@bazel_gazelle//:def.bzl", "DEFAULT_LANGUAGES", "gazelle", "gazelle_binary")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@rules_pkg//pkg:pkg.bzl", "pkg_tar")

Expand All @@ -20,6 +20,17 @@ gazelle(
gazelle = ":gazelle-binary",
)

gazelle(
name = "gazelle-update-repos",
args = [
"-build_file_proto_mode=disable",
"-from_file=go.mod",
"-prune=true",
"-to_macro=oci/private/repositories/go_repositories.bzl%go_repositories",
],
command = "update-repos",
)

gazelle_binary(
name = "gazelle-binary",
languages = DEFAULT_LANGUAGES + [
Expand All @@ -38,6 +49,7 @@ pkg_tar(
"//oci:files",
"//oci/private:files",
"//oci/private/repositories:files",
"//oci/repositories:files",
],
empty_files = ["BUILD.bazel"],
extension = "tar.gz",
Expand Down
15 changes: 11 additions & 4 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ bazel_dep(name = "aspect_bazel_lib", version = "2.7.3")
bazel_dep(name = "aspect_rules_js", version = "2.1.2")
bazel_dep(name = "aspect_rules_lint", version = "1.0.8")
bazel_dep(name = "bazel_skylib", version = "1.6.1")
bazel_dep(name = "gazelle", version = "0.38.0")
bazel_dep(name = "gazelle", version = "0.38.0", repo_name = "bazel_gazelle")
bazel_dep(name = "rules_go", version = "0.47.1", repo_name = "io_bazel_rules_go")
bazel_dep(name = "rules_nodejs", version = "6.3.2")
bazel_dep(name = "rules_pkg", version = "0.10.1")
Expand All @@ -16,14 +16,22 @@ bazel_dep(name = "stardoc", version = "0.6.2")
bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "1.6.1", dev_dependency = True)
bazel_dep(name = "buildifier_prebuilt", version = "7.3.1", dev_dependency = True)

single_version_override(
module_name = "bazel_skylib_gazelle_plugin",
patch_strip = 2,
patches = [
"//third_party/com_github_bazelbuild_bazel-skylib:01-pr-535-support-resolve.patch",
],
)

go_sdk = use_extension("@io_bazel_rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(
name = "go_sdk",
version = "1.22.5",
)
use_repo(go_sdk, "go_sdk")

go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
use_repo(
go_deps,
Expand All @@ -36,7 +44,6 @@ use_repo(
"com_github_sirupsen_logrus",
"com_github_stretchr_testify",
"com_github_urfave_cli_v2",
"land_oras_oras_go",
)
go_deps.module_override(
patch_strip = 1,
Expand All @@ -46,7 +53,7 @@ go_deps.module_override(
path = "github.com/containerd/containerd",
)

oci_pull = use_repo_rule("//oci:repositories.bzl", "oci_pull")
oci_pull = use_repo_rule("//oci:defs.bzl", "oci_pull")

oci_pull(
name = "ubuntu_noble",
Expand Down
2 changes: 1 addition & 1 deletion MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ rust_register_toolchains(
load("@rules_rust//crate_universe:defs.bzl", "crates_repository")

crates_repository(
name = "crate_index",
name = "com_github_datadog_rules_oci_crate_index",
cargo_lockfile = "//:Cargo.lock",
lockfile = "//:cargo-bazel-lock.json",
manifests = [
Expand All @@ -39,6 +39,6 @@ crates_repository(
rust_version = _RUSTC_VERSION,
)

load("@crate_index//:defs.bzl", "crate_repositories")
load("@com_github_datadog_rules_oci_crate_index//:defs.bzl", "crate_repositories")

crate_repositories()
2 changes: 1 addition & 1 deletion cargo-bazel-lock.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "030e153464f00aa0a15a3162aa3e206b5e42faf52412bf783ec58f62406c22bc",
"checksum": "bcebbb48f8aa8d2b6ef3a9c5b2cb5c8698f2f2eaa50fdd72a9e863b4b3314d6e",
"crates": {
"aho-corasick 1.1.3": {
"name": "aho-corasick",
Expand Down
4 changes: 2 additions & 2 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ stardoc_with_diff_test(
)

stardoc_with_diff_test(
name = "repositories",
bzl_library_target = "//oci:repositories",
name = "pull",
bzl_library_target = "//oci/repositories:pull",
)

update_docs()
32 changes: 0 additions & 32 deletions docs/defs.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,35 +106,3 @@ oci_image_layer
| <a id="oci_image_layer-kwargs"></a>kwargs | Additional arguments to pass to the rule, e.g. `tags` or `visibility` | none |


<a id="oci_pull"></a>

## oci_pull

<pre>
oci_pull(<a href="#oci_pull-name">name</a>, <a href="#oci_pull-debug">debug</a>, <a href="#oci_pull-digest">digest</a>, <a href="#oci_pull-registry">registry</a>, <a href="#oci_pull-repo_mapping">repo_mapping</a>, <a href="#oci_pull-repository">repository</a>, <a href="#oci_pull-scheme">scheme</a>, <a href="#oci_pull-shallow">shallow</a>)
</pre>

**ATTRIBUTES**


| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="oci_pull-name"></a>name | A unique name for this repository. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="oci_pull-debug"></a>debug | Deprecated. Does nothing | Boolean | optional | `False` |
| <a id="oci_pull-digest"></a>digest | The digest or tag of the manifest file | String | required | |
| <a id="oci_pull-registry"></a>registry | Remote registry host to pull from, e.g. `gcr.io` or `index.docker.io` | String | required | |
| <a id="oci_pull-repo_mapping"></a>repo_mapping | In `WORKSPACE` context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<br><br>For example, an entry `"@foo": "@bar"` declares that, for any time this repository depends on `@foo` (such as a dependency on `@foo//some:target`, it should actually resolve that dependency within globally-declared `@bar` (`@bar//some:target`).<br><br>This attribute is _not_ supported in `MODULE.bazel` context (when invoking a repository rule inside a module extension's implementation function). | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | |
| <a id="oci_pull-repository"></a>repository | Image path beneath the registry, e.g. `distroless/static` | String | required | |
| <a id="oci_pull-scheme"></a>scheme | scheme portion of the URL for fetching from the registry | String | optional | `"https"` |
| <a id="oci_pull-shallow"></a>shallow | Deprecated. Does nothing | Boolean | optional | `False` |

**ENVIRONMENT VARIABLES**

This repository rule depends on the following environment variables:
* `DOCKER_CONFIG`
* `REGISTRY_AUTH_FILE`
* `XDG_RUNTIME_DIR`
* `HOME`
* `OCI_ENABLE_OAUTH2_SUPPORT`


2 changes: 1 addition & 1 deletion docs/repositories.md → docs/pull.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!-- Generated with Stardoc: http://skydoc.bazel.build -->

public repository rules
oci_pull

<a id="oci_pull"></a>

Expand Down
11 changes: 11 additions & 0 deletions examples/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
common --noenable_bzlmod
common --enable_workspace

build:ci --verbose_failures --show_timestamps --announce_rc
build:ci --noshow_progress --noshow_loading_progress --color=yes
build:ci --define image.tag=ci

test:ci --keep_going
# Only show failing test targets to avoid scrolling past a long list of
# successful tests in order to see error logs.
test:ci --test_summary=terse
1 change: 1 addition & 0 deletions examples/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.2.1
4 changes: 4 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bazel-bin
bazel-out
bazel-rules_oci
bazel-testlogs
15 changes: 15 additions & 0 deletions examples/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@bazel_gazelle//:def.bzl", "DEFAULT_LANGUAGES", "gazelle", "gazelle_binary")

# gazelle:prefix github.com/DataDog/rules_oci/examples
# gazelle:go_naming_convention go_default_library
# gazelle:lang go

gazelle(
name = "gazelle",
gazelle = ":gazelle-binary",
)

gazelle_binary(
name = "gazelle-binary",
languages = DEFAULT_LANGUAGES,
)
64 changes: 64 additions & 0 deletions examples/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
workspace(name = "examples")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

################################################################################
# rules_go
################################################################################

http_archive(
name = "io_bazel_rules_go",
sha256 = "f74c98d6df55217a36859c74b460e774abc0410a47cc100d822be34d5f990f16",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.47.1/rules_go-v0.47.1.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.47.1/rules_go-v0.47.1.zip",
],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

go_rules_dependencies()

go_register_toolchains(version = "1.22.5")

################################################################################
# rules_oci
################################################################################

# Note: In this example, we use local_repository here, but in a real project,
# you'll want to use http_archive to fetch the rules_oci repository
local_repository(
name = "com_github_datadog_rules_oci",
path = "..",
)

load("@com_github_datadog_rules_oci//oci/repositories:01_direct_dependencies.bzl", "rules_oci_direct_dependencies")

rules_oci_direct_dependencies()

load("@com_github_datadog_rules_oci//oci/repositories:02_toolchains.bzl", "rules_oci_toolchains")

rules_oci_toolchains(
# Only set this if you have not already registered a go toolchain
register_go_toolchain_version = None,

# Only set this if you have not already registered a rust toolchain
register_rust_toolchain_version = "1.82.0",
)

load(
"@com_github_datadog_rules_oci//oci/repositories:03_third_party_go_and_rust_libraries.bzl",
"rules_oci_third_party_go_and_rust_libraries",
)

rules_oci_third_party_go_and_rust_libraries()

load("@com_github_datadog_rules_oci//oci/repositories:pull.bzl", "oci_pull")

oci_pull(
name = "ubuntu",
# "noble" tag as of 2024-12-30
digest = "sha256:80dd3c3b9c6cecb9f1667e9290b3bc61b78c2678c02cbdae5f0fea92cc6734ab",
registry = "mirror.gcr.io",
repository = "library/ubuntu",
)
1 change: 1 addition & 0 deletions examples/bazel-examples
24 changes: 0 additions & 24 deletions examples/go-multiarch-image/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load(
"@com_github_datadog_rules_oci//oci:defs.bzl",
"oci_push",
)
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load(":go.bzl", "go_multiarch_image")

go_library(
name = "go_default_library",
Expand All @@ -18,21 +12,3 @@ go_binary(
embed = [":go_default_library"],
visibility = ["//visibility:public"],
)

go_multiarch_image(
name = "image",
archs = [
"amd64",
"arm64",
],
base = "@ubuntu_noble//image",
embed = [":go_default_library"],
visibility = ["//visibility:public"],
)

oci_push(
name = "push",
manifest = ":image",
registry = "ghcr.io",
repository = "datadog/rules_oci/hello-world",
)
11 changes: 8 additions & 3 deletions examples/simple/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("//oci:defs.bzl", "oci_image", "oci_image_index", "oci_image_layer")
load(
"@com_github_datadog_rules_oci//oci:defs.bzl",
"oci_image",
"oci_image_index",
"oci_image_layer",
)

write_file(
name = "hello.txt.write_file",
Expand All @@ -21,7 +26,7 @@ oci_image_layer(
"foo": "bar",
},
arch = arch,
base = "@ubuntu_noble//image",
base = "@ubuntu//image",
layers = [":layer-hello.txt"],
os = "linux",
)
Expand All @@ -32,7 +37,7 @@ oci_image_layer(
]

oci_image_index(
name = "image!",
name = "image",
annotations = {
"baz": "qux",
},
Expand Down
Loading

0 comments on commit 1069888

Please sign in to comment.