|
| 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