Skip to content

Commit 89c5298

Browse files
committed
Use a local script for pushing Docker images
1 parent d37eb3a commit 89c5298

File tree

3 files changed

+58
-39
lines changed

3 files changed

+58
-39
lines changed

.github/workflows/docker.yml

Lines changed: 0 additions & 29 deletions
This file was deleted.

Dockerfile

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
FROM rust:alpine AS builder
1+
# This file is not intended to be used directly ;
2+
# please refer to the build script instead
23

3-
RUN apk add musl-dev perl make
4+
FROM debian:bullseye-slim
45

5-
WORKDIR /app
6-
7-
COPY Cargo.toml Cargo.lock .
8-
COPY src src
9-
RUN cargo build --release
10-
11-
FROM alpine
6+
# These two are provided by docker buildx
7+
ARG TARGETOS
8+
ARG TARGETARCH
9+
ARG TARGETVARIANT
1210

1311
WORKDIR /app
14-
COPY --from=builder /app/target/release/tapo-rest .
12+
COPY target/building-for-docker/artifacts/${TARGETOS}/${TARGETARCH}/${TARGETVARIANT}/tapo-rest ./
13+
1514
ENTRYPOINT ["./tapo-rest", "/app/devices.json", "--port=80"]

docker-build.rsh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Due to difficulties building cross-platform binaries with Docker,
2+
# I resort here to building locally using `cross` and then copying the
3+
# output into a container.
4+
5+
# Ensure 'cross' is installed
6+
if whereis('cross') == null {
7+
throw 'Please install "cross" to continue.'
8+
}
9+
10+
# Ensure a container engine is installed
11+
if whereis('docker') == null && whereis('podman') == null {
12+
throw 'Please install either "docker" or "podman" to continue.'
13+
}
14+
15+
# Get name and version from the manifest
16+
let manifest = parseToml(readFile('Cargo.toml'))
17+
let { name, version } = $manifest['package']
18+
19+
# List build targets
20+
let targets = map([
21+
['aarch64-unknown-linux-musl', 'linux/arm64'],
22+
['x86_64-unknown-linux-musl' , 'linux/amd64']
23+
])
24+
25+
# Build for every image
26+
let buildDir = 'target/building-for-docker'
27+
28+
for target, dockerPlatform in $targets {
29+
echo "\nBuilding for target: $target...\n"
30+
31+
# We use a different target directory for each crate and each target, otherwise dependencies build
32+
# may clash between platforms
33+
let targetDir = "$buildDir/targets/$target"
34+
mkdir -p -i $targetDir
35+
cross build --release --target $target --target-dir $targetDir
36+
37+
# Put build artifact in the correct directory
38+
let artifactsFile = "$buildDir/artifacts/$dockerPlatform/$name"
39+
mkdir -p (parentDir($artifactsFile))
40+
cp "$targetDir/$target/release/$name" $artifactsFile
41+
}
42+
43+
# Build and publish Docker images
44+
echo '\nPublishing on Docker Hub...\n'
45+
46+
docker buildx build --push . \
47+
--platform ($targets.values().join(',')) \
48+
--tag "clementnerma/$name:$version" \
49+
--tag "clementnerma/$name:latest"

0 commit comments

Comments
 (0)