File: test-driver

package info (click to toggle)
docker-buildx 0.13.1%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,356 kB
  • sloc: sh: 299; makefile: 87
file content (180 lines) | stat: -rwxr-xr-x 4,340 bytes parent folder | download
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
#!/usr/bin/env bash

set -eu -o pipefail

: "${BUILDX_CMD=docker buildx}"
: "${BUILDKIT_IMAGE=moby/buildkit:buildx-stable-1}"
: "${BUILDKIT_CFG=}"
: "${DRIVER=docker-container}"
: "${DRIVER_OPT=}"
: "${ENDPOINT=}"
: "${MULTI_NODE=0}"
: "${PLATFORMS=linux/amd64,linux/arm64}"

function buildxCmd {
  (set -x ; $BUILDX_CMD "$@")
}

function clean {
  rm -rf "$context"
  if [ "$builderName" != "default" ]; then
    buildxCmd rm "$builderName"
  fi
}

context=$(mktemp -d -t buildx-output.XXXXXXXXXX)
dockerfile=${context}/Dockerfile
bakedef=${context}/docker-bake.hcl
trap clean EXIT

builderName=buildx-test-$(openssl rand -hex 16)
buildPlatformFlag=
if [ "$DRIVER" = "docker" ]; then
  builderName=default
else
  buildPlatformFlag=--platform="${PLATFORMS}"
fi

if [ "$DRIVER" != "remote" ]; then
  driverOpt=${driverOpt:+"${driverOpt},"}image=${BUILDKIT_IMAGE}
fi
if [ -n "$DRIVER_OPT" ]; then
  driverOpt=${driverOpt:+"${driverOpt},"}$DRIVER_OPT
fi

# create builder except for docker driver
if [ "$DRIVER" != "docker" ]; then
  if [ "${MULTI_NODE}" = "1" ]; then
    firstNode=1
    for platform in ${PLATFORMS//,/ }; do
      createFlags=""
      if [ -f "$BUILDKIT_CFG" ]; then
        createFlags="$createFlags --buildkitd-config=${BUILDKIT_CFG}"
      fi
      if [ "$firstNode" = "0" ]; then
        createFlags="$createFlags --append"
      fi
      nodeName=""
      if [ "$DRIVER" != "kubernetes" ]; then
        nodeName="${builderName}-${platform/\//-}"
      fi
      buildxCmd create ${createFlags} \
        --bootstrap \
        --name="${builderName}" \
        --node="${nodeName}" \
        --platform="${platform}" \
        --driver="${DRIVER}" \
        ${driverOpt:+"--driver-opt=${driverOpt}"} \
        ${ENDPOINT}
      firstNode=0
    done
  else
    createFlags=""
    if [ -f "$BUILDKIT_CFG" ]; then
      createFlags="$createFlags --buildkitd-config=${BUILDKIT_CFG}"
    fi
    buildxCmd create ${createFlags} \
      --bootstrap \
      --name="${builderName}" \
      --platform="${PLATFORMS}" \
      --driver="${DRIVER}" \
      ${driverOpt:+"--driver-opt=${driverOpt}"} \
      ${ENDPOINT}
  fi
fi

function buildOutput {
  local name=$1
  if [ "$DRIVER" != "docker" ]; then
    if [ "${MULTI_NODE}" = "1" ]; then
      echo "type=cacheonly"
    else
      echo "type=oci,dest=${context}/${name}.tar"
    fi
  else
    echo "type=docker,name=${name}"
  fi
}

# multi-platform not supported by docker driver
buildPlatformFlag=
bakePlatformFlag=
if [ "$DRIVER" != "docker" ]; then
  buildPlatformFlag=--platform="${PLATFORMS}"
  bakePlatformFlag=--set="*.platform=${PLATFORMS}"
fi

# inspect and bootstrap
buildxCmd inspect --bootstrap --builder="${builderName}"

# create dockerfile
cat > "${dockerfile}" <<EOL
FROM busybox as build
ARG TARGETPLATFORM
ARG BUILDPLATFORM
RUN echo "I am running on \$BUILDPLATFORM, building for \$TARGETPLATFORM" > /log

FROM busybox AS log
COPY --from=build /log /log
RUN cat /log
RUN uname -a

FROM busybox AS hello
RUN echo hello > /hello

FROM scratch
COPY --from=log /log /log
COPY --from=hello /hello /hello
EOL

# build
buildxCmd build ${buildPlatformFlag} \
  --output="$(buildOutput buildx-test-build)" \
  --builder="${builderName}" \
  --metadata-file="${context}/metadata-build.json" \
  "${context}"
cat "${context}/metadata-build.json"

# load to docker store
if [ "$DRIVER" != "docker" ]; then
  buildxCmd build \
    --output="type=docker,name=buildx-test-load" \
    --builder="${builderName}" \
    "${context}"
fi

# create bake def
cat > "${bakedef}" <<EOL
group "default" {
  targets = ["release"]
}
group "all" {
  targets = ["log", "hello"]
}
target "release" {
  output = ["$(buildOutput buildx-test-bake-release)"]
}
target "log" {
  output = ["$(buildOutput buildx-test-bake-log)"]
}
target "hello" {
  output = ["$(buildOutput buildx-test-bake-hello)"]
}
EOL

# bake default target
buildxCmd bake ${bakePlatformFlag} \
  --file="${bakedef}" \
  --builder="${builderName}" \
  --set "*.context=${context}" \
  --metadata-file="${context}/metadata-bake-def.json"
cat "${context}/metadata-bake-def.json"

# bake all target
buildxCmd bake ${bakePlatformFlag} \
  --file="${bakedef}" \
  --builder="${builderName}" \
  --set "*.context=${context}" \
  --metadata-file="${context}/metadata-bake-all.json" \
  all
cat "${context}/metadata-bake-all.json"