Skip to content

Commit 882b15e

Browse files
authored
Merge pull request #113 from infosiftr/run-mount-from-image
Move Dockerfile parsing to a dedicated package, add support for `RUN --mount=type=bind,from=...`
2 parents 9939a32 + 60ee93c commit 882b15e

File tree

11 files changed

+397
-137
lines changed

11 files changed

+397
-137
lines changed

.github/workflows/ci.yml

+8
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ jobs:
5555
# files: coverage.out
5656
# fail_ci_if_error: true
5757
# verbose: true
58+
# in the meantime, upload coverage to GHA
59+
- run: docker run --rm -i test go tool cover -html /dev/stdin -o /dev/stdout < coverage.out > coverage.html
60+
- uses: actions/upload-artifact@v4
61+
with:
62+
name: coverage
63+
path: coverage.*
64+
include-hidden-files: true
65+
if-no-files-found: error
5866
dockerfile:
5967
name: Test Dockerfile
6068
runs-on: ubuntu-latest

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.20-bullseye AS build
1+
FROM golang:1.21-bookworm AS build
22

33
SHELL ["bash", "-Eeuo", "pipefail", "-xc"]
44

Dockerfile.release

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.20-bullseye
1+
FROM golang:1.21-bookworm
22

33
SHELL ["bash", "-Eeuo", "pipefail", "-xc"]
44

Dockerfile.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.20-bullseye
1+
FROM golang:1.21-bookworm
22

33
SHELL ["bash", "-Eeuo", "pipefail", "-xc"]
44

bashbrew.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ set -Eeuo pipefail
66
dir="$(readlink -f "$BASH_SOURCE")"
77
dir="$(dirname "$dir")"
88

9-
: "${CGO_ENABLED:=0}"
10-
export GO111MODULE=on CGO_ENABLED
9+
: "${CGO_ENABLED=0}" "${GOTOOLCHAIN=local}"
10+
export CGO_ENABLED GOTOOLCHAIN
1111
(
1212
cd "$dir"
13-
go build -o bin/bashbrew ./cmd/bashbrew > /dev/null
13+
go build -trimpath -o bin/bashbrew ./cmd/bashbrew > /dev/null
1414
)
1515

1616
exec "$dir/bin/bashbrew" "$@"

cmd/bashbrew/docker.go

+16-122
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"bufio"
54
"bytes"
65
"crypto/sha256"
76
"encoding/hex"
@@ -10,21 +9,14 @@ import (
109
"os"
1110
"os/exec"
1211
"path"
13-
"strconv"
1412
"strings"
13+
"sync"
1514

1615
"github.com/docker-library/bashbrew/manifest"
16+
"github.com/docker-library/bashbrew/pkg/dockerfile"
1717
"github.com/urfave/cli"
1818
)
1919

20-
type dockerfileMetadata struct {
21-
StageFroms []string // every image "FROM" instruction value (or the parent stage's FROM value in the case of a named stage)
22-
StageNames []string // the name of any named stage (in order)
23-
StageNameFroms map[string]string // map of stage names to FROM values (or the parent stage's FROM value in the case of a named stage), useful for resolving stage names to FROM values
24-
25-
Froms []string // every "FROM" or "COPY --from=xxx" value (minus named and/or numbered stages in the case of "--from=")
26-
}
27-
2820
// this returns the "FROM" value for the last stage (which essentially determines the "base" for the final published image)
2921
func (r Repo) ArchLastStageFrom(arch string, entry *manifest.Manifest2822Entry) (string, error) {
3022
dockerfileMeta, err := r.archDockerfileMetadata(arch, entry)
@@ -46,27 +38,25 @@ func (r Repo) ArchDockerFroms(arch string, entry *manifest.Manifest2822Entry) ([
4638
return dockerfileMeta.Froms, nil
4739
}
4840

49-
func (r Repo) dockerfileMetadata(entry *manifest.Manifest2822Entry) (*dockerfileMetadata, error) {
41+
func (r Repo) dockerfileMetadata(entry *manifest.Manifest2822Entry) (dockerfile.Metadata, error) {
5042
return r.archDockerfileMetadata(arch, entry)
5143
}
5244

53-
var dockerfileMetadataCache = map[string]*dockerfileMetadata{}
45+
var (
46+
dockerfileMetadataCache = map[string]dockerfile.Metadata{}
47+
scratchDockerfileMetadata = sync.OnceValues(func() (dockerfile.Metadata, error) {
48+
return dockerfile.Parse(`FROM scratch`)
49+
})
50+
)
5451

55-
func (r Repo) archDockerfileMetadata(arch string, entry *manifest.Manifest2822Entry) (*dockerfileMetadata, error) {
52+
func (r Repo) archDockerfileMetadata(arch string, entry *manifest.Manifest2822Entry) (dockerfile.Metadata, error) {
5653
if builder := entry.ArchBuilder(arch); builder == "oci-import" {
57-
return &dockerfileMetadata{
58-
StageFroms: []string{
59-
"scratch",
60-
},
61-
Froms: []string{
62-
"scratch",
63-
},
64-
}, nil
54+
return scratchDockerfileMetadata()
6555
}
6656

6757
commit, err := r.fetchGitRepo(arch, entry)
6858
if err != nil {
69-
return nil, cli.NewMultiError(fmt.Errorf("failed fetching Git repo for arch %q from entry %q", arch, entry.String()), err)
59+
return dockerfile.Metadata{}, cli.NewMultiError(fmt.Errorf("failed fetching Git repo for arch %q from entry %q", arch, entry.String()), err)
7060
}
7161

7262
dockerfileFile := path.Join(entry.ArchDirectory(arch), entry.ArchFile(arch))
@@ -79,116 +69,20 @@ func (r Repo) archDockerfileMetadata(arch string, entry *manifest.Manifest2822En
7969
return meta, nil
8070
}
8171

82-
dockerfile, err := gitShow(commit, dockerfileFile)
72+
df, err := gitShow(commit, dockerfileFile)
8373
if err != nil {
84-
return nil, cli.NewMultiError(fmt.Errorf(`failed "git show" for %q from commit %q`, dockerfileFile, commit), err)
74+
return dockerfile.Metadata{}, cli.NewMultiError(fmt.Errorf(`failed "git show" for %q from commit %q`, dockerfileFile, commit), err)
8575
}
8676

87-
meta, err := parseDockerfileMetadata(dockerfile)
77+
meta, err := dockerfile.Parse(df)
8878
if err != nil {
89-
return nil, cli.NewMultiError(fmt.Errorf(`failed parsing Dockerfile metadata for %q from commit %q`, dockerfileFile, commit), err)
79+
return dockerfile.Metadata{}, cli.NewMultiError(fmt.Errorf(`failed parsing Dockerfile metadata for %q from commit %q`, dockerfileFile, commit), err)
9080
}
9181

9282
dockerfileMetadataCache[cacheKey] = meta
9383
return meta, nil
9484
}
9585

96-
func parseDockerfileMetadata(dockerfile string) (*dockerfileMetadata, error) {
97-
meta := &dockerfileMetadata{
98-
// panic: assignment to entry in nil map
99-
StageNameFroms: map[string]string{},
100-
// (nil slices work fine)
101-
}
102-
103-
scanner := bufio.NewScanner(strings.NewReader(dockerfile))
104-
for scanner.Scan() {
105-
line := strings.TrimSpace(scanner.Text())
106-
107-
if line == "" {
108-
// ignore blank lines
109-
continue
110-
}
111-
112-
if line[0] == '#' {
113-
// TODO handle "escape" parser directive
114-
// TODO handle "syntax" parser directive -- explode appropriately (since custom syntax invalidates our Dockerfile parsing)
115-
// ignore comments
116-
continue
117-
}
118-
119-
// handle line continuations
120-
// (TODO see note above regarding "escape" parser directive)
121-
for line[len(line)-1] == '\\' && scanner.Scan() {
122-
nextLine := strings.TrimSpace(scanner.Text())
123-
if nextLine == "" || nextLine[0] == '#' {
124-
// ignore blank lines and comments
125-
continue
126-
}
127-
line = line[0:len(line)-1] + nextLine
128-
}
129-
130-
fields := strings.Fields(line)
131-
if len(fields) < 1 {
132-
// must be a much more complex empty line??
133-
continue
134-
}
135-
instruction := strings.ToUpper(fields[0])
136-
137-
// TODO balk at ARG / $ in from values
138-
139-
switch instruction {
140-
case "FROM":
141-
from := fields[1]
142-
143-
if stageFrom, ok := meta.StageNameFroms[from]; ok {
144-
// if this is a valid stage name, we should resolve it back to the original FROM value of that previous stage (we don't care about inter-stage dependencies for the purposes of either tag dependency calculation or tag building -- just how many there are and what external things they require)
145-
from = stageFrom
146-
}
147-
148-
// make sure to add ":latest" if it's implied
149-
from = latestizeRepoTag(from)
150-
151-
meta.StageFroms = append(meta.StageFroms, from)
152-
meta.Froms = append(meta.Froms, from)
153-
154-
if len(fields) == 4 && strings.ToUpper(fields[2]) == "AS" {
155-
stageName := fields[3]
156-
meta.StageNames = append(meta.StageNames, stageName)
157-
meta.StageNameFroms[stageName] = from
158-
}
159-
case "COPY":
160-
for _, arg := range fields[1:] {
161-
if !strings.HasPrefix(arg, "--") {
162-
// doesn't appear to be a "flag"; time to bail!
163-
break
164-
}
165-
if !strings.HasPrefix(arg, "--from=") {
166-
// ignore any flags we're not interested in
167-
continue
168-
}
169-
from := arg[len("--from="):]
170-
171-
if stageFrom, ok := meta.StageNameFroms[from]; ok {
172-
// see note above regarding stage names in FROM
173-
from = stageFrom
174-
} else if stageNumber, err := strconv.Atoi(from); err == nil && stageNumber < len(meta.StageFroms) {
175-
// must be a stage number, we should resolve it too
176-
from = meta.StageFroms[stageNumber]
177-
}
178-
179-
// make sure to add ":latest" if it's implied
180-
from = latestizeRepoTag(from)
181-
182-
meta.Froms = append(meta.Froms, from)
183-
}
184-
}
185-
}
186-
if err := scanner.Err(); err != nil {
187-
return nil, err
188-
}
189-
return meta, nil
190-
}
191-
19286
func (r Repo) DockerCacheName(entry *manifest.Manifest2822Entry) (string, error) {
19387
cacheHash, err := r.dockerCacheHash(entry)
19488
if err != nil {

cmd/bashbrew/repo.go

-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"path"
77
"path/filepath"
88
"sort"
9-
"strings"
109

1110
"github.com/docker-library/bashbrew/manifest"
1211
)
@@ -39,13 +38,6 @@ func repos(all bool, args ...string) ([]string, error) {
3938
return ret, nil
4039
}
4140

42-
func latestizeRepoTag(repoTag string) string {
43-
if repoTag != "scratch" && strings.IndexRune(repoTag, ':') < 0 {
44-
return repoTag + ":latest"
45-
}
46-
return repoTag
47-
}
48-
4941
type Repo struct {
5042
RepoName string
5143
TagName string

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/docker-library/bashbrew
22

3-
go 1.20
3+
go 1.21
44

55
require (
66
github.com/containerd/containerd v1.6.19

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
617617
github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
618618
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
619619
github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU=
620+
github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
620621
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
621622
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
622623
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=

0 commit comments

Comments
 (0)