Skip to content

Commit

Permalink
ci: add a setup action shared by ci and release workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
wangl-cc committed Oct 16, 2023
1 parent d813338 commit 117690e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 83 deletions.
48 changes: 48 additions & 0 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
@@ -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

71 changes: 17 additions & 54 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,14 @@ on:
- ".github/workflows/ci.yml"

env:
RUST_BACKTRACE: full
CARGO_TERM_COLOR: always

defaults:
run:
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
Expand All @@ -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:
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
36 changes: 7 additions & 29 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ permissions:

jobs:
meta:
name: Meta
runs-on: ubuntu-latest
outputs:
dryrun: ${{ steps.dryrun.outputs.dryrun }}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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: |
Expand Down

0 comments on commit 117690e

Please sign in to comment.