From 18be7c5a504530d99465c37880dffed019199282 Mon Sep 17 00:00:00 2001 From: Yuki WADA Date: Wed, 12 Apr 2023 01:59:38 +0900 Subject: [PATCH] feat: use cross to build --- ...d-and-release-application.yaml => ci.yaml} | 52 +--------- .github/workflows/image-release.yaml | 97 +++++++++++++++++++ Dockerfile | 36 ------- docker/Dockerfile | 12 +++ docker/copy-platform-artifact.sh | 11 +++ docker/seichi-portal-backend.sh | 2 + rust-toolchain.toml | 2 +- server/.cargo/config.toml | 2 - 8 files changed, 125 insertions(+), 89 deletions(-) rename .github/workflows/{build-and-release-application.yaml => ci.yaml} (50%) create mode 100644 .github/workflows/image-release.yaml delete mode 100644 Dockerfile create mode 100644 docker/Dockerfile create mode 100644 docker/copy-platform-artifact.sh create mode 100644 docker/seichi-portal-backend.sh diff --git a/.github/workflows/build-and-release-application.yaml b/.github/workflows/ci.yaml similarity index 50% rename from .github/workflows/build-and-release-application.yaml rename to .github/workflows/ci.yaml index d74dd8d5..9bddf6fe 100644 --- a/.github/workflows/build-and-release-application.yaml +++ b/.github/workflows/ci.yaml @@ -1,4 +1,4 @@ -name: Build and release application +name: Checking on: push: @@ -19,7 +19,7 @@ jobs: - name: Setup Rust toolchain uses: dtolnay/rust-toolchain@stable with: - toolchain: 1.68.0 + toolchain: 1.68.2 target: x86_64-unknown-linux-musl components: clippy, rustfmt @@ -39,9 +39,6 @@ jobs: run: cargo fmt --all -- --check working-directory: ./server - - name: Install Deps - run: sudo apt install -y musl-tools build-essential - - name: Cargo build run: cargo build working-directory: ./server @@ -55,48 +52,3 @@ jobs: - name: Cargo test run: cargo test --all-features working-directory: ./server - - build-image: - name: Build server docker image (and publish on main) - needs: [ lint-and-test ] - runs-on: ubuntu-latest - outputs: - image_tag: ${{ steps.meta.outputs.tags }} - concurrency: - group: ${{ github.ref }}-docker-build - cancel-in-progress: true - steps: - - name: checkout - uses: actions/checkout@v3 - - - name: Set up Docker Buildx - id: buildx - uses: docker/setup-buildx-action@v2 - - - name: Login to GitHub Container Registry - uses: docker/login-action@v2 - with: - registry: ghcr.io - username: ${{ github.repository_owner }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: Docker meta - id: meta - uses: docker/metadata-action@v4 - with: - images: ${{ env.image_name }} - tags: | - type=sha,prefix=sha-,suffix=,format=short - - name: Build (and push if on main) - id: docker_build - uses: docker/build-push-action@v4 - with: - context: ./server - file: ./Dockerfile - builder: ${{ steps.buildx.outputs.name }} - push: ${{ github.ref == 'refs/heads/main' }} - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} - cache-from: type=gha - # すべてのビルドステージのすべてのレイヤーをキャッシュして欲しいのでmode=max - cache-to: type=gha,mode=max diff --git a/.github/workflows/image-release.yaml b/.github/workflows/image-release.yaml new file mode 100644 index 00000000..dc5200ea --- /dev/null +++ b/.github/workflows/image-release.yaml @@ -0,0 +1,97 @@ +name: Release docker images + +on: + push: + branches: + - feature/cross + +jobs: + build: + name: Build for ${{ matrix.docker-platform }} + strategy: + fail-fast: false + matrix: + include: + - + docker-platform: linux/amd64 + cross-target: x86_64-unknown-linux-gnu + - + docker-platform: linux/arm64/v8 + cross-target: aarch64-unknown-linux-gnu + runs-on: ubuntu-latest + steps: + - + name: Checkout + uses: actions/checkout@master + - + name: Setup rust toolchain + uses: actions-rs/toolchain@v1 + with: + toolchain: 1.68.2 + target: ${{ matrix.cross-target }} + override: true + - + name: Build binary using cross + uses: actions-rs/cargo@v1 + with: + use-cross: true + command: build + args: --manifest-path server/Cargo.toml --target ${{ matrix.cross-target }} --release + - + name: Upload artifact to collect in next job + uses: actions/upload-artifact@v2 + with: + name: ${{ matrix.cross-target }} + path: server/target/${{ matrix.cross-target }}/release/entrypoint + + release-all: + name: Push docker image for all platforms + runs-on: ubuntu-latest + needs: build + env: + image_name: seichi-portal-backend + steps: + - + name: Checkout + uses: actions/checkout@master + - + uses: actions/download-artifact@v3 + with: + name: x86_64-unknown-linux-gnu + path: docker/artifacts/x86_64-unknown-linux-gnu + - + uses: actions/download-artifact@v3 + with: + name: aarch64-unknown-linux-gnu + path: docker/artifacts/aarch64-unknown-linux-gnu + - + name: Set up QEMU + uses: docker/setup-qemu-action@v1 + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - + name: Login to GitHub Container Registry + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + - + id: prepare_image_id + name: Prepare image id's components + run: | + image_id=ghcr.io/${{ github.repository_owner }}/${{ env.image_name }} + echo "lowercase_id=$(echo $image_id | tr '[A-Z]' '[a-z]')" >> $GITHUB_OUTPUT + echo "short-ref=$(git rev-parse --short "$GITHUB_SHA")" >> $GITHUB_OUTPUT + echo "branch=$(echo ${GITHUB_REF#refs/heads/} | tr / -)" >> $GITHUB_OUTPUT + - + name: Build and push + uses: docker/build-push-action@v4 + with: + context: docker + platforms: linux/amd64,linux/arm64/v8 + push: true + tags: | + ${{ steps.prepare_image_id.outputs.lowercase_id }}:latest + ${{ steps.prepare_image_id.outputs.lowercase_id }}:${{ steps.prepare_image_id.outputs.branch }}-${{ steps.prepare_image_id.outputs.short-ref }} diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index b4d7de7e..00000000 --- a/Dockerfile +++ /dev/null @@ -1,36 +0,0 @@ -# syntax=docker/dockerfile:1.4 -FROM rust:1.68-slim AS build-env - -WORKDIR /app - -COPY --link . . - -RUN --mount=target=. \ - --mount=type=cache,target=/usr/local/cargo/git/db \ - --mount=type=cache,target=/usr/local/cargo/registry/cache \ - --mount=type=cache,target=/usr/local/cargo/registry/index \ - cargo fetch --manifest-path Cargo.toml - -RUN rustup target add x86_64-unknown-linux-musl && \ - apt update && apt-get install -y musl-tools build-essential - -# TODO: cargo build の --out-dir オプションが stable に落ちてきたらコメントの内容に置き換える -# RUN --mount=type=cache,target=/usr/local/cargo/git/db \ -# --mount=type=cache,target=/usr/local/cargo/registry/cache \ -# --mount=type=cache,target=/usr/local/cargo/registry/index \ -# --mount=type=cache,target=/app/server/target \ -# cargo build --release --out-dir . - -RUN --mount=type=cache,target=/usr/local/cargo/git/db \ - --mount=type=cache,target=/usr/local/cargo/registry/cache \ - --mount=type=cache,target=/usr/local/cargo/registry/index \ - --mount=type=cache,target=target \ - cargo build --release && \ - cp target/x86_64-unknown-linux-musl/release/entrypoint /seichi-portal-backend - -FROM gcr.io/distroless/cc -LABEL org.opencontainers.image.source=https://github.com/GiganticMinecraft/seichi-portal-backend -# TODO: cargo build の --out-dir オプションが stable に落ちてきたらコメントの内容に置き換える -# COPY --from=build-env --link /app/server/entrypoint /seichi-portal-backend -COPY --from=build-env --link /seichi-portal-backend /seichi-portal-backend -CMD ["/seichi-portal-backend"] diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 00000000..d74d3008 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,12 @@ +# syntax=docker/dockerfile:1.4 +FROM ubuntu:latest +LABEL org.opencontainers.image.source=https://github.com/GiganticMinecraft/seichi-portal-backend + +COPY --link / /work/ +RUN ls +COPY /seichi-portal-backend.sh /etc/service/seichi-portal-backend/run + +RUN chmod +x /work/copy-platform-artifact.sh +RUN chmod +x /etc/service/seichi-portal-backend/run +RUN /work/copy-platform-artifact.sh +RUN chmod +x /usr/local/bin/seichi-portal-backend diff --git a/docker/copy-platform-artifact.sh b/docker/copy-platform-artifact.sh new file mode 100644 index 00000000..c72bb114 --- /dev/null +++ b/docker/copy-platform-artifact.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +case $(uname -m) in + # linux/amd64 + "x86_64" ) cp /work/artifacts/x86_64-unknown-linux-gnu/entrypoint /usr/local/bin/seichi-portal-backend ;; + # linux/arm/v7 + "armv7l" ) cp /work/artifacts/armv7-unknown-linux-gnueabihf/entrypoint /usr/local/bin/seichi-portal-backend ;; + # linux/arm64/v8 + "aarch64" ) cp /work/artifacts/aarch64-unknown-linux-gnu/entrypoint /usr/local/bin/seichi-portal-backend ;; + * ) exit 1 # we do not expect other platform +esac diff --git a/docker/seichi-portal-backend.sh b/docker/seichi-portal-backend.sh new file mode 100644 index 00000000..1eaedddd --- /dev/null +++ b/docker/seichi-portal-backend.sh @@ -0,0 +1,2 @@ +#!/bin/sh +exec /usr/local/bin/seichi-poartal-backend diff --git a/rust-toolchain.toml b/rust-toolchain.toml index a0b0ebcc..b4e16de6 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,4 +1,4 @@ [toolchain] -channel = "1.68.0" +channel = "1.68.2" components = [ "rustfmt", "clippy", "cargo" ] profile = "minimal" diff --git a/server/.cargo/config.toml b/server/.cargo/config.toml index d67b66e2..e69de29b 100644 --- a/server/.cargo/config.toml +++ b/server/.cargo/config.toml @@ -1,2 +0,0 @@ -[build] -target = "x86_64-unknown-linux-musl"