From 8c8b09087b3bc391b85a18edc31befc79dd6aa3d Mon Sep 17 00:00:00 2001 From: Ugo Mignon Date: Thu, 15 May 2025 22:43:19 +0200 Subject: [PATCH 1/2] feat(rust-build): add reusable workflow --- .github/workflows/rust-build.yml | 103 +++++++++++++++++++++++++++++++ rust-build/README.md | 87 ++++++++++++++++++++++++++ 2 files changed, 190 insertions(+) create mode 100644 .github/workflows/rust-build.yml create mode 100644 rust-build/README.md diff --git a/.github/workflows/rust-build.yml b/.github/workflows/rust-build.yml new file mode 100644 index 0000000..6b200ea --- /dev/null +++ b/.github/workflows/rust-build.yml @@ -0,0 +1,103 @@ +name: Build, Test and Publish Rust Package + +on: + workflow_call: + inputs: + rust-version: + description: 'Rust version to use' + default: 'stable' + type: string + build-target: + description: 'Cargo profile to use for building (debug, release)' + default: 'release' + type: string + enable-cache: + description: 'Enable caching of dependencies' + default: true + type: boolean + publish-crates-io: + description: 'Publish package to crates.io' + default: false + type: boolean + upload-artifact: + description: 'Upload build artifact' + default: false + type: boolean + artifact-name: + description: 'Name of the artifact to upload' + type: string + required: false + artifact-path: + description: 'Path to the artifact to upload' + type: string + required: false + secrets: + CRATES_IO_TOKEN: + required: false + +jobs: + build: + runs-on: ubuntu-latest + outputs: + build_success: ${{ steps.set-output.outputs.build_success }} + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Install Rust toolchain + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: ${{ inputs.rust-version }} + override: true + + - name: Cache dependencies + if: ${{ inputs.enable-cache }} + uses: actions/cache@v4 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + + - name: Build + run: cargo build --profile ${{ inputs.build-target }} + + - name: Run tests + run: cargo test --profile ${{ inputs.build-target }} + + - name: Set build success output + id: set-output + run: echo "build_success=true" >> $GITHUB_OUTPUT + + - name: Upload artifact + if: ${{ inputs.upload-artifact }} + uses: actions/upload-artifact@v4 + with: + name: ${{ inputs.artifact-name }} + path: ${{ inputs.artifact-path }} + + publish: + needs: build + if: ${{ inputs.publish-crates-io && needs.build.outputs.build_success == 'true' }} + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Install Rust toolchain + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: ${{ inputs.rust-version }} + override: true + + - name: Login to crates.io + run: cargo login ${{ secrets.CRATES_IO_TOKEN }} + + - name: Package for crates.io + run: cargo package + + - name: Publish to crates.io + run: cargo publish \ No newline at end of file diff --git a/rust-build/README.md b/rust-build/README.md new file mode 100644 index 0000000..b21d096 --- /dev/null +++ b/rust-build/README.md @@ -0,0 +1,87 @@ +# Rust Build Workflow + +A reusable GitHub Actions workflow for building, testing, and publishing Rust packages. + +## Features + +- Build and test Rust packages +- Cache dependencies for faster builds +- Publish packages to crates.io +- Upload build artifacts + +## Usage + +```yaml +name: Rust CI + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + build-and-test: + uses: your-org/github-actions-workflows/.github/workflows/rust-build.yml@main + with: + rust-version: 'stable' + build-target: 'release' + enable-cache: true + upload-artifact: true + artifact-name: 'my-rust-app' + artifact-path: 'target/release/my-app' + secrets: + CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }} +``` + +## Inputs + +| Name | Description | Default | Required | +|---------------------|----------------------------------------------------|-----------|-------------------------------------| +| `rust-version` | Rust version to use | `stable` | No | +| `build-target` | Cargo profile to use for building (debug, release) | `release` | No | +| `enable-cache` | Enable caching of dependencies | `true` | No | +| `publish-crates-io` | Publish package to crates.io | `false` | No | +| `upload-artifact` | Upload build artifact | `false` | No | +| `artifact-name` | Name of the artifact to upload | - | Only if `upload-artifact` is `true` | +| `artifact-path` | Path to the artifact to upload | - | Only if `upload-artifact` is `true` | + +## Secrets + +| Name | Description | Required | +|-------------------|-----------------------------------|---------------------------------------| +| `CRATES_IO_TOKEN` | Token for publishing to crates.io | Only if `publish-crates-io` is `true` | + +## Examples + +### Basic Build and Test + +```yaml +jobs: + build-and-test: + uses: your-org/github-actions-workflows/.github/workflows/rust-build.yml@main +``` + +### Build, Test, and Upload Artifact + +```yaml +jobs: + build-and-test: + uses: your-org/github-actions-workflows/.github/workflows/rust-build.yml@main + with: + upload-artifact: true + artifact-name: 'my-rust-app' + artifact-path: 'target/release/my-app' +``` + +### Build, Test, and Publish to crates.io + +```yaml +jobs: + build-and-publish: + uses: your-org/github-actions-workflows/.github/workflows/rust-build.yml@main + with: + publish-crates-io: true + secrets: + CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }} +``` \ No newline at end of file From d1a02c8afe1f1d1ce6c293ae15dfa3d51cf91357 Mon Sep 17 00:00:00 2001 From: Ugo Mignon Date: Fri, 16 May 2025 08:09:58 +0200 Subject: [PATCH 2/2] docs(rust-build): update workflow references to iExecBlockchainComputing --- rust-build/README.md | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/rust-build/README.md b/rust-build/README.md index b21d096..57cb651 100644 --- a/rust-build/README.md +++ b/rust-build/README.md @@ -14,15 +14,11 @@ A reusable GitHub Actions workflow for building, testing, and publishing Rust pa ```yaml name: Rust CI -on: - push: - branches: [ main ] - pull_request: - branches: [ main ] +on: [pull_request] jobs: build-and-test: - uses: your-org/github-actions-workflows/.github/workflows/rust-build.yml@main + uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/rust-build.yml@main with: rust-version: 'stable' build-target: 'release' @@ -30,8 +26,6 @@ jobs: upload-artifact: true artifact-name: 'my-rust-app' artifact-path: 'target/release/my-app' - secrets: - CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }} ``` ## Inputs @@ -59,7 +53,7 @@ jobs: ```yaml jobs: build-and-test: - uses: your-org/github-actions-workflows/.github/workflows/rust-build.yml@main + uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/rust-build.yml@main ``` ### Build, Test, and Upload Artifact @@ -67,7 +61,7 @@ jobs: ```yaml jobs: build-and-test: - uses: your-org/github-actions-workflows/.github/workflows/rust-build.yml@main + uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/rust-build.yml@main with: upload-artifact: true artifact-name: 'my-rust-app' @@ -79,7 +73,7 @@ jobs: ```yaml jobs: build-and-publish: - uses: your-org/github-actions-workflows/.github/workflows/rust-build.yml@main + uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/rust-build.yml@main with: publish-crates-io: true secrets: