name: CI
on:
push:
branches: [ main, master ]
tags:
- 'v*.*.*'
pull_request:
branches: [ main, master ]
env:
CARGO_TERM_COLOR: always
RUST_BACKTRACE: 1
FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true
jobs:
check:
name: Code Quality Checks
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- name: Cache cargo
uses: actions/cache@v5
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml') }}
- name: Check formatting
run: cargo fmt -- --check
- name: Run clippy
run: cargo clippy --all-targets -- -D warnings
- name: Check with all features
run: cargo clippy --all-features --all-targets -- -D warnings
security:
name: Security Audit
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Run cargo audit
run: |
cargo install cargo-audit
cargo audit
test:
name: Test
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
rust: [stable, beta, nightly]
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ matrix.rust }}
- name: Cache cargo
uses: actions/cache@v5
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-${{ matrix.rust }}-cargo-${{ hashFiles('**/Cargo.toml') }}
- name: Build
run: cargo build --verbose
- name: Test
run: cargo test --verbose
- name: Test with all features
run: cargo test --all-features --verbose
build-windows:
name: Build Windows
runs-on: windows-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Build
run: |
cargo build --release --all-features
- name: Verify binary
run: |
if (Test-Path target\release\crates-docs.exe) {
Write-Host "Binary compiled successfully for Windows"
} else {
Write-Error "Binary not found for Windows"
exit 1
}
test-redis:
name: Test with Redis
runs-on: ubuntu-latest
services:
redis:
image: redis:7-alpine
ports:
- 6379:6379
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Cache cargo
uses: actions/cache@v5
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-redis-cargo-${{ hashFiles('**/Cargo.toml') }}
- name: Build with Redis feature
run: cargo build --features cache-redis --verbose
- name: Test with Redis feature
run: cargo test --features cache-redis --verbose
docs:
name: Documentation
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Cache cargo
uses: actions/cache@v5
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-docs-cargo-${{ hashFiles('**/Cargo.toml') }}
- name: Build documentation
run: cargo doc --no-deps --all-features
- name: Check documentation links
run: cargo doc --no-deps --all-features --document-private-items
coverage:
name: Code Coverage
runs-on: ubuntu-latest
services:
redis:
image: redis:7-alpine
ports:
- 6379:6379
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Install tarpaulin
run: cargo install cargo-tarpaulin
- name: Generate coverage
run: cargo tarpaulin --out Xml --all-features --verbose
- name: Install Codecov CLI
run: python -m pip install --user codecov-cli
- name: Upload to codecov.io
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
run: |
export PATH="$(python -m site --user-base)/bin:$PATH"
codecovcli upload-process \
--disable-search \
--file ./cobertura.xml \
--flag unittests \
--name codecov-umbrella \
--sha "$GITHUB_SHA"
continue-on-error: true
docker:
name: Docker Build
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Build Docker image
run: docker build -t crates-docs:test .
- name: Test Docker image
run: |
docker run -d --name test-container -p 8080:8080 crates-docs:test
trap 'docker logs test-container || true; docker stop test-container || true; docker rm test-container || true' EXIT
for i in $(seq 1 30); do
if curl -fsSI http://127.0.0.1:8080/mcp > /tmp/docker-mcp-headers.txt; then
break
fi
sleep 2
done
curl -fsSI http://127.0.0.1:8080/mcp | tee /tmp/docker-mcp-headers.txt
grep -Eq '^HTTP/[0-9.]+ (200|204|400|405|406)' /tmp/docker-mcp-headers.txt
docker logs test-container
publish-check:
name: Publish Check
runs-on: ubuntu-latest
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master')
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Cache cargo
uses: actions/cache@v5
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-publish-cargo-${{ hashFiles('**/Cargo.toml') }}
- name: Check publish (dry-run)
run: cargo publish --dry-run
docker-credential-check:
name: Docker Credential Check
runs-on: ubuntu-latest
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master')
steps:
- name: Log in to Docker Hub
uses: docker/login-action@v4
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Verify Docker credentials
run: |
# 验证登录成功
docker info | grep -q "Username" || {
echo "Error: Docker Hub login failed"
exit 1
}
echo "Docker Hub credentials are valid!"
publish:
name: Publish to crates.io
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags/v')
needs: [check, security, test, build-windows, test-redis, docs, docker]
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Cache cargo
uses: actions/cache@v5
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-publish-cargo-${{ hashFiles('**/Cargo.toml') }}
- name: Verify version matches tag
run: |
CARGO_VERSION=$(grep '^version = ' Cargo.toml | head -n 1 | sed 's/version = "\(.*\)"/\1/')
TAG_VERSION=${GITHUB_REF#refs/tags/v}
echo "Cargo.toml version: $CARGO_VERSION"
echo "Tag version: $TAG_VERSION"
if [ "$CARGO_VERSION" != "$TAG_VERSION" ]; then
echo "Error: Version mismatch!"
exit 1
fi
- name: Publish to crates.io
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: |
if [ -z "$CARGO_REGISTRY_TOKEN" ]; then
echo "Error: CARGO_REGISTRY_TOKEN is not set"
exit 1
fi
cargo publish
build-docker-binaries:
name: Build Docker Binaries
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags/v')
needs: [check, docker]
strategy:
fail-fast: false
matrix:
include:
- target: x86_64-unknown-linux-musl
arch: amd64
use_cross: false
- target: aarch64-unknown-linux-musl
arch: arm64
use_cross: true
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}
- name: Cache cargo
uses: actions/cache@v5
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-${{ matrix.target }}-docker-cargo-${{ hashFiles('**/Cargo.toml') }}
- name: Install musl tools
run: |
sudo apt-get update
sudo apt-get install -y musl-tools
- name: Install cross
if: matrix.use_cross
run: cargo install cross --git https://github.com/cross-rs/cross
- name: Build binary (native)
if: ${{ !matrix.use_cross }}
run: cargo build --release --target ${{ matrix.target }}
- name: Build binary (cross)
if: matrix.use_cross
run: cross build --release --target ${{ matrix.target }}
- name: Prepare binary
run: |
BINARY_NAME="crates-docs-${{ matrix.arch }}"
cp target/${{ matrix.target }}/release/crates-docs "$BINARY_NAME"
chmod +x "$BINARY_NAME"
echo "binary_name=$BINARY_NAME" >> $GITHUB_OUTPUT
id: prepare
- name: Upload binary artifact
uses: actions/upload-artifact@v4
with:
name: binary-${{ matrix.arch }}
path: ${{ steps.prepare.outputs.binary_name }}
retention-days: 1
publish-docker:
name: Publish Docker Images
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags/v')
needs: [build-docker-binaries]
permissions:
contents: read
packages: write
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Log in to Docker Hub
uses: docker/login-action@v4
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Extract version
id: version
run: |
VERSION=${GITHUB_REF#refs/tags/v}
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "Tag version: $VERSION"
- name: Download amd64 binary
uses: actions/download-artifact@v4
with:
name: binary-amd64
path: ./binaries/amd64
- name: Download arm64 binary
uses: actions/download-artifact@v4
with:
name: binary-arm64
path: ./binaries/arm64
- name: Verify and prepare binaries
run: |
echo "Downloaded binaries structure:"
find ./binaries -type f -ls
# Move binaries to expected locations for Docker
# download-artifact@v4 creates subdirectories with artifact names
mv ./binaries/amd64/crates-docs-amd64 ./binaries/crates-docs-amd64 || true
mv ./binaries/arm64/crates-docs-arm64 ./binaries/crates-docs-arm64 || true
echo "Final binaries:"
ls -la ./binaries/
# Verify files exist and are executable
test -f ./binaries/crates-docs-amd64 || { echo "ERROR: amd64 binary not found"; exit 1; }
test -f ./binaries/crates-docs-arm64 || { echo "ERROR: arm64 binary not found"; exit 1; }
chmod +x ./binaries/crates-docs-amd64 ./binaries/crates-docs-arm64
- name: Build and push Docker image
uses: docker/build-push-action@v7
with:
context: .
file: ./Dockerfile.release
push: true
platforms: linux/amd64,linux/arm64
tags: |
${{ secrets.DOCKER_USERNAME }}/crates-docs:latest
${{ secrets.DOCKER_USERNAME }}/crates-docs:${{ steps.version.outputs.version }}
cache-from: type=gha
cache-to: type=gha,mode=max
release:
name: Build and Release Binaries
runs-on: ${{ matrix.os }}
if: startsWith(github.ref, 'refs/tags/v')
needs: [check, security, test, build-windows, test-redis, docs, docker]
permissions:
contents: write
strategy:
fail-fast: false
matrix:
include:
- target: x86_64-unknown-linux-gnu
os: ubuntu-latest
cross: false
- target: x86_64-unknown-linux-musl
os: ubuntu-latest
cross: false
- target: aarch64-unknown-linux-gnu
os: ubuntu-latest
cross: true
- target: aarch64-unknown-linux-musl
os: ubuntu-latest
cross: true
- target: x86_64-apple-darwin
os: macos-latest
cross: false
- target: aarch64-apple-darwin
os: macos-latest
cross: false
- target: x86_64-pc-windows-msvc
os: windows-latest
cross: false
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}
- name: Cache cargo
uses: actions/cache@v5
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-${{ matrix.target }}-cargo-${{ hashFiles('**/Cargo.toml') }}
- name: Install cross
if: matrix.cross
run: cargo install cross --git https://github.com/cross-rs/cross
- name: Install musl tools (Linux musl)
if: endsWith(matrix.target, '-musl')
run: |
sudo apt-get update
sudo apt-get install -y musl-tools
- name: Build binary (native)
if: ${{ !matrix.cross }}
run: cargo build --release --target ${{ matrix.target }}
- name: Build binary (cross)
if: matrix.cross
run: cross build --release --target ${{ matrix.target }}
- name: Extract version
id: version
shell: bash
run: |
VERSION=${GITHUB_REF#refs/tags/v}
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "Tag version: $VERSION"
- name: Prepare binary (Unix)
if: runner.os != 'Windows'
run: |
BINARY_NAME="crates-docs-v${{ steps.version.outputs.version }}-${{ matrix.target }}"
mv target/${{ matrix.target }}/release/crates-docs "$BINARY_NAME"
chmod +x "$BINARY_NAME"
echo "binary_name=$BINARY_NAME" >> $GITHUB_OUTPUT
- name: Prepare binary (Windows)
if: runner.os == 'Windows'
shell: bash
run: |
BINARY_NAME="crates-docs-v${{ steps.version.outputs.version }}-${{ matrix.target }}.exe"
mv target/${{ matrix.target }}/release/crates-docs.exe "$BINARY_NAME"
echo "binary_name=$BINARY_NAME" >> $GITHUB_OUTPUT
- name: Upload release asset
uses: softprops/action-gh-release@v2
with:
files: crates-docs-v${{ steps.version.outputs.version }}-${{ matrix.target }}*
fail_on_unmatched_files: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}