Skip to content

Commit

Permalink
Use a local script for pushing Docker images
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementNerma committed Dec 8, 2024
1 parent d37eb3a commit 89c5298
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 39 deletions.
29 changes: 0 additions & 29 deletions .github/workflows/docker.yml

This file was deleted.

19 changes: 9 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
FROM rust:alpine AS builder
# This file is not intended to be used directly ;
# please refer to the build script instead

RUN apk add musl-dev perl make
FROM debian:bullseye-slim

WORKDIR /app

COPY Cargo.toml Cargo.lock .
COPY src src
RUN cargo build --release

FROM alpine
# These two are provided by docker buildx
ARG TARGETOS
ARG TARGETARCH
ARG TARGETVARIANT

WORKDIR /app
COPY --from=builder /app/target/release/tapo-rest .
COPY target/building-for-docker/artifacts/${TARGETOS}/${TARGETARCH}/${TARGETVARIANT}/tapo-rest ./

ENTRYPOINT ["./tapo-rest", "/app/devices.json", "--port=80"]
49 changes: 49 additions & 0 deletions docker-build.rsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Due to difficulties building cross-platform binaries with Docker,
# I resort here to building locally using `cross` and then copying the
# output into a container.

# Ensure 'cross' is installed
if whereis('cross') == null {
throw 'Please install "cross" to continue.'
}

# Ensure a container engine is installed
if whereis('docker') == null && whereis('podman') == null {
throw 'Please install either "docker" or "podman" to continue.'
}

# Get name and version from the manifest
let manifest = parseToml(readFile('Cargo.toml'))
let { name, version } = $manifest['package']

# List build targets
let targets = map([
['aarch64-unknown-linux-musl', 'linux/arm64'],
['x86_64-unknown-linux-musl' , 'linux/amd64']
])

# Build for every image
let buildDir = 'target/building-for-docker'

for target, dockerPlatform in $targets {
echo "\nBuilding for target: $target...\n"

# We use a different target directory for each crate and each target, otherwise dependencies build
# may clash between platforms
let targetDir = "$buildDir/targets/$target"
mkdir -p -i $targetDir
cross build --release --target $target --target-dir $targetDir

# Put build artifact in the correct directory
let artifactsFile = "$buildDir/artifacts/$dockerPlatform/$name"
mkdir -p (parentDir($artifactsFile))
cp "$targetDir/$target/release/$name" $artifactsFile
}

# Build and publish Docker images
echo '\nPublishing on Docker Hub...\n'

docker buildx build --push . \
--platform ($targets.values().join(',')) \
--tag "clementnerma/$name:$version" \
--tag "clementnerma/$name:latest"

0 comments on commit 89c5298

Please sign in to comment.