Skip to content

Commit

Permalink
fix(CI): rework cross compilation (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbbirkisson authored Aug 20, 2024
1 parent b526c60 commit 6c628d0
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 161 deletions.
7 changes: 7 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# apt install gcc-arm-linux-gnueabihf
[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"

# apt install gcc-aarch64-linux-gnu
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"
55 changes: 17 additions & 38 deletions .github/actions/toolchain/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,37 @@
name: toolchain
description: "Install toolchain"

inputs:
bin-cache:
description: 'Where to keep cached bins'
required: false
default: /home/runner/.bin-cache

runs:
using: "composite"
steps:
- name: Setup mise
uses: jdx/mise-action@v2
with:
# version: TODO:
install: true
cache: true
experimental: true

- name: Setup rust caching
uses: Swatinem/rust-cache@v2
with:
shared-key: toolchain
cache-directories: ${{ inputs.bin-cache }}
shared-key: toolchains

- name: "Set environment variables used by toolchain"
shell: bash
run: |
echo CARGO_TERM_COLOR=always >> $GITHUB_ENV
echo CARGO_INCREMENTAL=0 >> $GITHUB_ENV
echo RUST_BACKTRACE=1 >> $GITHUB_ENV
shell: bash

- name: "Install rust toolchain"
shell: bash
run: |
# Make sure bin-cache dir exists with something in it
mkdir -p ${{ inputs.bin-cache }}
touch ${{ inputs.bin-cache }}/empty
cp ${{ inputs.bin-cache }}/* /home/runner/.cargo/bin
# Install everything needed
mise run setup:toolchain
# Install dependencies
cargo install [email protected] # TODO:
cargo install [email protected] # TODO:
- name: Install rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
targets: |
x86_64-unknown-linux-gnu,
armv7-unknown-linux-gnueabihf,
aarch64-unknown-linux-gnu
# Cache these binaries
for f in sqlx cargo-tarpaulin; do
cp -f "/home/runner/.cargo/bin/$f" "${{ inputs.bin-cache }}"
done
- name: Install cross compilation dependencies
uses: awalsh128/cache-apt-pkgs-action@latest
with:
version: 1.0
packages: gcc-arm-linux-gnueabihf gcc-aarch64-linux-gnu

- name: "Setup DB"
- name: Install cargo dependencies
shell: bash
run: |
mise run setup:db
cargo install [email protected] # TODO:
cargo install [email protected] # TODO:
15 changes: 6 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ on:
branches:
- main

env:
DATABASE_URL: sqlite:data/spis.db

jobs:
ci:
name: Run ${{ matrix.target }}
Expand All @@ -19,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
target: ["lint:fmt", "lint:clippy", "test"]
target: ["lint-fmt", "lint-clippy", "test"]

steps:
- name: Checkout
Expand All @@ -29,12 +26,12 @@ jobs:
uses: ./.github/actions/toolchain

- name: Run ${{ matrix.target }}
run: mise run ${{ matrix.target }}
run: make ${{ matrix.target }}
env:
DATABASE_URL: sqlite:data/spis.db

- name: Upload test coverage
uses: codecov/codecov-action@v4
if: matrix.target == 'test'
continue-on-error: true
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
21 changes: 4 additions & 17 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
VERSION: ${{ github.ref_name }}
DATABASE_URL: sqlite:data/spis.db

jobs:
ci:
Expand All @@ -35,22 +34,10 @@ jobs:
- name: Install rust toolchain
uses: ./.github/actions/toolchain

- name: Docker pulls images.
uses: ScribeMD/[email protected]
with:
key: docker-${{ runner.os }}

- name: Build binary
uses: houseabsolute/actions-rust-cross@v0
with:
target: ${{ matrix.target }}
args: "--locked --release"
cross-version: v0.2.5

- name: Move to release dir
run: |
mkdir release
mv target/${{ matrix.target }}/release/spis release/spis-${{ matrix.target }}
- name: Build spis release
run: make release/spis-${{ matrix.target }}
env:
DATABASE_URL: sqlite:data/spis.db

- name: Store artifact
uses: actions/upload-artifact@v4
Expand Down
97 changes: 1 addition & 96 deletions .mise.toml
Original file line number Diff line number Diff line change
@@ -1,42 +1,3 @@
##### SETUP / CLEANUP #####

[tasks."setup:folders"]
run = ["mkdir -p data/media", "mkdir -p data/thumbnails"]
outputs = ["data/media", "data/thumbnails"]

[tasks."setup:db"]
run = ["sqlx database create", "sqlx migrate run --source migrations"]
sources = ["migrations/*.sql"]
outputs = ["data/spis.db"]
depends = ["setup:folders"]

[tasks."setup:toolchain"]
run = "rustup show"

[tasks.setup]
depends = ["setup:*"]

[tasks."clean:cargo"]
run = ["cargo clean", "rm -rf target"]

[tasks."clean:state"]
run = ["rm -f data/spis.db"]

[tasks."clean:thumbnails"]
run = ["rm -rf data/thumbnails"]

[tasks.clean]
depends = ["clean:*"]

##### DEV #####


[tasks."dev:clippy"]
run = """
watchexec --stop-timeout=0 -r -e rs,toml,html,css -- cargo clippy -F dev -- --no-deps -D warnings
"""
depends = ["setup:db"]

[tasks."dev:nginx"]
run = """
cat << EOF > /tmp/nginx.conf
Expand Down Expand Up @@ -65,63 +26,7 @@ nginx -c /tmp/nginx.conf > /dev/null
"""

[tasks."dev:spis"]
run = "watchexec --stop-timeout=0 -r -e rs,toml,html,css -- cargo run --color always -F dev"
depends = ["setup:db", "setup:folders"]

[tasks."dev:process"]
run = "cargo run -- -t"
depends = ["setup:db", "setup:folders"]
run = "make dev-spis"

[tasks."dev"]
depends = ["dev:nginx", "dev:spis"]

##### CI #####

[tasks."lint:clippy"]
run = """
cargo clippy -F dev -- --no-deps -D warnings
"""
depends = ["setup:db"]

[tasks."lint:fmt"]
run = "cargo fmt -- --check"

[tasks."lint:audit"]
run = "cargo audit"

[tasks.lint]
depends = ["lint:*"]

[tasks.test]
run = "cargo tarpaulin --ignore-tests --workspace --timeout 120 --skip-clean --out Xml"
depends = ["setup:db"]

[tasks.ci]
depends = ["lint", "test"]

##### RELEASE #####

[tasks.release]
depends = ["setup:db"]
sources = ["src/**", "templates/**", "assets/**", "migrations/**"]
outputs = ["release/spis-x86_64-unknown-linux-gnu"]
run = """
mkdir -p release
cargo build --locked --release
cp target/release/spis release/spis-x86_64-unknown-linux-gnu
"""

[tasks."docker:build"]
run = "docker build -t spis-local -f docker/Dockerfile ."
depends = ["release"]

[tasks."docker:run"]
raw = true
depends = ["docker:build"]
run = """
docker run -it --rm \
-p 8080:8080 \
-v ${PWD}/data/media:/var/lib/spis/media \
-e SPIS_PROCESSING_RUN_ON_START=true \
spis-local
"""
78 changes: 78 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
X86_64:=x86_64-unknown-linux-gnu
ARMV7:=armv7-unknown-linux-gnueabihf
AARCH64:=aarch64-unknown-linux-gnu

DATABASE:=$(shell echo ${DATABASE_URL} | awk -F':' '{print $$2}')
MEDIA_DIR:=data/media
THUMBNAIL_DIR:=data/thumbnails

${DATABASE}:
mkdir -p $(shell dirname ${DATABASE})
sqlx database create
sqlx migrate run --source migrations

${MEDIA_DIR}:
mkdir -p ${MEDIA_DIR}

${THUMBNAIL_DIR}:
mkdir -p ${THUMBNAIL_DIR}

_release: ${DATABASE}
cargo build --locked --release --target ${TARGET}
mkdir -p release
cp target/${TARGET}/release/spis release/spis-${TARGET}

release/spis-${X86_64}:
TARGET=${X86_64} $(MAKE) --no-print-directory _release

release/spis-${ARMV7}:
TARGET=${ARMV7} $(MAKE) --no-print-directory _release

release/spis-${AARCH64}:
TARGET=${AARCH64} $(MAKE) --no-print-directory _release

.PHONY: toolchain
toolchain:
rustup show

.PHONY: dev-clippy
dev-clippy: ${DATABASE}
watchexec --stop-timeout=0 -r -e rs,toml,html,css -- \
cargo clippy -F dev -- --no-deps -D warnings

.PHONY: dev-spis
dev-spis: ${DATABASE} ${MEDIA_DIR} ${THUMBNAIL_DIR}
watchexec --stop-timeout=0 -r -e rs,toml,html,css -- \
cargo run --color always -F dev

.PHONY: lint-fmt
lint-fmt:
cargo fmt -- --check

.PHONY: lint-clippy
lint-clippy: ${DATABASE}
cargo clippy -F dev -- --no-deps -D warnings

.PHONY: lint
lint: lint-fmt lint-clippy

.PHONY: test
test: ${DATABASE} ${MEDIA_DIR} ${THUMBNAIL_DIR}
cargo tarpaulin --ignore-tests --color always --timeout 120 --skip-clean --out Xml

.PHONY: docker-build
docker-build: release/spis-${X86_64}
docker build -t spis-local -f docker/Dockerfile .

.PHONY: docker-run
docker-run: docker-build
docker run -it --rm \
-p 8080:8080 \
-v ${PWD}/data/media:/var/lib/spis/media \
-e SPIS_PROCESSING_RUN_ON_START=true \
spis-local

.PHONY: clean
clean:
rm -rf target release data/spis.db data/thumbnails
cargo clean
6 changes: 5 additions & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
[toolchain]
channel = "1.80.0"
components = ["rustfmt", "rust-src", "rust-std", "clippy"]
targets = ["x86_64-unknown-linux-gnu"]
targets = [
"x86_64-unknown-linux-gnu",
"armv7-unknown-linux-gnueabihf",
"aarch64-unknown-linux-gnu",
]
profile = "minimal"

0 comments on commit 6c628d0

Please sign in to comment.