naftulikay / rust-fuzzy-datetime

An example project to demonstrate the features and characteristics of "fuzzy" datetimes which can represent historical dates and time periods.
Other
0 stars 0 forks source link

Build Multi-Platform Docker Images in GitHub Actions #2

Open naftulikay opened 1 year ago

naftulikay commented 1 year ago

It would be especially cool to see multi-platform Docker images being built, specifically for Linux amd64 and arm64 (aarch64).

We'd need to add the QEMU setup step:

 - uses: docker/setup-qemu-action@v2

We'd also need to emend the build step to add platforms:

- name: build
  uses: docker/build-push-action@v2
  with:
    context: ./
    file: ./Dockerfile
    push: false
    tags: naftulikay/fuzzy-datetime:latest
    platforms: linux/amd64,linux/arm64
    # we include audit info in the binary, do not strip the binary, and skip the audit and tests because these are
    # handled in other GitHub Actions workflows.
    build-args: |
      RUST_AUDITABLE_BINARY=true
      RUST_STRIP_BINARY=false
      RUST_RUN_AUDIT=false
      RUST_RUN_TESTS=false

An example can be found here, documentation of docker buildx can be found here, and here is some documentation on the arguments available to the image when building with buildx.

Essentially, we'd need to take advantage of some of the per-platform variables present:

We could verify this locally by using buildx, and we'd need to use conditionals in the Dockerfile:

RUN rustup target add x86_64-unknown-linux-musl
# ...
RUN cargo build --target x86_64-unknown-linux-musl
# ...
RUN strip target/x86_64-unknown-linux-musl/release/${APP_BIN_NAME}
# ...
# copy built binaries
COPY --from=build /usr/src/app/target/x86_64-unknown-linux-musl/release/fuzzydatetime /app
COPY --from=build /usr/src/app/target/x86_64-unknown-linux-musl/release/examples/static-test /app-static-test

All of these would need to be emended so that when TARGETARCH is amd64, we yield x86_64, for arm64, we yield aarch64.