From 7d2771acf4dcfb2b351e2fe0b105bce531fe6324 Mon Sep 17 00:00:00 2001 From: nohoster Date: Sat, 24 Feb 2024 16:16:10 -0600 Subject: [PATCH] Optimize container pipeline --- .github/workflows/build-artifacts.yaml | 68 ++++++++++++++++++++++++++ .github/workflows/main-docker.yml | 19 +++---- Dockerfile | 28 ++--------- 3 files changed, 83 insertions(+), 32 deletions(-) create mode 100644 .github/workflows/build-artifacts.yaml diff --git a/.github/workflows/build-artifacts.yaml b/.github/workflows/build-artifacts.yaml new file mode 100644 index 00000000..7b671a63 --- /dev/null +++ b/.github/workflows/build-artifacts.yaml @@ -0,0 +1,68 @@ +name: Release Build + +on: + push: + paths-ignore: + - "*.md" + - "compose.*" + branches: + - "main" + +env: + CARGO_TERM_COLOR: always + + CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER: aarch64-linux-gnu-gcc + CC_aarch64_unknown_linux_musl: aarch64-linux-gnu-gcc + CARGO_TARGET_ARMV7_UNKNOWN_LINUX_MUSLEABIHF_LINKER: arm-linux-gnueabihf-gcc + CC_armv7_unknown_linux_musleabihf: arm-linux-gnueabihf-gcc + +jobs: + build: + name: Rust project - latest + runs-on: ubuntu-latest + strategy: + matrix: + target: + - x86_64-unknown-linux-musl + - aarch64-unknown-linux-musl + - armv7-unknown-linux-musleabihf + steps: + - uses: actions/checkout@v4 + + - uses: actions-rust-lang/setup-rust-toolchain@v1 + with: + target: ${{ matrix.target }} + + - if: matrix.target == 'x86_64-unknown-linux-musl' + run: | + sudo apt-get update + sudo apt-get install -y --no-install-recommends musl-tools + + - if: matrix.target == 'armv7-unknown-linux-musleabihf' + run: | + sudo apt update + sudo apt install -y gcc-arm-linux-gnueabihf musl-tools + + - if: matrix.target == 'aarch64-unknown-linux-musl' + run: | + sudo apt update + sudo apt install -y gcc-aarch64-linux-gnu musl-tools + + - name: Date + id: date + run: echo "DATE=$(date +%Y%m%d-%H%M)" >> "$GITHUB_OUTPUT" + + - name: Build + run: cargo build --release --target ${{ matrix.target }} + + - name: Package release + run: tar czf redlib-${{ matrix.target }}.tar.gz -C target/${{ matrix.target }}/release/ redlib + + - name: Upload release + uses: softprops/action-gh-release@v1 + with: + tag_name: ${{ steps.date.outputs.DATE }} + files: | + redlib-${{ matrix.target }}.tar.gz + + diff --git a/.github/workflows/main-docker.yml b/.github/workflows/main-docker.yml index 492cddb9..e51dc4b6 100644 --- a/.github/workflows/main-docker.yml +++ b/.github/workflows/main-docker.yml @@ -1,23 +1,23 @@ name: Container build on: - push: - paths-ignore: - - "**.md" - branches: - - "main" + workflow_run: + workflows: ["Release Build"] + types: + - completed env: REGISTRY_IMAGE: ghcr.io/nohoster/redlib + jobs: build: runs-on: ubuntu-latest strategy: fail-fast: false matrix: - platform: - - linux/amd64 - - linux/arm64 - - linux/arm/v7 + include: + - { platform: linux/amd64, target: x86_64-unknown-linux-musl} + - { platform: linux/arm64, target: aarch64-unknown-linux-musl} + - { platform: linux/arm/v7, target: armv7-unknown-linux-musleabihf} steps: - name: Checkout @@ -54,6 +54,7 @@ jobs: labels: ${{ steps.meta.outputs.labels }} outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true file: Dockerfile + build-args: TARGET=${{ matrix.target }} - name: Export digest run: | diff --git a/Dockerfile b/Dockerfile index d0e7246e..034f5548 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,30 +1,12 @@ -#################################################################################################### -## Builder -#################################################################################################### - -FROM alpine:3.19 AS builder - -RUN apk add --no-cache cargo git g++ - -WORKDIR /redlib - -COPY . . - -RUN cargo build --config net.git-fetch-with-cli=true --release - -#################################################################################################### -## Final image -#################################################################################################### - FROM alpine:3.19 -COPY --from=builder /usr/share/ca-certificates /usr/share/ca-certificates -COPY --from=builder /etc/ssl/certs /etc/ssl/certs +ARG TARGET + +RUN apk add --no-cache curl -# Copy our build -COPY --from=builder /redlib/target/release/redlib /usr/local/bin/redlib +RUN curl -L https://github.com/nohoster/redlib/releases/latest/download/redlib-${TARGET}.tar.gz | \ + tar xz -C /usr/local/bin/ -# Use an unprivileged user. RUN adduser --home /nonexistent --no-create-home --disabled-password redlib USER redlib