-
-
Notifications
You must be signed in to change notification settings - Fork 157
/
push.bzl
200 lines (158 loc) · 6.09 KB
/
push.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"Implementation details for the push rule"
load("//oci/private:util.bzl", "util")
_DOC = """Push an oci_image or oci_image_index to a remote registry.
Internal rule used by the [oci_push macro](/docs/push.md#oci_push).
Most users should use the macro.
Authorization
=============
By default, oci_push uses the standard authorization config file located on the host where `oci_push` is running.
Therefore the following documentation may be consulted:
- https://docs.docker.com/engine/reference/commandline/login/
- https://docs.podman.io/en/latest/markdown/podman-login.1.html
- https://github.com/google/go-containerregistry/blob/main/cmd/crane/doc/crane_auth_login.md
Behavior
========
Pushing and tagging are performed sequentially which MAY lead to non-atomic pushes if one the following events occur;
- Remote registry rejects a tag due to various reasons. eg: forbidden characters, existing tags
- Remote registry closes the connection during the tagging
- Local network outages
In order to avoid incomplete pushes oci_push will push the image by its digest and then apply the `remote_tags` sequentially at
the remote registry.
Any failure during pushing or tagging will be reported with non-zero exit code and cause remaining steps to be skipped.
Usage
=====
When running the pusher, you can pass flags to `bazel run`.
1. Override `repository` by passing the `-r|--repository` flag.
e.g. `bazel run //myimage:push -- --repository index.docker.io/<ORG>/image`
2. Supply tags in addition to `remote_tags` by passing the `-t|--tag` flag.
e.g. `bazel run //myimage:push -- --tag latest`
Examples
========
Push an oci_image to docker registry with 'latest' tag
```starlark
oci_image(name = "image")
oci_push(
name = "push",
image = ":image",
repository = "index.docker.io/<ORG>/image",
remote_tags = ["latest"]
)
```
Push a multi-architecture image to github container registry with a semver tag
```starlark
load("@aspect_bazel_lib//lib:expand_template.bzl", "expand_template_rule")
oci_image(name = "app_linux_arm64")
oci_image(name = "app_linux_amd64")
oci_image(name = "app_windows_amd64")
oci_image_index(
name = "app_image",
images = [
":app_linux_arm64",
":app_linux_amd64",
":app_windows_amd64",
]
)
# Use the value of --embed_label under --stamp, otherwise use a deterministic constant
# value to ensure cache hits for actions that depend on this.
expand_template(
name = "stamped",
out = "_stamped.tags.txt",
template = ["0.0.0"],
stamp_substitutions = {"0.0.0": "{{BUILD_EMBED_LABEL}}"},
)
oci_push(
name = "push",
image = ":app_image",
repository = "ghcr.io/<OWNER>/image",
remote_tags = ":stamped",
)
```
"""
_attrs = {
"image": attr.label(
allow_single_file = True,
doc = "Label to an oci_image or oci_image_index",
mandatory = True,
),
"repository": attr.string(
doc = """\
Repository URL where the image will be signed at, e.g.: `index.docker.io/<user>/image`.
Digests and tags are not allowed.
""",
),
"repository_file": attr.label(
doc = """\
The same as 'repository' but in a file. This allows pushing to different repositories based on stamping.
""",
allow_single_file = True,
),
"remote_tags": attr.label(
doc = """\
a .txt file containing tags, one per line.
These are passed to [`crane tag`](
https://github.com/google/go-containerregistry/blob/main/cmd/crane/doc/crane_tag.md)
""",
allow_single_file = [".txt"],
),
"_push_sh_tpl": attr.label(
default = "push.sh.tpl",
allow_single_file = True,
),
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
}
def _quote_args(args):
return ["\"{}\"".format(arg) for arg in args]
def _impl(ctx):
crane = ctx.toolchains["@rules_oci//oci:crane_toolchain_type"]
jq = ctx.toolchains["@aspect_bazel_lib//lib:jq_toolchain_type"]
if ctx.attr.repository and ctx.attr.repository_file:
fail("must specify exactly one of 'repository_file' or 'repository'")
if not ctx.file.image.is_directory:
fail("image attribute must be a oci_image or oci_image_index")
_, _, _, maybe_digest, maybe_tag = util.parse_image(ctx.attr.repository)
if maybe_digest or maybe_tag:
fail("`repository` attribute should not contain digest or tag. got: {}".format(ctx.attr.repository))
executable = ctx.actions.declare_file("push_%s.sh" % ctx.label.name)
files = [ctx.file.image]
substitutions = {
"{{crane_path}}": crane.crane_info.binary.short_path,
"{{jq_path}}": jq.jqinfo.bin.short_path,
"{{image_dir}}": ctx.file.image.short_path,
"{{fixed_args}}": "",
}
if ctx.attr.repository:
substitutions["{{fixed_args}}"] += " ".join(_quote_args(["--repository", ctx.attr.repository]))
elif ctx.attr.repository_file:
files.append(ctx.file.repository_file)
substitutions["{{repository_file}}"] = ctx.file.repository_file.short_path
else:
fail("must specify exactly one of 'repository_file' or 'repository'")
if ctx.attr.remote_tags:
files.append(ctx.file.remote_tags)
substitutions["{{tags}}"] = ctx.file.remote_tags.short_path
ctx.actions.expand_template(
template = ctx.file._push_sh_tpl,
output = executable,
is_executable = True,
substitutions = substitutions,
)
runfiles = ctx.runfiles(files = files)
runfiles = runfiles.merge(jq.default.default_runfiles)
runfiles = runfiles.merge(crane.default.default_runfiles)
return DefaultInfo(executable = util.maybe_wrap_launcher_for_windows(ctx, executable), runfiles = runfiles)
oci_push_lib = struct(
implementation = _impl,
attrs = _attrs,
toolchains = [
"@rules_oci//oci:crane_toolchain_type",
"@aspect_bazel_lib//lib:jq_toolchain_type",
"@bazel_tools//tools/sh:toolchain_type",
],
)
oci_push = rule(
doc = _DOC,
implementation = oci_push_lib.implementation,
attrs = oci_push_lib.attrs,
toolchains = oci_push_lib.toolchains,
executable = True,
)