Skip to content

Commit bb503dd

Browse files
committed
docs: add auto-generated sections to buildctl.md
Signed-off-by: Justin Chadwell <[email protected]>
1 parent 1c6a500 commit bb503dd

File tree

6 files changed

+194
-11
lines changed

6 files changed

+194
-11
lines changed

Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,12 @@ validate-generated-files:
6262
validate-doctoc:
6363
$(BUILDX_CMD) bake validate-doctoc
6464

65+
.PHONY: validate-docs
66+
validate-docs:
67+
$(BUILDX_CMD) bake validate-docs
68+
6569
.PHONY: validate-all
66-
validate-all: test lint validate-vendor validate-generated-files validate-doctoc
70+
validate-all: test lint validate-vendor validate-generated-files validate-doctoc validate-docs
6771

6872
.PHONY: vendor
6973
vendor:
@@ -84,3 +88,7 @@ authors:
8488
.PHONY: doctoc
8589
doctoc:
8690
$(BUILDX_CMD) bake doctoc
91+
92+
.PHONY: docs
93+
docs:
94+
$(BUILDX_CMD) bake docs

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ $ brew install buildkit
128128

129129
To build BuildKit from source, see [`.github/CONTRIBUTING.md`](./.github/CONTRIBUTING.md).
130130

131-
For a `buildctl` reference, see [this document](./docs/buildctl.md).
131+
For a `buildctl` reference, see [this document](./docs/reference/buildctl.md).
132132

133133
### Starting the `buildkitd` daemon
134134

docker-bake.hcl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ target "integration-tests-base" {
7575
}
7676

7777
group "validate" {
78-
targets = ["lint", "validate-vendor", "validate-doctoc", "validate-generated-files", "validate-shfmt"]
78+
targets = ["lint", "validate-vendor", "validate-doctoc", "validate-generated-files", "validate-shfmt", "validate-docs"]
7979
}
8080

8181
target "lint" {
@@ -119,6 +119,13 @@ target "validate-authors" {
119119
output = ["type=cacheonly"]
120120
}
121121

122+
target "validate-docs" {
123+
inherits = ["_common"]
124+
dockerfile = "./hack/dockerfiles/docs.Dockerfile"
125+
target = "validate"
126+
output = ["type=cacheonly"]
127+
}
128+
122129
target "vendor" {
123130
inherits = ["_common"]
124131
dockerfile = "./hack/dockerfiles/vendor.Dockerfile"
@@ -153,3 +160,10 @@ target "authors" {
153160
target = "update"
154161
output = ["."]
155162
}
163+
164+
target "docs" {
165+
inherits = ["_common"]
166+
dockerfile = "./hack/dockerfiles/docs.Dockerfile"
167+
target = "update"
168+
output = ["./docs"]
169+
}

docs/generate.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io/fs"
7+
"os"
8+
"os/exec"
9+
"path/filepath"
10+
"regexp"
11+
12+
"github.com/pkg/errors"
13+
)
14+
15+
func main() {
16+
re := regexp.MustCompile("(?s)<!---GENERATE_START (.*?)-->(.*?)<!---GENERATE_END-->\n")
17+
18+
err := filepath.Walk("./docs", func(path string, stat fs.FileInfo, err error) error {
19+
if err != nil {
20+
return err
21+
}
22+
if stat.IsDir() {
23+
return nil
24+
}
25+
if filepath.Ext(path) != ".md" {
26+
return nil
27+
}
28+
29+
data, err := os.ReadFile(path)
30+
if err != nil {
31+
return err
32+
}
33+
34+
dataNew := re.ReplaceAllFunc(data, func(match []byte) []byte {
35+
groups := re.FindStringSubmatch(string(match))
36+
stdout := bytes.NewBuffer(nil)
37+
fmt.Fprintf(stdout, "<!---GENERATE_START %s-->\n", groups[1])
38+
fmt.Fprintf(stdout, "```\n")
39+
cmd := exec.Cmd{
40+
Path: "/bin/sh",
41+
Args: []string{"sh", "-c", groups[1]},
42+
Stdout: stdout,
43+
}
44+
err = cmd.Start()
45+
if err != nil {
46+
err = errors.Wrapf(err, "could not start command %s", groups[1])
47+
return nil
48+
}
49+
err = cmd.Wait()
50+
if err != nil {
51+
err = errors.Wrapf(err, "could not run command %s", groups[1])
52+
return nil
53+
}
54+
fmt.Fprintf(stdout, "```\n")
55+
fmt.Fprintf(stdout, "<!---GENERATE_END-->\n")
56+
57+
return stdout.Bytes()
58+
})
59+
if err != nil {
60+
return err
61+
}
62+
63+
if !bytes.Equal(data, dataNew) {
64+
fmt.Println(path)
65+
if err := os.WriteFile(path, dataNew, stat.Mode()); err != nil {
66+
return err
67+
}
68+
}
69+
70+
return nil
71+
})
72+
if err != nil {
73+
fmt.Println(err)
74+
os.Exit(1)
75+
}
76+
}

docs/buildctl.md renamed to docs/reference/buildctl.md

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
`buildctl` is the command-line interface to `buildkitd`.
44

5+
<!---GENERATE_START buildctl --help-->
56
```
67
NAME:
78
buildctl - build utility
@@ -10,14 +11,15 @@ USAGE:
1011
buildctl [global options] command [command options] [arguments...]
1112
1213
VERSION:
13-
0.0.0+unknown
14+
v0.0.0+unknown
1415
1516
COMMANDS:
16-
du disk usage
17-
prune clean up build cache
18-
build, b build
19-
debug debug utilities
20-
help, h Shows a list of commands or help for one command
17+
du disk usage
18+
prune clean up build cache
19+
prune-histories clean up build histories
20+
build, b build
21+
debug debug utilities
22+
help, h Shows a list of commands or help for one command
2123
2224
GLOBAL OPTIONS:
2325
--debug enable debug output in logs
@@ -31,6 +33,7 @@ GLOBAL OPTIONS:
3133
--help, -h show help
3234
--version, -v print the version
3335
```
36+
<!---GENERATE_END-->
3437

3538
## Connecting
3639

@@ -45,9 +48,37 @@ Practically, that normally will be one of:
4548

4649
Synopsis:
4750

51+
<!---GENERATE_START buildctl build --help-->
4852
```
49-
buildctl build --frontend dockerfile.v0 --opt target=foo --opt build-arg:foo=bar --local context=. --local dockerfile=. --output type=image,name=docker.io/username/image,push=true
50-
```
53+
NAME:
54+
buildctl build - build
55+
56+
USAGE:
57+
58+
To build and push an image using Dockerfile:
59+
$ buildctl build --frontend dockerfile.v0 --opt target=foo --opt build-arg:foo=bar --local context=. --local dockerfile=. --output type=image,name=docker.io/username/image,push=true
60+
61+
62+
OPTIONS:
63+
--output value, -o value Define exports for build result, e.g. --output type=image,name=docker.io/username/image,push=true
64+
--progress value Set type of progress (auto, plain, tty). Use plain to show container output (default: "auto")
65+
--trace value Path to trace file. Defaults to no tracing.
66+
--local value Allow build access to the local directory
67+
--oci-layout value Allow build access to the local OCI layout
68+
--frontend value Define frontend used for build
69+
--opt value Define custom options for frontend, e.g. --opt target=foo --opt build-arg:foo=bar
70+
--no-cache Disable cache for all the vertices
71+
--export-cache value Export build cache, e.g. --export-cache type=registry,ref=example.com/foo/bar, or --export-cache type=local,dest=path/to/dir
72+
--import-cache value Import build cache, e.g. --import-cache type=registry,ref=example.com/foo/bar, or --import-cache type=local,src=path/to/dir
73+
--secret value Secret value exposed to the build. Format id=secretname,src=filepath
74+
--allow value Allow extra privileged entitlement, e.g. network.host, security.insecure
75+
--ssh value Allow forwarding SSH agent to the builder. Format default|<id>[=<socket>|<key>[,<key>]]
76+
--metadata-file value Output build metadata (e.g., image digest) to a file as JSON
77+
--source-policy-file value Read source policy file from a JSON file
78+
--ref-file value Write build ref to a file
79+
80+
```
81+
<!---GENERATE_END-->
5182

5283
`buildctl build` uses a buildkit daemon `buildkitd` to drive a build.
5384

hack/dockerfiles/docs.Dockerfile

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# syntax=docker/dockerfile:1
2+
3+
ARG GO_VERSION=1.20
4+
5+
FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS golatest
6+
7+
FROM golatest AS buildctl
8+
WORKDIR /src
9+
ENV CGO_ENABLED=0
10+
ARG TARGETPLATFORM
11+
RUN --mount=target=. \
12+
--mount=target=/root/.cache,type=cache \
13+
--mount=target=/go/pkg/mod,type=cache \
14+
go build -mod=vendor -o /usr/bin/buildctl ./cmd/buildctl
15+
16+
FROM golatest AS docsgen
17+
WORKDIR /src
18+
ENV CGO_ENABLED=0
19+
RUN --mount=target=. \
20+
--mount=target=/root/.cache,type=cache \
21+
--mount=target=/go/pkg/mod,type=cache \
22+
go build -mod=vendor -o /out/docsgen ./docs/generate.go
23+
24+
FROM alpine AS gen
25+
RUN apk add --no-cache rsync git
26+
WORKDIR /src
27+
COPY --from=docsgen /out/docsgen /usr/bin
28+
COPY --from=buildctl /usr/bin/buildctl /usr/bin/
29+
RUN --mount=target=/context \
30+
--mount=target=.,type=tmpfs <<EOT
31+
set -e
32+
rsync -a /context/. .
33+
docsgen
34+
mkdir /out
35+
cp -r docs/* /out
36+
EOT
37+
38+
FROM scratch AS update
39+
COPY --from=gen /out /
40+
41+
FROM gen AS validate
42+
RUN --mount=target=/context \
43+
--mount=target=.,type=tmpfs <<EOT
44+
set -e
45+
rsync -a /context/. .
46+
git add -A
47+
rm -rf docs/*
48+
cp -rf /out/* ./docs/
49+
if [ -n "$(git status --porcelain -- docs/)" ]; then
50+
echo >&2 'ERROR: Docs result differs. Please update with "make docs"'
51+
git status --porcelain -- docs/
52+
exit 1
53+
fi
54+
EOT

0 commit comments

Comments
 (0)