Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dockerでビルドしてghcr.ioに上げるactions追加 #39

Merged
merged 4 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
name: Docker

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

on:
schedule:
- cron: '00 15 * * *'
push:
branches: [ "main" ]
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]
pull_request:
branches: [ "main" ]

env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
REGISTRY_IMAGE: ghcr.io/${{ github.repository }}


jobs:
build:

runs-on: ubuntu-latest
permissions:
contents: read
packages: write
# This is used to complete the identity challenge
# with sigstore/fulcio when running outside of PRs.
id-token: write
strategy:
fail-fast: false
matrix:
platform:
- linux/386
- linux/amd64
- linux/arm64
- linux/arm/v6
- linux/arm/v7
steps:
- name: Prepare
run: |
platform=${{ matrix.platform }}
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY_IMAGE }}

- name: CacheMount
uses: actions/cache@v4
id: cache
with:
path: |
cargo-home
app-target
key: cache-${{ matrix.platform }}-${{ hashFiles('**/Cargo.toml') }}

- name: inject cache into docker
uses: reproducible-containers/[email protected]
with:
cache-map: |
{
"cargo-home": "/var/cache/cargo",
"app-target": "/app/target"
}
skip-extraction: ${{ steps.cache.outputs.cache-hit }}
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build
uses: docker/build-push-action@v5
with:
context: .
platforms: ${{ matrix.platform }}
labels: ${{ steps.meta.outputs.labels }}
outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=${{ github.event_name != 'pull_request' }}
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Export digest
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: digests-${{ env.PLATFORM_PAIR }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
merge:
runs-on: ubuntu-latest
if: ${{ github.event_name != 'pull_request' }}
needs:
- build
steps:
- name: Download digests
uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: digests-*
merge-multiple: true
# Install the cosign tool except on PR
# https://github.com/sigstore/cosign-installer
- name: Install cosign
if: github.event_name != 'pull_request'
uses: sigstore/cosign-installer@v3
with:
cosign-release: 'v2.1.1'
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY_IMAGE }}
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Create manifest list and push
working-directory: /tmp/digests
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
- name: Inspect image
run: |
docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }}
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ avif-decoder_dep = { path="./avif-decoder_dep" ,optional = true }
[profile.release]
strip = true
opt-level = 3
lto = true
lto = "thin"
panic = "abort"

[profile.dev]
Expand Down
30 changes: 22 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
FROM rust:alpine
RUN apk add --no-cache musl-dev curl meson ninja pkgconfig git
RUN sh -c "if [ $(uname -m) = x86_64 ]; then apk add --no-cache nasm;fi"
FROM alpine:latest
RUN apk add --no-cache clang musl-dev meson ninja pkgconfig nasm git
RUN git clone --branch 1.3.0 --depth 1 https://code.videolan.org/videolan/dav1d.git /dav1d_src
WORKDIR /dav1d_src
RUN meson build -Dprefix=/dav1d -Denable_tools=false -Denable_examples=false -Ddefault_library=static --buildtype release
RUN ninja -C build
RUN ninja -C build install

FROM --platform=$BUILDPLATFORM rust:alpine
ARG BUILDARCH
ARG TARGETARCH
ARG TARGETVARIANT
RUN apk add --no-cache clang musl-dev curl pkgconfig nasm mold
ENV PKG_CONFIG_PATH=/dav1d/lib/pkgconfig
ENV LD_LIBRARY_PATH=/dav1d/lib
ENV CARGO_HOME=/var/cache/cargo
RUN mkdir /app
ENV SYSTEM_DEPS_BUILD_INTERNAL=always
ENV RUSTFLAGS="-C link-args=-Wl,-lc"
ENV SYSTEM_DEPS_LINK=static
COPY crossfiles /app/crossfiles
RUN sh /app/crossfiles/deps.sh
COPY --from=0 /dav1d /dav1d
WORKDIR /app
COPY avif-decoder_dep ./avif-decoder_dep
COPY src ./src
COPY Cargo.toml ./Cargo.toml
RUN --mount=type=cache,target=/var/cache/cargo cargo build --release
COPY asset ./asset
RUN --mount=type=cache,target=/var/cache/cargo --mount=type=cache,target=/app/target sh /app/crossfiles/build.sh

FROM alpine:latest
ARG UID="852"
Expand All @@ -18,6 +32,6 @@ RUN addgroup -g "${GID}" proxy && adduser -u "${UID}" -G proxy -D -h /media-prox
WORKDIR /media-proxy-rs
USER proxy
COPY asset ./asset
COPY --from=0 /app/target/release/media-proxy-rs ./media-proxy-rs
COPY --from=1 /app/media-proxy-rs ./media-proxy-rs
EXPOSE 12766
CMD ["./media-proxy-rs"]
13 changes: 13 additions & 0 deletions crossfiles/386.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export MUSL_NAME="i686-linux-musl-cross"
export PATH="/${MUSL_NAME}/bin:${PATH}"
export CC=i686-linux-musl-gcc
export CXX=i686-linux-musl-g++
export AR=i686-linux-musl-ar
#現時点ではringがsse2を必須としている
#https://github.com/briansmith/ring/blob/main/src/cpu/intel.rs#L23
#https://github.com/briansmith/ring/issues/1793#issuecomment-1793243725
#https://github.com/briansmith/ring/issues/1832
#https://github.com/briansmith/ring/issues/1833.
export RUSTFLAGS="-C target-feature=+sse -C target-feature=+sse2 -C linker=${CC}"
export PKG_CONFIG_SYSROOT_DIR="/${MUSL_NAME}/"
export RUST_TARGET="i686-unknown-linux-musl"
8 changes: 8 additions & 0 deletions crossfiles/amd64.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export MUSL_NAME="x86_64-linux-musl-cross"
export PATH="/${MUSL_NAME}/bin:${PATH}"
export CC=x86_64-linux-musl-gcc
export CXX=x86_64-linux-musl-g++
export AR=x86_64-linux-musl-ar
export RUSTFLAGS="-C target-feature=+avx -C linker=${CC}"
export PKG_CONFIG_SYSROOT_DIR="/${MUSL_NAME}/"
export RUST_TARGET="x86_64-unknown-linux-musl"
8 changes: 8 additions & 0 deletions crossfiles/arm/v6.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export MUSL_NAME="armv6-linux-musleabihf-cross"
export PATH="/${MUSL_NAME}/bin:${PATH}"
export CC=armv6-linux-musleabihf-gcc
export CXX=armv6-linux-musleabihf-g++
export AR=armv6-linux-musleabihf-ar
export RUSTFLAGS="-C link-args=-Wl,-lc -C linker=${CC}"
export PKG_CONFIG_SYSROOT_DIR="/${MUSL_NAME}/"
export RUST_TARGET="arm-unknown-linux-musleabihf"
8 changes: 8 additions & 0 deletions crossfiles/arm/v7.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export MUSL_NAME="armv7l-linux-musleabihf-cross"
export PATH="/${MUSL_NAME}/bin:${PATH}"
export CC=armv7l-linux-musleabihf-gcc
export CXX=armv7l-linux-musleabihf-g++
export AR=armv7l-linux-musleabihf-ar
export RUSTFLAGS="-C link-args=-Wl,-lc -C linker=${CC}"
export PKG_CONFIG_SYSROOT_DIR="/${MUSL_NAME}/"
export RUST_TARGET="armv7-unknown-linux-musleabihf"
8 changes: 8 additions & 0 deletions crossfiles/arm64.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export MUSL_NAME="aarch64-linux-musl-cross"
export PATH="/${MUSL_NAME}/bin:${PATH}"
export CC=aarch64-linux-musl-gcc
export CXX=aarch64-linux-musl-g++
export AR=aarch64-linux-musl-ar
export RUSTFLAGS="-C linker=${CC}"
export PKG_CONFIG_SYSROOT_DIR="/${MUSL_NAME}/"
export RUST_TARGET="aarch64-unknown-linux-musl"
9 changes: 9 additions & 0 deletions crossfiles/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
set -eu
if [ -f "/app/crossfiles/${TARGETARCH}.sh" ]; then
source /app/crossfiles/${TARGETARCH}.sh
else
source /app/crossfiles/${TARGETARCH}/${TARGETVARIANT}.sh
fi
cp -r /dav1d/lib /${MUSL_NAME}/dav1d/lib
cargo build --release --target ${RUST_TARGET}
cp /app/target/${RUST_TARGET}/release/media-proxy-rs /app/media-proxy-rs
9 changes: 9 additions & 0 deletions crossfiles/deps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
set -eu
if [ -f "/app/crossfiles/${TARGETARCH}.sh" ]; then
source /app/crossfiles/${TARGETARCH}.sh
else
source /app/crossfiles/${TARGETARCH}/${TARGETVARIANT}.sh
fi
rustup target add ${RUST_TARGET}
curl -sSL https://musl.cc/${MUSL_NAME}.tgz | tar -zxf - -C /
mkdir -p /${MUSL_NAME}/dav1d/
Loading