Skip to content

Commit

Permalink
make multiarch work
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhoesel committed Oct 13, 2022
1 parent 3dcf65d commit e281e96
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 67 deletions.
76 changes: 56 additions & 20 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ jobs:
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_TOKEN }}

build-release-artifact:
build:
name: Build ${{ matrix.target.name }}
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
strategy:
matrix:
Expand All @@ -61,7 +60,8 @@ jobs:
with:
key: "{{ matrix.target.name }}"

- uses: XAMPPRocky/get-github-release@v1
- name: Get cross
uses: XAMPPRocky/get-github-release@v1
id: cross
with:
owner: cross-rs
Expand All @@ -77,8 +77,39 @@ jobs:
- name: Build release binary
run: cross build --release --target ${{ matrix.target.name }}

- name: Upload binary artifact
uses: actions/upload-artifact@v3
with:
name: ${{ github.event.repository.name }}_${{ matrix.target.name }}
path: target/${{ matrix.target.name }}/release/${{ github.event.repository.name }}${{ matrix.target.extension }}

gh-release-artifacts:
name: Upload ${{ matrix.target }} to GH Release
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
needs:
- build
strategy:
matrix:
target:
- name: aarch64-unknown-linux-gnu
extension: ""
- name: x86_64-unknown-linux-gnu
extension: ""
- name: x86_64-pc-windows-gnu
extension: ".exe"
steps:
- name: Check out codebase
uses: actions/checkout@v3

- name: Get target binary artifact
uses: actions/download-artifact@v3
with:
name: ${{ github.event.repository.name }}_${{ matrix.target.name }}
path: /tmp/${{ github.event.repository.name }}_${{ matrix.target.name }}${{ matrix.target.extension }}

- name: Create release asset archive
run: tar cvzf binary.tar.gz --directory=target/${{ matrix.target.name }}/release "${{ github.event.repository.name }}${{ matrix.target.extension }}"
run: tar cvzf binary.tar.gz --directory=/tmp ${{ github.event.repository.name }}_${{ matrix.target.name }}${{ matrix.target.extension }}

- name: Get current GitHub release
id: get_release
Expand All @@ -98,29 +129,31 @@ jobs:
asset_content_type: application/tar+gzip

docker:
name: "Publish Docker images"
name: "Publish Docker image"
runs-on: ubuntu-latest
needs:
- build
steps:
- name: Check out codebase
uses: actions/checkout@v3

- name: Install rust toolchain
uses: actions-rs/toolchain@v1
with:
target: "${{ matrix.target.name }}"
override: true
toolchain: stable
- name: Enable cargo caching
uses: Swatinem/rust-cache@v2
with:
key: "{{ matrix.target.name }}"

# Needed for multi-platform images
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2


- name: Create binaries folder
run: mkdir binaries
- name: Get amd64 artifact
uses: actions/download-artifact@v3
with:
name: ${{ github.event.repository.name }}_x86_64-unknown-linux-gnu
path: binaries/${{ github.event.repository.name }}_amd64
- name: Get arm64 artifact
uses: actions/download-artifact@v3
with:
name: ${{ github.event.repository.name }}_aarch64-unknown-linux-gnu
path: binaries/${{ github.event.repository.name }}_arm64

- name: Login to Docker Hub
uses: docker/login-action@v2
with:
Expand Down Expand Up @@ -155,11 +188,14 @@ jobs:
type=semver,pattern={{major}}.{{minor}},enable=${{ startsWith(github.ref, 'refs/tags/v')}}
type=semver,pattern={{major}},enable=${{ startsWith(github.ref, 'refs/tags/v') && !startsWith(github.ref, 'refs/tags/v0.') }}
latest
- name: Build and push
uses: docker/build-push-action@v3
with:
context: .
platforms: linux/amd64,linux/arm64
tags: ${{ steps.tags.outputs.tags }}
push: true
file: Dockerfile.multiarch
build-args: |
binary_dir=binaries
binary_name=${{ github.event.repository.name }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ lcov.info

# Python venvs
.venv

# Docker stuff
binaries
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
FROM rust:1.64 as builder

WORKDIR /usr/src/app
WORKDIR /usr/local/src

COPY . .

RUN cargo install --path .

FROM debian:bullseye-slim

COPY --from=builder /usr/local/cargo/bin/clouddns-nat-helper /usr/local/bin/clouddns-nat-helper
RUN chmod +x /usr/local/bin/clouddns-nat-helper

# run unprivileged
USER 1001
Expand Down
30 changes: 30 additions & 0 deletions Dockerfile.multiarch
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM debian:bullseye-slim as builder

ARG TARGETPLATFORM
ARG binary_dir
ENV BINARY_DIR=${binary_dir}

ARG binary_name
ENV BINARY_NAME=${binary_name}

COPY ${BINARY_DIR}/* /tmp/binaries/
# Select the correct binary and put it to a predictable location
RUN case ${TARGETPLATFORM} in \
"linux/amd64") export TARGET="amd64" ;; \
"linux/arm64") export TARGET="arm64" ;; \
esac; \
mv /tmp/binaries/${BINARY_NAME}_${TARGET} /root/${BINARY_NAME}


FROM debian:bullseye-slim

ARG binary_name
ENV BINARY_NAME=${binary_name}

COPY --from=builder /root/${BINARY_NAME} /usr/local/bin/
RUN chmod +x /usr/local/bin/${BINARY_NAME}

# run unprivileged
USER 1001

CMD ["${BINARY_NAME}"]
112 changes: 74 additions & 38 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
[config]
skip_core_tasks = true

[env.development]
TARGET_FOLDER = "debug"

[env.release]
RELEASE_FLAG = "--release"
TARGET_FOLDER = "release"

# DEPENDENCIES
[tasks.install-clippy]
private = true
Expand Down Expand Up @@ -46,89 +53,112 @@ dependencies = [
# LINTING
[tasks.lint]
description = "Lint this project"
category = "lint"
dependencies = ["clippy", "format"]
[tasks.clippy]
category = "lint"
description = "Run clippy"
dependencies = ["install-clippy"]
command = "cargo"
args = ["clippy"]
[tasks.format]
category = "line"
description = "Format the codebase"
dependencies = ["install-rustfmt"]
command = "cargo"
args = ["fmt"]

# BUILDS
[tasks.build]
category = "build"
description = "Build the project"
dependencies = [
"build-development-default",
"build-development-target",
"build-release-target",
"build-release-default"
]
[tasks.build-development-target]
private = true
condition = { env_true = ["TARGET"], profiles = ["development"] }
command = "cargo"
args = ["build", "--all-features", "@@remove-empty(RELEASE_FLAG)"]
# Officially supported targets
[tasks.build-x86_64-unknown-linux-gnu]
category = "build"
dependencies = ["install-cross"]
command = "cross"
args = ["build", "--all-features", "--target", "${TARGET}"]
[tasks.build-development-default]
private = true
condition = { env_not_set = ["TARGET"], profiles = ["development"] }
args = ["build", "--all-features", "--target", "x86_64-unknown-linux-gnu", "@@remove-empty(RELEASE_FLAG)"]
[tasks.build-aarch64-unknown-linux-gnu]
category = "build"
dependencies = ["install-cross"]
command = "cross"
args = ["build", "--all-features"]
[tasks.build-release-target]
private = true
condition = { env_true = ["TARGET"], profiles = ["release"] }
args = ["build", "--all-features", "--target", "aarch64-unknown-linux-gnu", "@@remove-empty(RELEASE_FLAG)"]
[tasks.build-x86_64-pc-windows-gnu]
category = "build"
dependencies = ["install-cross"]
command = "cross"
args = ["build", "--all-features", "--target", "${TARGET}", "--release"]
[tasks.build-release-default]
private = true
condition = { env_not_set = ["TARGET"], profiles = ["release"] }
dependencies = ["install-cross"]
command = "cross"
args = ["build", "--all-features", "--release"]
args = ["build", "--all-features", "--target", "x86_64-pc-windows-gnu", "@@remove-empty(RELEASE_FLAG)"]


# TESTS
[tasks.test]
category = "test"
description = "Test the project"
dependencies = [
"test-default",
"test-target"
]
[tasks.test-target]
private = true
condition = { env_true = ["TARGET"] }
command = "cargo"
args = ["test", "--all-features"]
# Officially supported targets
[tasks.test-x86_64-unknown-linux-gnu]
category = "test"
dependencies = ["install-cross"]
command = "cross"
args = ["test", "--all-features", "--target", "${TARGET}"]
[tasks.test-default]
private = true
condition = { env_not_set = ["TARGET"] }
args = ["test", "--all-features", "--target", "x86_64-unknown-linux-gnu"]
[tasks.test-aarch64-unknown-linux-gnu]
category = "test"
dependencies = ["install-cross"]
command = "cross"
args = ["test", "--all-features"]
args = ["test", "--all-features", "--target", "aarch64-unknown-linux-gnu"]
[tasks.test-x86_64-pc-windows-gnu]
category = "test"
dependencies = ["install-cross"]
command = "cross"
args = ["test", "--all-features", "--target", "x86_64-pc-windows-gnu"]

[tasks.docker]
description = "Build a local docker image"
command = "docker"
args = [
"build",
"-t",
"${CARGO_MAKE_PROJECT_NAME}",
"${DOCKER_TAG}",
"."
]
[tasks.docker-multiarch]
description = "Build a multiarch docker image"
dependencies = ["docker-collect-binaries"]
command = "docker"
args = [
"buildx", "build",
"-f", "Dockerfile.multiarch",
"--platform", "linux/amd64,linux/arm64",
"--build-arg", "binary_dir=binaries",
"--build-arg", "binary_name=${CARGO_MAKE_CRATE_NAME}",
"--tag", "${DOCKER_TAG}",
"--push", "."
]
[tasks.docker-collect-binaries]
private = true
dependencies = [
"build-aarch64-unknown-linux-gnu",
"build-x86_64-unknown-linux-gnu",
]
description = "Collect compiled binaries into central location for docker buildx"
script = '''
#!/usr/bin/env bash
mkdir -p ./binaries
cp target/x86_64-unknown-linux-gnu/${TARGET_FOLDER}/${CARGO_MAKE_CRATE_NAME} binaries/${CARGO_MAKE_CRATE_NAME}_amd64
cp target/aarch64-unknown-linux-gnu/${TARGET_FOLDER}/${CARGO_MAKE_CRATE_NAME} binaries/${CARGO_MAKE_CRATE_NAME}_arm64
'''



# MISC
[tasks.docs]
description = "Generate rustdoc for the project"
command = "cargo"
args = ["doc", "--no-deps"]


[tasks.coverage]
description = "Generate a coverage report"
install_crate = "cargo-llvm-cov"
Expand All @@ -137,5 +167,11 @@ args = ["llvm-cov", "--lcov", "--output-path", "lcov.info"]

[tasks.clean]
description = "Cleanup"
dependencies = ["clean-docker-multiarch"]
command = "cargo"
args = ["clean"]

[tasks.clean-docker-multiarch]
private = true
command = "rm"
args = ["-rf", "./binaries"]
Loading

0 comments on commit e281e96

Please sign in to comment.