forked from koalaman/shellcheck
-
Notifications
You must be signed in to change notification settings - Fork 1
/
.multi_arch_docker
executable file
·113 lines (101 loc) · 3.81 KB
/
.multi_arch_docker
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
#!/bin/bash
# This script builds and deploys multi-architecture docker images from the
# binaries previously built and deployed to GCS by the Travis pipeline.
if [[ "$TRAVIS_SECURE_ENV_VARS" != "true" ]]
then
echo >&2 "Missing TRAVIS_SECURE_ENV_VARS. Skipping Docker builds."
exit 0
fi
function multi_arch_docker::install_docker_buildx() {
# Install up-to-date version of docker, with buildx support.
local -r docker_apt_repo='https://download.docker.com/linux/ubuntu'
curl -fsSL "${docker_apt_repo}/gpg" | sudo apt-key add -
local -r os="$(lsb_release -cs)"
sudo add-apt-repository "deb [arch=amd64] $docker_apt_repo $os stable"
sudo apt-get update
sudo apt-get -y -o Dpkg::Options::="--force-confnew" install docker-ce
# Enable docker daemon experimental support (for 'pull --platform').
local -r config='/etc/docker/daemon.json'
if [[ -e "$config" ]]; then
sudo sed -i -e 's/{/{ "experimental": true, /' "$config"
else
echo '{ "experimental": true }' | sudo tee "$config"
fi
sudo systemctl restart docker
# Install QEMU multi-architecture support for docker buildx.
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
# Instantiate docker buildx builder with multi-architecture support.
export DOCKER_CLI_EXPERIMENTAL=enabled
docker buildx create --name mybuilder
docker buildx use mybuilder
# Start up buildx and verify that all is OK.
docker buildx inspect --bootstrap
}
# Log in to Docker Hub for deployment.
function multi_arch_docker::login_to_docker_hub() {
echo "$DOCKER_PASSWORD" | docker login -u="$DOCKER_USERNAME" --password-stdin
}
# Run buildx build and push. Passed in arguments augment the command line.
function multi_arch_docker::buildx() {
mkdir -p /tmp/empty
docker buildx build \
--platform "${DOCKER_PLATFORMS// /,}" \
--push \
--progress plain \
-f Dockerfile.multi-arch \
"$@" \
/tmp/empty
rmdir /tmp/empty
}
# Build and push plain and alpine docker images for all tags.
function multi_arch_docker::build_and_push_all() {
for tag in $TAGS; do
multi_arch_docker::buildx -t "$DOCKER_BASE:$tag" --build-arg "tag=$tag"
multi_arch_docker::buildx -t "$DOCKER_BASE-alpine:$tag" \
--build-arg "tag=$tag" --target alpine
done
}
# Test all pushed docker images.
function multi_arch_docker::test_all() {
printf '%s\n' "#!/bin/sh" "echo 'hello world'" > myscript
for platform in $DOCKER_PLATFORMS; do
for tag in $TAGS; do
for ext in '-alpine' ''; do
image="${DOCKER_BASE}${ext}:${tag}"
msg="Testing docker image $image on platform $platform"
line="${msg//?/=}"
printf '\n%s\n%s\n%s\n' "${line}" "${msg}" "${line}"
docker pull -q --platform "$platform" "$image"
if [ -n "$ext" ]; then
echo -n "Image architecture: "
docker run --rm --entrypoint /bin/sh "$image" -c 'uname -m'
version=$(docker run --rm "$image" shellcheck --version \
| grep 'version:')
else
version=$(docker run --rm "$image" --version | grep 'version:')
fi
version=${version/#version: /v}
echo "shellcheck version: $version"
if [[ ! ("$tag" =~ ^(latest|stable)$) && "$tag" != "$version" ]]; then
echo "Version mismatch: shellcheck $version tagged as $tag"
exit 1
fi
if [ -n "$ext" ]; then
docker run --rm -v "$PWD:/mnt" -w /mnt "$image" shellcheck myscript
else
docker run --rm -v "$PWD:/mnt" "$image" myscript
fi
done
done
done
}
function multi_arch_docker::main() {
export DOCKER_PLATFORMS='linux/amd64'
DOCKER_PLATFORMS+=' linux/arm64'
DOCKER_PLATFORMS+=' linux/arm/v6'
multi_arch_docker::install_docker_buildx
multi_arch_docker::login_to_docker_hub
multi_arch_docker::build_and_push_all
set +x
multi_arch_docker::test_all
}