diff --git a/.github/actions/setup-rust/action.yml b/.github/actions/setup-rust/action.yml new file mode 100644 index 0000000..c687c71 --- /dev/null +++ b/.github/actions/setup-rust/action.yml @@ -0,0 +1,53 @@ +name: 'Setup Rust environment' +description: 'Composite action to checkout the repo, restore cargo caches and set up the Rust toolchain. Use this from job steps to keep setup DRY across jobs.' +inputs: + toolchain: + description: 'Rust toolchain to install' + required: false + default: 'stable' + override: + description: 'Whether to override the default toolchain' + required: false + default: 'true' + components: + description: 'Comma-separated list of additional rust components to install' + required: false + default: 'clippy, rustfmt' +runs: + using: 'composite' + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Cache cargo registry + uses: actions/cache@v3 + with: + path: ~/.cargo/registry + key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }} + restore-keys: | + ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }} + + - name: Cache cargo index + uses: actions/cache@v3 + with: + path: ~/.cargo/index + key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }} + restore-keys: | + ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }} + + - name: Cache cargo build (target) + uses: actions/cache@v3 + with: + path: target + key: ${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.lock') }} + restore-keys: | + ${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.lock') }} + + - name: Set up rust toolchain + uses: dtolnay/rust-toolchain@stable + with: + toolchain: ${{ inputs.toolchain }} + override: ${{ inputs.override }} + components: ${{ inputs.components }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..1a8b5ce --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,77 @@ +# this workflow runs tests on pull request and push events targeting master branch +# it also verify the generated code is up to date and valid + +name: Test +on: + pull_request: + branches: + - master + push: + branches: + - master + + +jobs: + # setup is now handled by a composite action used by downstream jobs to keep + # the workflow DRY. The composite action performs checkout, cache restore and + # toolchain setup. + + verify-generated-code: + # no need to depend on a separate setup job; the composite action runs in + # this job and restores caches/toolchain here. + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Setup Rust, checkout and restore caches + uses: ./.github/actions/setup-rust + + - name: generate entities from migration files + run: | + cd apps/cli + cargo run -- db:migrate_and_generate --output-path ../../public/database/src/generated/entities + - name: Check for uncommitted changes in /generated/ + run: | + if [[ -n $(git status --porcelain | grep '^ M .*\/generated\/') ]]; then + echo "Generated code is not up to date. Please run the code generation locally and commit the changes." + git status --porcelain | grep '^ M .*\/generated\/' + exit 1 + else + echo "Generated code is up to date." + fi + + test: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Setup Rust, checkout and restore caches + uses: ./.github/actions/setup-rust + + - name: Run tests + run: cargo test --all-features + + lint: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Setup Rust, checkout and restore caches + uses: ./.github/actions/setup-rust + with: + components: clippy, rustfmt + + - name: Run clippy + run: cargo clippy --all-features -- -D warnings + + - name: Check code formatting + run: cargo fmt --all -- --check \ No newline at end of file