From 55b4e5ccae77531819ebf0c65f2863cffd0a4266 Mon Sep 17 00:00:00 2001 From: Kevin Cui Date: Thu, 22 Feb 2024 21:27:57 +0800 Subject: [PATCH] chore(ci): add test ci (#62) Signed-off-by: Kevin Cui --- .github/pull_request_template.md | 12 ++ .github/workflows/release.yml | 2 +- .github/workflows/test.yml | 205 +++++++++++++++++++++++++++++++ 3 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 .github/pull_request_template.md create mode 100644 .github/workflows/test.yml diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..a090151 --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,12 @@ +## Description of Change + + + +## Required to run CI + +- [ ] applehv-rootfs-amd64 +- [ ] applehv-rootfs-arm64 +- [ ] initrd-amd64 +- [ ] initrd-arm64 +- [ ] kernel-amd64 +- [ ] kernel-arm64 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 6941028..03d0ef7 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -64,7 +64,7 @@ jobs: run: make defconfig-${{ matrix.target }}-${{ matrix.arch }} - name: Build - run: make build-${{ matrix.target }}-${{ matrix.arch }} + run: make build-${{ matrix.target }}-${{ matrix.arch }} -j$(nproc) - name: Upload binaries run: | diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..1dafa35 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,205 @@ +name: Test + +on: + pull_request: + branches: + - main + types: + - opened + - synchronize + - reopened + - edited + +permissions: + contents: read + pull-requests: write + issues: write + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + pre-test: + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.set-matrix.outputs.matrix }} + need_to_run: ${{ steps.set-matrix.outputs.need_to_run }} + + steps: + - name: generate matrix from PR body + id: set-matrix + env: + RAW_BODY: ${{ github.event.pull_request.body }} + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 + with: + script: | + const TASK_REGEXP = /- \[x\] (.*)/g; + const getTask = (text) => { + const result = []; + let match; + while ((match = TASK_REGEXP.exec(text)) !== null) { + result.push(match[1]); + } + + return { + matrix: result, + need_to_run: result.length > 0 + }; + } + + if (context.payload.action !== "edited") { + console.log("not edited event"); + const { matrix, need_to_run } = getTask(process.env.RAW_BODY); + core.setOutput("matrix", matrix); + core.setOutput("need_to_run", need_to_run); + return; + } + + const query = `query ($owner: String!, $name: String!, $number: Int!) { + repository(owner: $owner, name: $name) { + pullRequest(number: $number) { + userContentEdits(first: 2) { + nodes { + diff + } + } + } + } + }`; + + const variables = { + owner: context.repo.owner, + name: context.repo.repo, + number: context.issue.number + }; + + const history = await github.graphql(query, variables); + + if (history.repository.pullRequest.userContentEdits.nodes.length !== 2) { + console.log("not enough history") + const { matrix, need_to_run } = getTask(process.env.RAW_BODY); + core.setOutput("matrix", matrix); + core.setOutput("need_to_run", need_to_run); + return; + } + + const latestTasks = getTask(history.repository.pullRequest.userContentEdits.nodes[0].diff); + const previousTasks = getTask(history.repository.pullRequest.userContentEdits.nodes[1].diff); + + const sortLatest = latestTasks.matrix.sort(); + const sortPrevious = previousTasks.matrix.sort(); + + for (const [index, task] of sortPrevious.entries()) { + if (sortLatest[index] === undefined) { + console.log("latest task is less than previous, skip"); + continue; + } + + if (task !== sortLatest[index]) { + console.log(`has added ${task}, need to run`); + const { matrix, need_to_run } = latestTasks; + core.setOutput("matrix", matrix); + core.setOutput("need_to_run", need_to_run); + return; + } + } + + console.log("no new task added, skip"); + core.setOutput("matrix", []); + core.setOutput("need_to_run", false); + + test: + runs-on: ubuntu-latest + needs: pre-test + if: ${{ needs.pre-test.outputs.need_to_run == 'true' }} + + outputs: + matrix: ${{ needs.pre-test.outputs.matrix }} + + strategy: + fail-fast: true + matrix: + target-arch: ${{ fromJSON(needs.pre-test.outputs.matrix) }} + + steps: + - name: Checkout + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 + + - name: Update kernel submodules + if: ${{ startsWith(matrix.target-arch, 'kernel') }} + run: git submodule update --init kernel + + - name: Update buildroot submodules + if: ${{ !startsWith(matrix.target-arch, 'kernel') }} + run: git submodule update --init buildroot + + - name: Install deps + run: sudo apt-get install -y build-essential flex bison libssl-dev libelf-dev bc + + - name: Install kernel arm64 deps + if: ${{ matrix.target-arch == 'kernel-arm64' }} + run: sudo apt-get install -y gcc-aarch64-linux-gnu + + - name: Defconfig + run: make defconfig-${{ matrix.target-arch }} + + - name: Build + run: make build-${{ matrix.target-arch }} -j$(nproc) + + - name: Get output file size + run: | + FILE_PATH=$(make print-outpath-${{ matrix.target-arch }}) + SIZE_B=$(stat -c%s "$FILE_PATH") + SIZE_MB=$(echo $SIZE_B | awk '{ printf "%.4f", $1 / 1024 / 1024 }') + + mkdir outputs-size + echo "${{ matrix.target-arch }} $SIZE_MB $SIZE_B" > outputs-size/${{ matrix.target-arch }}.txt + + - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 + with: + name: ${{ github.run_id }}-${{ github.run_number }}-${{ matrix.target-arch }} + path: outputs-size/*.txt + retention-days: 1 + + post-test: + runs-on: ubuntu-latest + needs: + - pre-test + - test + if: ${{ needs.pre-test.outputs.need_to_run == 'true' }} + + steps: + - name: Load outputs-size + uses: actions/download-artifact@eaceaf801fd36c7dee90939fad912460b18a1ffe # v4.1.2 + with: + pattern: ${{ github.run_id }}-${{ github.run_number }}-* + path: outputs-size + merge-multiple: true + + - name: Format outputs-size + id: size + run: | + cat > comment.md <