From 117690e4a4d30c6d656624edf2002afbc8abc437 Mon Sep 17 00:00:00 2001 From: Loong Date: Tue, 17 Oct 2023 00:43:03 +0800 Subject: [PATCH] ci: add a setup action shared by ci and release workflows --- .github/actions/setup/action.yml | 48 +++++++++++++++++++++ .github/workflows/ci.yml | 71 ++++++++------------------------ .github/workflows/release.yml | 36 ++++------------ 3 files changed, 72 insertions(+), 83 deletions(-) create mode 100644 .github/actions/setup/action.yml diff --git a/.github/actions/setup/action.yml b/.github/actions/setup/action.yml new file mode 100644 index 00000000..3a33ecc9 --- /dev/null +++ b/.github/actions/setup/action.yml @@ -0,0 +1,48 @@ +name: Setup Rust +description: 'Setup Rust for cross-compilation' +inputs: + os: + description: 'Host and target OS' + required: true + arch: + description: 'Target architecture' + required: true + +runs: + using: 'composite' + steps: + - name: Compute Target Triple + shell: bash + run: | + os=${{ inputs.os }} + arch=${{ inputs.arch }} + case "$os" in + ubuntu*) + target="$arch-unknown-linux-gnu" + ;; + macos*) + target="$arch-apple-darwin" + ;; + windows*) + target="$arch-pc-windows-msvc" + ;; + *) + echo "Unknown OS: $os" + exit 1 + ;; + esac + echo "CARGO_BUILD_TARGET=$target" >> $GITHUB_ENV + - name: Install Target + if: inputs.arch == 'aarch64' + shell: bash + run: rustup target add $CARGO_BUILD_TARGET + - name: Install Cross + if: inputs.arch == 'aarch64' && inputs.os == 'ubuntu-latest' + uses: taiki-e/install-action@v2 + with: + tool: cross + - name: Use Cross + if: inputs.arch == 'aarch64' && inputs.os == 'ubuntu-latest' + shell: bash + run: echo "CARGO=cross" >> $GITHUB_ENV + diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 234bf58e..504c4ad1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,6 +19,7 @@ on: - ".github/workflows/ci.yml" env: + RUST_BACKTRACE: full CARGO_TERM_COLOR: always defaults: @@ -26,26 +27,6 @@ defaults: shell: bash jobs: - lint: - name: Lint - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: - - ubuntu-latest - - macos-latest - - windows-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - - name: Setup Rust - run: rustup component add clippy rustfmt - - name: Lint (clippy) - run: cargo clippy --all-targets --all-features -- -D warnings - - name: Lint (rustfmt) - run: cargo fmt --all --check - build: name: Build and Test needs: lint @@ -67,44 +48,28 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 - - name: Install Cross - if: matrix.os == 'ubuntu-latest' && matrix.arch == 'aarch64' - uses: taiki-e/install-action@v2 + - name: Setup Rust + uses: ./.github/actions/setup with: - tool: cross - - name: Setup environment + os: ${{ matrix.os }} + arch: ${{ matrix.arch }} + - name: Lint (clippy) + run: | + cargo clippy --all-targets --all-features -- -D warnings + - name: Lint (rustfmt) run: | - OS=${{ matrix.os }} - ARCH=${{ matrix.arch }} - if [ "$OS" = "macos-latest" ]; then - CARGO_CMD=cargo - CARGO_BUILD_TARGET="${ARCH}-apple-darwin" - rustup target add $CARGO_BUILD_TARGET - elif [ "$OS" = "windows-latest" ]; then - CARGO_CMD=cargo - CARGO_BUILD_TARGET="${ARCH}-pc-windows-msvc" - rustup target add $CARGO_BUILD_TARGET - else - if [ "$ARCH" = "aarch64" ]; then - CARGO_CMD=cross - else - CARGO_CMD=cargo - fi - CARGO_BUILD_TARGET="${ARCH}-unknown-linux-gnu" - fi - echo "CARGO_CMD=$CARGO_CMD" >> $GITHUB_ENV - echo "CARGO_BUILD_TARGET=$CARGO_BUILD_TARGET" >> $GITHUB_ENV + cargo fmt --all -- --check - name: Build (maa-cli) run: | - $CARGO_CMD build --package maa-cli --locked + cargo build --package maa-cli --locked - name: Test (maa-cli) if: matrix.arch == 'x86_64' || matrix.os == 'ubuntu-latest' run: | - $CARGO_CMD test --package maa-cli --locked + cargo test --package maa-cli --locked - name: Test (maa-cli, no-default-features) if: matrix.arch == 'x86_64' || matrix.os == 'ubuntu-latest' run: | - $CARGO_CMD test --package maa-cli --no-default-features --locked + cargo test --package maa-cli --no-default-features --locked - name: Install MaaCore if: matrix.arch == 'x86_64' env: @@ -115,13 +80,11 @@ jobs: if: matrix.arch == 'x86_64' run: | MAA_CORE_DIR="$(cargo run -- dir lib)" - RESOURCE_DIR="$(cargo run -- dir resource)" - cd "$MAA_CORE_DIR" || exit 1 + MAA_RESOURCE_DIR="$(cargo run -- dir resource) ls -l "$MAA_CORE_DIR" - cd "$RESOURCE_DIR" || exit 1 - ls -l "$RESOURCE_DIR" + ls -l "$MAA_RESOURCE_DIR" echo "MAA_CORE_DIR=$MAA_CORE_DIR" >> $GITHUB_ENV - echo "RESOURCE_DIR=$RESOURCE_DIR" >> $GITHUB_ENV + echo "MAA_RESOURCE_DIR=$RESOURCE_DIR" >> $GITHUB_ENV - name: Test (maa-sys, static) # It seems rust needs a static library to check the linking. # Without this, we can not build maa-sys on Windows. @@ -151,7 +114,7 @@ jobs: mv "$MAA_CORE_DIR" "$lib_dir" ls -l "$lib_dir" mkdir -p "$share_dir" - mv "$RESOURCE_DIR" "$share_dir/resource" + mv "$MAA_RESOURCE_DIR" "$share_dir/resource" ls -l "$share_dir" cargo run -- version cargo run -- run daily --dry-run --batch diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 329cf9aa..ae4bc6b3 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -26,6 +26,7 @@ permissions: jobs: meta: + name: Meta runs-on: ubuntu-latest outputs: dryrun: ${{ steps.dryrun.outputs.dryrun }} @@ -80,33 +81,11 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 - - name: Install Cross - if: matrix.arch == 'aarch64' && matrix.os == 'ubuntu-latest' - uses: taiki-e/install-action@v2 + - name: Setup Rust + uses: ./.github/actions/setup with: - tool: cross - - name: Setup Environment - run: | - OS=${{ matrix.os }} - ARCH=${{ matrix.arch }} - if [ "$OS" = "macos-latest" ]; then - CARGO_CMD=cargo - CARGO_BUILD_TARGET="$ARCH-apple-darwin" - rustup target add $CARGO_BUILD_TARGET - elif [ "$OS" = "windows-latest" ]; then - CARGO_CMD=cargo - CARGO_BUILD_TARGET="$ARCH-pc-windows-msvc" - rustup target add $CARGO_BUILD_TARGET - else - if [ "$ARCH" = "aarch64" ]; then - CARGO_CMD=cross - else - CARGO_CMD=cargo - fi - CARGO_BUILD_TARGET="$ARCH-unknown-linux-gnu" - fi - echo "CARGO_CMD=$CARGO_CMD" >> $GITHUB_ENV - echo "CARGO_BUILD_TARGET=$CARGO_BUILD_TARGET" >> $GITHUB_ENV + os: ${{ matrix.os }} + arch: ${{ matrix.arch }} - name: Build run: | $CARGO_CMD build --release --locked --package maa-cli @@ -199,10 +178,9 @@ jobs: generate_release_notes: true fail_on_unmatched_files: true files: | - maa_cli-v${{ needs.meta.outputs.version }}-x86_64-unknown-linux-gnu.tar.gz* - maa_cli-v${{ needs.meta.outputs.version }}-aarch64-unknown-linux-gnu.tar.gz* + maa_cli-v${{ needs.meta.outputs.version }}-*-unknown-linux-gnu.tar.gz* maa_cli-v${{ needs.meta.outputs.version }}-universal-apple-darwin.zip* - maa_cli-v${{ needs.meta.outputs.version }}-x86_64-pc-windows-msvc.zip* + maa_cli-v${{ needs.meta.outputs.version }}-*-pc-windows-msvc.zip* - name: Commit version.json and Push working-directory: version run: |