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..57cb651 --- /dev/null +++ b/rust-build/README.md @@ -0,0 +1,81 @@ +# 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: [pull_request] + +jobs: + build-and-test: + uses: iExecBlockchainComputing/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' +``` + +## 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: iExecBlockchainComputing/github-actions-workflows/.github/workflows/rust-build.yml@main +``` + +### Build, Test, and Upload Artifact + +```yaml +jobs: + build-and-test: + uses: iExecBlockchainComputing/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: iExecBlockchainComputing/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