From e10e4bc71cd0a57ab641cf3a75447b3bf5b537f2 Mon Sep 17 00:00:00 2001 From: zeke Date: Tue, 26 Nov 2024 13:35:20 +0800 Subject: [PATCH] add: github workflow --- .github/workflows/build-rs-capi.yml | 140 ++++++++++++++++++++++++++++ .gitignore | 3 +- rs-capi/Cargo.lock | 2 +- rs-capi/Cargo.toml | 2 +- rs-capi/Dockerfile | 26 ++++++ rs-capi/src/main.rs | 3 +- 6 files changed, 172 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/build-rs-capi.yml create mode 100644 rs-capi/Dockerfile diff --git a/.github/workflows/build-rs-capi.yml b/.github/workflows/build-rs-capi.yml new file mode 100644 index 0000000..124dbb2 --- /dev/null +++ b/.github/workflows/build-rs-capi.yml @@ -0,0 +1,140 @@ +name: Build and Push RS-CAPI Docker Images + +on: + push: + branches: [ "main" ] + tags: [ "v*" ] + paths: + - 'rs-capi/**' + pull_request: + branches: [ "main" ] + workflow_dispatch: + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }}/rs-capi + +jobs: + build-and-push: + strategy: + matrix: + include: + - runner: ubuntu-latest + platforms: linux/amd64,linux/arm64 + - runner: macos-latest + platforms: darwin/amd64 + - runner: macos-14 + platforms: darwin/arm64 + + runs-on: ${{ matrix.runner }} + permissions: + contents: write # 需要写入权限来创建 release + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 # 获取完整的 git 历史用于生成 changelog + + - name: Set up Docker + if: runner.os == 'macOS' + run: | + brew install docker docker-buildx + colima start + + - name: Set up QEMU + if: runner.os == 'Linux' + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + if: runner.os == 'Linux' + uses: docker/setup-buildx-action@v3 + + - name: Log in to the Container registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=raw,value=latest,enable={{is_default_branch}} + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=sha + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: ./rs-capi + file: ./rs-capi/Dockerfile + push: ${{ github.event_name != 'pull_request' }} + platforms: ${{ matrix.platforms }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + + create-release: + needs: build-and-push + if: startsWith(github.ref, 'refs/tags/v') + runs-on: ubuntu-latest + permissions: + contents: write + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Generate Changelog + id: changelog + uses: mikepenz/release-changelog-builder-action@v4 + with: + configuration: | + { + "categories": [ + { + "title": "## 🚀 Features", + "labels": ["feature"] + }, + { + "title": "## 🐛 Fixes", + "labels": ["fix"] + }, + { + "title": "## 📦 Dependencies", + "labels": ["dependencies"] + } + ] + } + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Create Release + uses: softprops/action-gh-release@v1 + with: + body: | + ## Docker Images + You can pull the Docker images using: + ```bash + docker pull ghcr.io/${{ github.repository }}/rs-capi:${{ github.ref_name }} + ``` + + Available platforms: + - linux/amd64 + - linux/arm64 + - darwin/amd64 + - darwin/arm64 + + ## Changelog + ${{ steps.changelog.outputs.changelog }} + draft: false + prerelease: false + token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 62ed5f2..a1e93ff 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ tests/ node_modules/ .DS_Store .idea/ -__pycache__/ \ No newline at end of file +__pycache__/ +rs-capi/target/ \ No newline at end of file diff --git a/rs-capi/Cargo.lock b/rs-capi/Cargo.lock index 2fd5fff..59aeabf 100644 --- a/rs-capi/Cargo.lock +++ b/rs-capi/Cargo.lock @@ -1083,7 +1083,7 @@ dependencies = [ ] [[package]] -name = "rs-api" +name = "rs-capi" version = "0.1.0" dependencies = [ "axum", diff --git a/rs-capi/Cargo.toml b/rs-capi/Cargo.toml index 2c2bb78..9ee35f1 100644 --- a/rs-capi/Cargo.toml +++ b/rs-capi/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "rs-api" +name = "rs-capi" version = "0.1.0" edition = "2021" diff --git a/rs-capi/Dockerfile b/rs-capi/Dockerfile new file mode 100644 index 0000000..44ca215 --- /dev/null +++ b/rs-capi/Dockerfile @@ -0,0 +1,26 @@ +FROM rust:1.81.0 AS builder + +RUN printf "\ + [source.crates-io] \n\ + replace-with = 'rsproxy-sparse'\n\ + \n\ + [source.rsproxy-sparse]\n\ + registry = 'sparse+https://rsproxy.cn/index/'\n\ + " > $CARGO_HOME/config.toml +# RUN cargo install crm && crm use rsproxy-sparse + +WORKDIR /workspace +ADD . /workspace +RUN cargo build --bin rs-capi --release + + + +FROM ubuntu:22.04 +# RUN apt-get update && apt-get install -y --no-install-recommends \ +# ca-certificates \ +# && rm -rf /var/lib/apt/lists/* + +COPY --from=builder /workspace/target/release/rs-capi /workspace/rs-capi + +WORKDIR /workspace +CMD ["./rs-capi"] \ No newline at end of file diff --git a/rs-capi/src/main.rs b/rs-capi/src/main.rs index 046fd66..5051f41 100644 --- a/rs-capi/src/main.rs +++ b/rs-capi/src/main.rs @@ -183,7 +183,8 @@ async fn main() { ); // 启动服务器 - let addr = "0.0.0.0:3002"; + let port = std::env::var("PORT").unwrap_or_else(|_| "3000".to_string()); + let addr = format!("0.0.0.0:{}", port); println!("Server running on {}", addr); // 修改服务器启动代码